]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
percent-util: when parsing permyriads, permit percents too with 1 place after the dot
authorLennart Poettering <lennart@poettering.net>
Wed, 17 Feb 2021 14:33:05 +0000 (15:33 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 18 Feb 2021 21:36:34 +0000 (22:36 +0100)
Previously, when parsing myriads, we'd support:

 x%          → percent, no places after the dot
 x.yz%       → percent, two places after the dot
 x‰          → permille, no places after the dot
 x.y‰        → permille, one place after the dot
 x‱          → permyriad, no places after the dot

What's missing is:

 x.y%        → percent, one place after the dot

Let's add it in.

src/basic/percent-util.c
src/test/test-percent-util.c

index f58a51dcf979fe5ace56feca74186f1b2e64e505..06f20fd61e9ab239b7c952046055a1ce20c1a4b6 100644 (file)
@@ -64,11 +64,23 @@ static int parse_parts_value_with_hundredths_place(const char *p, const char *sy
 
         dot = memchr(p, '.', pc - p);
         if (dot) {
-                if (dot + 3 != pc)
-                        return -EINVAL;
-                if (dot[1] < '0' || dot[1] > '9' || dot[2] < '0' || dot[2] > '9')
+                if (dot + 3 == pc) {
+                        /* Support two places after the dot */
+
+                        if (dot[1] < '0' || dot[1] > '9' || dot[2] < '0' || dot[2] > '9')
+                                return -EINVAL;
+                        q = (dot[1] - '0') * 10 + (dot[2] - '0');
+
+                } else if (dot + 2 == pc) {
+                        /* Support one place after the dot */
+
+                        if (dot[1] < '0' || dot[1] > '9')
+                                return -EINVAL;
+                        q = (dot[1] - '0') * 10;
+                } else
+                        /* We do not support zero or more than two places */
                         return -EINVAL;
-                q = (dot[1] - '0') * 10 + (dot[2] - '0');
+
                 n = strndupa(p, dot - p);
         } else {
                 q = 0;
index 9d0ad90de11c6d91a15c39ad059d25b5acae9051..75f8a6c83d86603b84d71e28aed4b5aad7571b93 100644 (file)
@@ -7,6 +7,7 @@ static void test_parse_percent(void) {
         assert_se(parse_percent("") == -EINVAL);
         assert_se(parse_percent("foo") == -EINVAL);
         assert_se(parse_percent("0") == -EINVAL);
+        assert_se(parse_percent("0.1") == -EINVAL);
         assert_se(parse_percent("50") == -EINVAL);
         assert_se(parse_percent("100") == -EINVAL);
         assert_se(parse_percent("-1") == -EINVAL);
@@ -34,6 +35,10 @@ static void test_parse_permille(void) {
         assert_se(parse_permille("50") == -EINVAL);
         assert_se(parse_permille("100") == -EINVAL);
         assert_se(parse_permille("-1") == -EINVAL);
+        assert_se(parse_permille("0.1") == -EINVAL);
+        assert_se(parse_permille("5%") == 50);
+        assert_se(parse_permille("5.5%") == 55);
+        assert_se(parse_permille("5.12%") == -EINVAL);
 
         assert_se(parse_permille("0‰") == 0);
         assert_se(parse_permille("555‰") == 555);
@@ -45,6 +50,7 @@ static void test_parse_permille(void) {
         assert_se(parse_permille("‰1") == -EINVAL);
         assert_se(parse_permille("1‰‰") == -EINVAL);
         assert_se(parse_permille("3.2‰") == -EINVAL);
+        assert_se(parse_permille("0.1‰") == -EINVAL);
 
         assert_se(parse_permille("0%") == 0);
         assert_se(parse_permille("55%") == 550);
@@ -57,6 +63,7 @@ static void test_parse_permille(void) {
         assert_se(parse_permille("%1") == -EINVAL);
         assert_se(parse_permille("1%%") == -EINVAL);
         assert_se(parse_permille("3.21%") == -EINVAL);
+        assert_se(parse_permille("0.1%") == 1);
 }
 
 static void test_parse_permille_unbounded(void) {
@@ -107,6 +114,8 @@ static void test_parse_permyriad(void) {
 
         assert_se(parse_permyriad("0%") == 0);
         assert_se(parse_permyriad("55%") == 5500);
+        assert_se(parse_permyriad("55.5%") == 5550);
+        assert_se(parse_permyriad("55.50%") == 5550);
         assert_se(parse_permyriad("55.53%") == 5553);
         assert_se(parse_permyriad("100%") == 10000);
         assert_se(parse_permyriad("-7%") == -ERANGE);