[PATCH 2/4] style: make conversion of version string to int public

Akim Demaille <[email protected]>
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <[email protected]>
* src/parse-gram.y (str_to_version): Rename as/move to...
* src/strversion.h, src/strversion.c (strversion_to_int): these new
files.
---
 src/local.mk     |  2 ++
 src/parse-gram.y | 50 ++----------------------------------
 src/strversion.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/strversion.h | 28 ++++++++++++++++++++
 4 files changed, 99 insertions(+), 48 deletions(-)
 create mode 100644 src/strversion.c
 create mode 100644 src/strversion.h

diff --git a/src/local.mk b/src/local.mk
index 32d09d10..08ff62be 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -103,6 +103,8 @@ src_bison_SOURCES =                             \
   src/state.h                                   \
   src/state-item.c                              \
   src/state-item.h                              \
+  src/strversion.c                              \
+  src/strversion.h                              \
   src/symlist.c                                 \
   src/symlist.h                                 \
   src/symtab.c                                  \
diff --git a/src/parse-gram.y b/src/parse-gram.y
index dc776130..7c146ea3 100644
--- a/src/parse-gram.y
+++ b/src/parse-gram.y
@@ -42,8 +42,6 @@
   #include "system.h"
 
   #include <c-ctype.h>
-  #include <errno.h>
-  #include <intprops.h>
   #include <quotearg.h>
   #include <vasnprintf.h>
   #include <xmemdup0.h>
@@ -57,6 +55,7 @@
   #include "reader.h"
   #include "scan-code.h"
   #include "scan-gram.h"
+  #include "strversion.h"
 
   /* Pretend to be at least that version, to check features published
      in that version while developping it.  */
@@ -1043,56 +1042,11 @@ handle_pure_parser (location const *loc, char const *directive)
 }
 
 
-/* Convert VERSION into an int (MAJOR * 10000 + MINOR * 100 + MICRO).
-   E.g., "3.7.4" => 30704, "3.8" => 30800.
-   Return -1 on errors. */
-static int
-str_to_version (char const *version)
-{
-  IGNORE_TYPE_LIMITS_BEGIN
-  int res = 0;
-  errno = 0;
-  char *cp = NULL;
-
-  {
-    long major = strtol (version, &cp, 10);
-    if (errno || cp == version || *cp != '.' || major < 0
-        || INT_MULTIPLY_WRAPV (major, 10000, &res))
-      return -1;
-  }
-
-  {
-    ++cp;
-    char *prev = cp;
-    long minor = strtol (cp, &cp, 10);
-    if (errno || cp == prev || (*cp != '\0' && *cp != '.')
-        || ! (0 <= minor && minor < 100)
-        || INT_MULTIPLY_WRAPV (minor, 100, &minor)
-        || INT_ADD_WRAPV (minor, res, &res))
-      return -1;
-  }
-
-  if (*cp == '.')
-    {
-      ++cp;
-      char *prev = cp;
-      long micro = strtol (cp, &cp, 10);
-      if (errno || cp == prev || (*cp != '\0' && *cp != '.')
-          || ! (0 <= micro && micro < 100)
-          || INT_ADD_WRAPV (micro, res, &res))
-        return -1;
-    }
-
-  IGNORE_TYPE_LIMITS_END
-  return res;
-}
-
-
 static void
 handle_require (location const *loc, char const *version_quoted)
 {
   char *version = unquote (version_quoted);
-  required_version = str_to_version (version);
+  required_version = strversion_to_int (version);
   if (required_version == -1)
     {
       complain (loc, complaint, _("invalid version requirement: %s"),
diff --git a/src/strversion.c b/src/strversion.c
new file mode 100644
index 00000000..27c71d34
--- /dev/null
+++ b/src/strversion.c
@@ -0,0 +1,67 @@
+/* Convert version string to int.
+
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This file is part of Bison, the GNU Compiler Compiler.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+#include "system.h"
+
+#include "strversion.h"
+
+#include <errno.h>
+#include <intprops.h>
+
+int
+strversion_to_int (char const *version)
+{
+  IGNORE_TYPE_LIMITS_BEGIN
+  int res = 0;
+  errno = 0;
+  char *cp = NULL;
+
+  {
+    long major = strtol (version, &cp, 10);
+    if (errno || cp == version || *cp != '.' || major < 0
+        || INT_MULTIPLY_WRAPV (major, 10000, &res))
+      return -1;
+  }
+
+  {
+    ++cp;
+    char *prev = cp;
+    long minor = strtol (cp, &cp, 10);
+    if (errno || cp == prev || (*cp != '\0' && *cp != '.')
+        || ! (0 <= minor && minor < 100)
+        || INT_MULTIPLY_WRAPV (minor, 100, &minor)
+        || INT_ADD_WRAPV (minor, res, &res))
+      return -1;
+  }
+
+  if (*cp == '.')
+    {
+      ++cp;
+      char *prev = cp;
+      long micro = strtol (cp, &cp, 10);
+      if (errno || cp == prev || (*cp != '\0' && *cp != '.')
+          || ! (0 <= micro && micro < 100)
+          || INT_ADD_WRAPV (micro, res, &res))
+        return -1;
+    }
+
+  IGNORE_TYPE_LIMITS_END
+  return res;
+}
diff --git a/src/strversion.h b/src/strversion.h
new file mode 100644
index 00000000..6d8d6235
--- /dev/null
+++ b/src/strversion.h
@@ -0,0 +1,28 @@
+/* Convert version string to int.
+
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This file is part of Bison, the GNU Compiler Compiler.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef STRVERSION_H_
+# define STRVERSION_H_
+
+/* Convert VERSION into an int (MAJOR * 10000 + MINOR * 100 + MICRO).
+   E.g., "3.7.4" => 30704, "3.8" => 30800.
+   Return -1 on errors. */
+int strversion_to_int (char const *version);
+
+#endif
-- 
2.29.2
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.