diff mbox series

[XTF,2/4] libc, strtol: Add isspace(), isdigit(), isxdigit(), isascii()

Message ID 20200423101918.13566-3-wipawel@amazon.de (mailing list archive)
State New, archived
Headers show
Series Add strncmp(), strtol() and strtoul() functions | expand

Commit Message

Wieczorkiewicz, Pawel April 23, 2020, 10:19 a.m. UTC
These ctype.h derived helper functions simplify strtol() code and will
help simplify strtoul().

Signed-off-by: Pawel Wieczorkiewicz <wipawel@amazon.de>
---
 common/lib.c            |  8 --------
 common/libc/strtol.c    |  9 +++------
 common/libc/vsnprintf.c |  8 --------
 include/xtf/libc.h      | 29 +++++++++++++++++++++++++++++
 4 files changed, 32 insertions(+), 22 deletions(-)
diff mbox series

Patch

diff --git a/common/lib.c b/common/lib.c
index acf4da1..7381a3a 100644
--- a/common/lib.c
+++ b/common/lib.c
@@ -3,14 +3,6 @@ 
 #include <xtf/hypercall.h>
 #include <xtf/xenstore.h>
 
-#ifndef isdigit
-/* Avoid pulling in all of ctypes just for this. */
-static int isdigit(int c)
-{
-    return c >= '0' && c <= '9';
-}
-#endif
-
 void __noreturn panic(const char *fmt, ...)
 {
     va_list args;
diff --git a/common/libc/strtol.c b/common/libc/strtol.c
index 64ce621..f85ac7a 100644
--- a/common/libc/strtol.c
+++ b/common/libc/strtol.c
@@ -57,7 +57,7 @@  long strtol(const char *nptr, char **endptr, int base)
     s = nptr;
     do {
         c = *s++;
-    } while ( c == ' ' );
+    } while ( isspace(c) );
     if (c == '-') {
         neg = 1;
         c = *s++;
@@ -67,10 +67,7 @@  long strtol(const char *nptr, char **endptr, int base)
             c = *s++;
     }
     if ((base == 0 || base == 16) &&
-            c == '0' && (*s == 'x' || *s == 'X') &&
-            ((s[1] >= '0' && s[1] <= '9') ||
-             (s[1] >= 'A' && s[1] <= 'F') ||
-             (s[1] >= 'a' && s[1] <= 'f'))) {
+            c == '0' && (*s == 'x' || *s == 'X') && isxdigit(s[1])) {
         c = s[1];
         s += 2;
         base = 16;
@@ -104,7 +101,7 @@  long strtol(const char *nptr, char **endptr, int base)
     cutlim = cutoff % base;
     cutoff /= base;
     for ( ; ; c = *s++) {
-        if (c >= '0' && c <= '9')
+        if (isdigit(c))
             c -= '0';
         else if (c >= 'A' && c <= 'Z')
             c -= 'A' - 10;
diff --git a/common/libc/vsnprintf.c b/common/libc/vsnprintf.c
index a49fd30..495c0a5 100644
--- a/common/libc/vsnprintf.c
+++ b/common/libc/vsnprintf.c
@@ -2,14 +2,6 @@ 
 #include <xtf/libc.h>
 #include <xtf/compiler.h>
 
-#ifndef isdigit
-/* Avoid pulling in all of ctypes just for this. */
-static int isdigit(int c)
-{
-    return c >= '0' && c <= '9';
-}
-#endif
-
 /*
  * The subset of formatting supported:
  *
diff --git a/include/xtf/libc.h b/include/xtf/libc.h
index f352f7f..0caa7d3 100644
--- a/include/xtf/libc.h
+++ b/include/xtf/libc.h
@@ -56,6 +56,35 @@  bool arch_fmt_pointer(
     char **str, char *end, const char **fmt_ptr, const void *arg,
     int width, int precision, unsigned int flags);
 
+#ifndef isspace
+static inline int isspace(int c)
+{
+    return c == ' ' || c == '\t';
+}
+#endif
+
+#ifndef isdigit
+static inline int isdigit(int c)
+{
+    return c >= '0' && c <= '9';
+}
+#endif
+
+#ifndef isxdigit
+static inline int isxdigit(int c)
+{
+    return (isdigit(c) || (c >= 'A' && c <= 'F') ||
+           (c >= 'a' && c <= 'f'));
+}
+#endif
+
+#ifndef isascii
+static inline int isascii(int c)
+{
+    return c >= 0 && c <= 127;
+}
+#endif
+
 #endif /* XTF_LIBC_H */
 
 /*