time-util,socket: accept both kinds of unicode µ symbols

Apparently there are two µ symbols, accept both when parsing.

One is the greek small letter mu (μ) the other is the micro sign (µ).
Unicode recommendation considers both equivalent, and says use of greek
small letter mu is preferred. See:

https://www.unicode.org/reports/tr25

Hence accept both when parsing.

Inspired by: #28029
This commit is contained in:
Lennart Poettering 2023-06-14 10:09:22 +02:00
parent 5bc4570fd4
commit d0a6d7c4d1
3 changed files with 10 additions and 6 deletions

View file

@ -1046,7 +1046,8 @@ static const char* extract_multiplier(const char *p, usec_t *ret) {
{ "y", USEC_PER_YEAR },
{ "usec", 1ULL },
{ "us", 1ULL },
{ "µs", 1ULL },
{ "μs", 1ULL }, /* U+03bc (aka GREEK SMALL LETTER MU) */
{ "µs", 1ULL }, /* U+b5 (aka MICRO SIGN) */
};
assert(p);
@ -1224,7 +1225,8 @@ static const char* extract_nsec_multiplier(const char *p, nsec_t *ret) {
{ "y", NSEC_PER_YEAR },
{ "usec", NSEC_PER_USEC },
{ "us", NSEC_PER_USEC },
{ "µs", NSEC_PER_USEC },
{ "μs", NSEC_PER_USEC }, /* U+03bc (aka GREEK LETTER MU) */
{ "µs", NSEC_PER_USEC }, /* U+b5 (aka MICRO SIGN) */
{ "nsec", 1ULL },
{ "ns", 1ULL },
{ "", 1ULL }, /* default is nsec */
@ -1701,9 +1703,9 @@ TimestampStyle timestamp_style_from_string(const char *s) {
t = (TimestampStyle) string_table_lookup(timestamp_style_table, ELEMENTSOF(timestamp_style_table), s);
if (t >= 0)
return t;
if (streq_ptr(s, "µs"))
if (STRPTR_IN_SET(s, "µs", "μs")) /* acccept both µ symbols in unicode, i.e. micro symbol + greek small letter mu. */
return TIMESTAMP_US;
if (streq_ptr(s, "µs+utc"))
if (STRPTR_IN_SET(s, "µs+utc", "μs+utc"))
return TIMESTAMP_US_UTC;
return t;
}

View file

@ -3519,7 +3519,7 @@ SocketTimestamping socket_timestamping_from_string_harder(const char *p) {
* too. */
if (streq(p, "nsec"))
return SOCKET_TIMESTAMPING_NS;
if (STR_IN_SET(p, "usec", "µs"))
if (STR_IN_SET(p, "usec", "µs", "μs")) /* Accept both small greek letter mu + micro sign unicode codepoints */
return SOCKET_TIMESTAMPING_US;
r = parse_boolean(p);

View file

@ -35,7 +35,9 @@ TEST(parse_sec) {
assert_se(u == 700 * USEC_PER_MSEC);
assert_se(parse_sec("23us", &u) >= 0);
assert_se(u == 23);
assert_se(parse_sec("23µs", &u) >= 0);
assert_se(parse_sec("23μs", &u) >= 0); /* greek small letter mu */
assert_se(u == 23);
assert_se(parse_sec("23µs", &u) >= 0); /* micro symbol */
assert_se(u == 23);
assert_se(parse_sec("infinity", &u) >= 0);
assert_se(u == USEC_INFINITY);