]> granicus.if.org Git - re2c/commitdiff
Rewrite vernum function
authorMike Gilbert <floppym@gentoo.org>
Tue, 28 Aug 2018 16:01:07 +0000 (12:01 -0400)
committerUlya Trofimovich <skvadrik@gmail.com>
Tue, 28 Aug 2018 19:19:13 +0000 (20:19 +0100)
Fixes: https://github.com/skvadrik/re2c/issues/211
re2c/src/conf/msg.cc

index 72eaa452de6c1089fdd8cd0a62c7ab62594e5557..a133b5f3d73f96f6c0f6ae1d22d14dc8d14f9541 100644 (file)
@@ -102,23 +102,22 @@ void usage()
 void vernum ()
 {
     std::string vernum (PACKAGE_VERSION);
-    if (vernum[1] == '.')
-    {
-        vernum.insert(0, "0");
-    }
-    vernum.erase(2, 1);
-    if (vernum[3] == '.')
-    {
-        vernum.insert(2, "0");
-    }
-    vernum.erase(4, 1);
-    if (vernum.length() < 6 || vernum[5] < '0' || vernum[5] > '9')
+    std::string parts[3];
+    unsigned p = 0;
+
+    for (unsigned i = 0; p < 3 && i < vernum.length (); i++)
     {
-        vernum.insert(4, "0");
+        if (vernum[i] == '.')
+            p++;
+        else
+            parts[p].push_back (vernum[i]);
     }
-    vernum.resize(6, '0');
 
-    printf ("%s\n", vernum.c_str ());
+    for (p = 0; p < 3; p++)
+        while (parts[p].length () < 2)
+            parts[p].insert (0, 1, '0');
+
+    printf ("%s%s%s\n", parts[0].c_str (), parts[1].c_str (), parts[2].c_str ());
 }
 
 void version ()