]> granicus.if.org Git - re2c/commitdiff
vernum: move version-string-to-vernum converter to a separate helper
authorSergei Trofimovich <slyfox@gentoo.org>
Tue, 28 Aug 2018 22:05:59 +0000 (23:05 +0100)
committerUlya Trofimovich <skvadrik@gmail.com>
Wed, 29 Aug 2018 06:12:06 +0000 (07:12 +0100)
No functional change. While at it added tests
to cover past failures:
- "1.1": https://github.com/skvadrik/re2c/issues/211
- "0.14": https://sourceforge.net/p/re2c/bugs/55/

Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
re2c/Makefile.am
re2c/src/conf/msg.cc
re2c/src/conf/ver_to_vernum.cc [new file with mode: 0644]
re2c/src/conf/ver_to_vernum.h [new file with mode: 0644]
re2c/src/test/ver_to_vernum/test.cc [new file with mode: 0644]

index a76bc364522fc10fe17d279341fb3afa2a02fd87..b6fceace746ab42e8c91f877726f13cb6d9f4b17 100644 (file)
@@ -18,6 +18,7 @@ SRC_HDR = \
        src/code/print.h \
        src/conf/msg.h \
        src/conf/opt.h \
+       src/conf/ver_to_vernum.h \
        src/conf/warn.h \
        src/adfa/action.h \
        src/adfa/adfa.h \
@@ -83,6 +84,7 @@ SRC = \
        src/code/print.cc \
        src/conf/msg.cc \
        src/conf/opt.cc \
+       src/conf/ver_to_vernum.cc \
        src/conf/warn.cc \
        src/nfa/dump.cc \
        src/nfa/estimate_size.cc \
@@ -329,8 +331,14 @@ testston32unsafe_SOURCES = \
        src/test/s_to_n32_unsafe/test.cc \
        src/util/s_to_n32_unsafe.cc
 
+TST_VER_TO_VERNUM = testvertovernum
+testvertovernum_SOURCES = \
+       src/test/ver_to_vernum/test.cc \
+       src/conf/ver_to_vernum.cc
+
 check_PROGRAMS = \
        $(TST_RANGE) \
-       $(TST_S_TO_N32_UNSAFE)
+       $(TST_S_TO_N32_UNSAFE) \
+       $(TST_VER_TO_VERNUM)
 
 TESTS = $(TST_SUITE) $(check_PROGRAMS)
index a133b5f3d73f96f6c0f6ae1d22d14dc8d14f9541..4d5fd64b6da277e217382cb1645842ea98db262c 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "config.h"
 #include "src/conf/msg.h"
+#include "src/conf/ver_to_vernum.h"
 
 extern const char *help; // autogenerated
 
@@ -101,23 +102,7 @@ void usage()
 
 void vernum ()
 {
-    std::string vernum (PACKAGE_VERSION);
-    std::string parts[3];
-    unsigned p = 0;
-
-    for (unsigned i = 0; p < 3 && i < vernum.length (); i++)
-    {
-        if (vernum[i] == '.')
-            p++;
-        else
-            parts[p].push_back (vernum[i]);
-    }
-
-    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 ());
+    printf ("%s\n", ver_to_vernum(PACKAGE_VERSION).c_str ());
 }
 
 void version ()
diff --git a/re2c/src/conf/ver_to_vernum.cc b/re2c/src/conf/ver_to_vernum.cc
new file mode 100644 (file)
index 0000000..6d0f675
--- /dev/null
@@ -0,0 +1,29 @@
+#include <stdio.h> /* fprintf() */
+#include <string>
+
+#include "config.h"
+#include "src/conf/ver_to_vernum.h"
+
+namespace re2c {
+
+std::string ver_to_vernum(const std::string & ver)
+{
+    std::string parts[3];
+    unsigned p = 0;
+
+    for (unsigned i = 0; p < 3 && i < ver.length (); i++)
+    {
+        if (ver[i] == '.')
+            p++;
+        else
+            parts[p].push_back (ver[i]);
+    }
+
+    for (p = 0; p < 3; p++)
+        while (parts[p].length () < 2)
+            parts[p].insert (0, 1, '0');
+
+    return parts[0] + parts[1] + parts[2];
+}
+
+} // namespace re2c
diff --git a/re2c/src/conf/ver_to_vernum.h b/re2c/src/conf/ver_to_vernum.h
new file mode 100644 (file)
index 0000000..43bac2b
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef _RE2C_CONF_VER_
+#define _RE2C_CONF_VER_
+
+#include <string>
+
+namespace re2c {
+
+std::string ver_to_vernum(const std::string & ver);
+
+} // namespace re2c
+
+#endif // _RE2C_CONF_VER_
diff --git a/re2c/src/test/ver_to_vernum/test.cc b/re2c/src/test/ver_to_vernum/test.cc
new file mode 100644 (file)
index 0000000..9e0ee3b
--- /dev/null
@@ -0,0 +1,53 @@
+#include <stdio.h> /* fprintf() */
+#include <string>
+#include "src/conf/ver_to_vernum.h" /* re2c::ver_to_vernum() */
+
+namespace re2c_test {
+
+struct examples_t {
+    std::string in;
+    std::string expected;
+};
+
+static const examples_t EXAMPLES[] =
+{
+    { "0.14",      "001400", },
+    { "0.14.1",    "001401", },
+    { "1",         "010000", },
+    { "1.1",       "010100", },
+    // Those used to exist. Should be supported?
+    //{ "1.1.dev",   "010100", },
+    { "1.1.1",     "010101", },
+    { "1.1.1.1",   "010101", },
+};
+
+int test ()
+{
+    int failures = 0;
+
+    for (size_t i = 0; i < sizeof (EXAMPLES) / sizeof (EXAMPLES[0]); ++i)
+    {
+        const examples_t & e = EXAMPLES[i];
+
+        std::string got = re2c::ver_to_vernum (e.in);
+        if (got != e.expected)
+        {
+            ++failures;
+            fprintf (stderr, "FAIL: re2c::ver_to_vernum('%s') = '%s', expected '%s'\n",
+                     e.in.c_str(), got.c_str(), e.expected.c_str());
+        }
+        else
+        {
+            fprintf (stderr, "PASS: re2c::ver_to_vernum('%s') = '%s\n", e.in.c_str(), got.c_str());
+        }
+    }
+
+    return failures;
+}
+
+} // namespace re2c_test
+
+int main ()
+{
+    return re2c_test::test ();
+}