[PATCH] common: move simple_strto{,u}l{,l}() to lib/

Jan Beulich posted 1 patch 9 months ago
Failed in applying to current master (apply log)
[PATCH] common: move simple_strto{,u}l{,l}() to lib/
Posted by Jan Beulich 9 months ago
Convert style from a Xen/Linux mix to pure Xen while doing the move. No
other changes, despite having been heavily tempted to do some - at the
very least to make simple_strtoul() and simple_strtoull() the same in
how they deal with non-numeric digits.

Requested-by: Shawn Anastasio <sanastasio@raptorengineering.com>
Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
Further changes I was considering:
- "value" doesn't need to be unsigned long, and even less so unsigned
  long long,
- strtoull.c could simply include strtoul.c, after #define-ing "long"
  to "long long" and "simple_strtoul" to "simple_strtoull",
- "else if ( base == 16 )" wants folding with its inner if().

--- a/xen/common/vsprintf.c
+++ b/xen/common/vsprintf.c
@@ -24,108 +24,6 @@
 #include <asm/div64.h>
 #include <asm/page.h>
 
-/**
- * simple_strtoul - convert a string to an unsigned long
- * @cp: The start of the string
- * @endp: A pointer to the end of the parsed string will be placed here
- * @base: The number base to use
- */
-unsigned long simple_strtoul(
-    const char *cp, const char **endp, unsigned int base)
-{
-    unsigned long result = 0,value;
-
-    if (!base) {
-        base = 10;
-        if (*cp == '0') {
-            base = 8;
-            cp++;
-            if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
-                cp++;
-                base = 16;
-            }
-        }
-    } else if (base == 16) {
-        if (cp[0] == '0' && toupper(cp[1]) == 'X')
-            cp += 2;
-    }
-    while (isxdigit(*cp) &&
-           (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
-        result = result*base + value;
-        cp++;
-    }
-    if (endp)
-        *endp = cp;
-    return result;
-}
-
-EXPORT_SYMBOL(simple_strtoul);
-
-/**
- * simple_strtol - convert a string to a signed long
- * @cp: The start of the string
- * @endp: A pointer to the end of the parsed string will be placed here
- * @base: The number base to use
- */
-long simple_strtol(const char *cp, const char **endp, unsigned int base)
-{
-    if(*cp=='-')
-        return -simple_strtoul(cp+1,endp,base);
-    return simple_strtoul(cp,endp,base);
-}
-
-EXPORT_SYMBOL(simple_strtol);
-
-/**
- * simple_strtoull - convert a string to an unsigned long long
- * @cp: The start of the string
- * @endp: A pointer to the end of the parsed string will be placed here
- * @base: The number base to use
- */
-unsigned long long simple_strtoull(
-    const char *cp, const char **endp, unsigned int base)
-{
-    unsigned long long result = 0,value;
-
-    if (!base) {
-        base = 10;
-        if (*cp == '0') {
-            base = 8;
-            cp++;
-            if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
-                cp++;
-                base = 16;
-            }
-        }
-    } else if (base == 16) {
-        if (cp[0] == '0' && toupper(cp[1]) == 'X')
-            cp += 2;
-    }
-    while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
-                                                               ? toupper(*cp) : *cp)-'A'+10) < base) {
-        result = result*base + value;
-        cp++;
-    }
-    if (endp)
-        *endp = cp;
-    return result;
-}
-
-EXPORT_SYMBOL(simple_strtoull);
-
-/**
- * simple_strtoll - convert a string to a signed long long
- * @cp: The start of the string
- * @endp: A pointer to the end of the parsed string will be placed here
- * @base: The number base to use
- */
-long long simple_strtoll(const char *cp,const char **endp,unsigned int base)
-{
-    if(*cp=='-')
-        return -simple_strtoull(cp+1,endp,base);
-    return simple_strtoull(cp,endp,base);
-}
-
 static int skip_atoi(const char **s)
 {
     int i=0;
--- a/xen/lib/Makefile
+++ b/xen/lib/Makefile
@@ -28,6 +28,10 @@ lib-y += strrchr.o
 lib-y += strsep.o
 lib-y += strspn.o
 lib-y += strstr.o
+lib-y += strtol.o
+lib-y += strtoll.o
+lib-y += strtoul.o
+lib-y += strtoull.o
 lib-$(CONFIG_X86) += xxhash32.o
 lib-$(CONFIG_X86) += xxhash64.o
 
--- /dev/null
+++ b/xen/lib/strtol.c
@@ -0,0 +1,28 @@
+/*
+ *  Copyright (C) 1991, 1992  Linus Torvalds
+ */
+
+#include <xen/lib.h>
+
+/**
+ * simple_strtol - convert a string to a signed long
+ * @cp: The start of the string
+ * @endp: A pointer to the end of the parsed string will be placed here
+ * @base: The number base to use
+ */
+long simple_strtol(const char *cp, const char **endp, unsigned int base)
+{
+    if ( *cp == '-' )
+        return -simple_strtoul(cp + 1, endp, base);
+    return simple_strtoul(cp, endp, base);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
--- /dev/null
+++ b/xen/lib/strtoll.c
@@ -0,0 +1,28 @@
+/*
+ *  Copyright (C) 1991, 1992  Linus Torvalds
+ */
+
+#include <xen/lib.h>
+
+/**
+ * simple_strtoll - convert a string to a signed long long
+ * @cp: The start of the string
+ * @endp: A pointer to the end of the parsed string will be placed here
+ * @base: The number base to use
+ */
+long long simple_strtoll(const char *cp, const char **endp, unsigned int base)
+{
+    if ( *cp == '-' )
+        return -simple_strtoull(cp + 1, endp, base);
+    return simple_strtoull(cp, endp, base);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
--- /dev/null
+++ b/xen/lib/strtoul.c
@@ -0,0 +1,61 @@
+/*
+ *  Copyright (C) 1991, 1992  Linus Torvalds
+ */
+
+#include <xen/ctype.h>
+#include <xen/lib.h>
+
+/**
+ * simple_strtoul - convert a string to an unsigned long
+ * @cp: The start of the string
+ * @endp: A pointer to the end of the parsed string will be placed here
+ * @base: The number base to use
+ */
+unsigned long simple_strtoul(
+    const char *cp, const char **endp, unsigned int base)
+{
+    unsigned long result = 0, value;
+
+    if ( !base )
+    {
+        base = 10;
+        if ( *cp == '0' )
+        {
+            base = 8;
+            cp++;
+            if ( (toupper(*cp) == 'X') && isxdigit(cp[1]) )
+            {
+                cp++;
+                base = 16;
+            }
+        }
+    }
+    else if ( base == 16 )
+    {
+        if ( cp[0] == '0' && toupper(cp[1]) == 'X' )
+            cp += 2;
+    }
+
+    while ( isxdigit(*cp) &&
+            (value = isdigit(*cp) ? *cp - '0'
+                                  : toupper(*cp) - 'A' + 10) < base )
+    {
+        result = result * base + value;
+        cp++;
+    }
+
+    if ( endp )
+        *endp = cp;
+
+    return result;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
--- /dev/null
+++ b/xen/lib/strtoull.c
@@ -0,0 +1,62 @@
+/*
+ *  Copyright (C) 1991, 1992  Linus Torvalds
+ */
+
+#include <xen/ctype.h>
+#include <xen/lib.h>
+
+/**
+ * simple_strtoull - convert a string to an unsigned long long
+ * @cp: The start of the string
+ * @endp: A pointer to the end of the parsed string will be placed here
+ * @base: The number base to use
+ */
+unsigned long long simple_strtoull(
+    const char *cp, const char **endp, unsigned int base)
+{
+    unsigned long long result = 0, value;
+
+    if ( !base )
+    {
+        base = 10;
+        if ( *cp == '0' )
+        {
+            base = 8;
+            cp++;
+            if ( (toupper(*cp) == 'X') && isxdigit(cp[1]) )
+            {
+                cp++;
+                base = 16;
+            }
+        }
+    }
+    else if ( base == 16 )
+    {
+        if ( cp[0] == '0' && toupper(cp[1]) == 'X' )
+            cp += 2;
+    }
+
+    while ( isxdigit(*cp) &&
+            (value = isdigit(*cp) ? *cp - '0'
+                                  : (islower(*cp) ? toupper(*cp)
+                                                  : *cp) - 'A' + 10) < base )
+    {
+        result = result * base + value;
+        cp++;
+    }
+
+    if ( endp )
+        *endp = cp;
+
+    return result;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Re: [PATCH] common: move simple_strto{,u}l{,l}() to lib/
Posted by Shawn Anastasio 9 months ago
On 8/1/23 5:34 AM, Jan Beulich wrote:
> Convert style from a Xen/Linux mix to pure Xen while doing the move. No
> other changes, despite having been heavily tempted to do some - at the
> very least to make simple_strtoul() and simple_strtoull() the same in
> how they deal with non-numeric digits.
> 
> Requested-by: Shawn Anastasio <sanastasio@raptorengineering.com>
> Signed-off-by: Jan Beulich <jbeulich@suse.com>
> ---
> Further changes I was considering:
> - "value" doesn't need to be unsigned long, and even less so unsigned
>   long long,
> - strtoull.c could simply include strtoul.c, after #define-ing "long"
>   to "long long" and "simple_strtoul" to "simple_strtoull",
> - "else if ( base == 16 )" wants folding with its inner if().
> 
> --- a/xen/common/vsprintf.c
> +++ b/xen/common/vsprintf.c
> @@ -24,108 +24,6 @@
>  #include <asm/div64.h>
>  #include <asm/page.h>
>  
> -/**
> - * simple_strtoul - convert a string to an unsigned long
> - * @cp: The start of the string
> - * @endp: A pointer to the end of the parsed string will be placed here
> - * @base: The number base to use
> - */
> -unsigned long simple_strtoul(
> -    const char *cp, const char **endp, unsigned int base)
> -{
> -    unsigned long result = 0,value;
> -
> -    if (!base) {
> -        base = 10;
> -        if (*cp == '0') {
> -            base = 8;
> -            cp++;
> -            if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
> -                cp++;
> -                base = 16;
> -            }
> -        }
> -    } else if (base == 16) {
> -        if (cp[0] == '0' && toupper(cp[1]) == 'X')
> -            cp += 2;
> -    }
> -    while (isxdigit(*cp) &&
> -           (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
> -        result = result*base + value;
> -        cp++;
> -    }
> -    if (endp)
> -        *endp = cp;
> -    return result;
> -}
> -
> -EXPORT_SYMBOL(simple_strtoul);
> -
> -/**
> - * simple_strtol - convert a string to a signed long
> - * @cp: The start of the string
> - * @endp: A pointer to the end of the parsed string will be placed here
> - * @base: The number base to use
> - */
> -long simple_strtol(const char *cp, const char **endp, unsigned int base)
> -{
> -    if(*cp=='-')
> -        return -simple_strtoul(cp+1,endp,base);
> -    return simple_strtoul(cp,endp,base);
> -}
> -
> -EXPORT_SYMBOL(simple_strtol);
> -
> -/**
> - * simple_strtoull - convert a string to an unsigned long long
> - * @cp: The start of the string
> - * @endp: A pointer to the end of the parsed string will be placed here
> - * @base: The number base to use
> - */
> -unsigned long long simple_strtoull(
> -    const char *cp, const char **endp, unsigned int base)
> -{
> -    unsigned long long result = 0,value;
> -
> -    if (!base) {
> -        base = 10;
> -        if (*cp == '0') {
> -            base = 8;
> -            cp++;
> -            if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
> -                cp++;
> -                base = 16;
> -            }
> -        }
> -    } else if (base == 16) {
> -        if (cp[0] == '0' && toupper(cp[1]) == 'X')
> -            cp += 2;
> -    }
> -    while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
> -                                                               ? toupper(*cp) : *cp)-'A'+10) < base) {
> -        result = result*base + value;
> -        cp++;
> -    }
> -    if (endp)
> -        *endp = cp;
> -    return result;
> -}
> -
> -EXPORT_SYMBOL(simple_strtoull);
> -
> -/**
> - * simple_strtoll - convert a string to a signed long long
> - * @cp: The start of the string
> - * @endp: A pointer to the end of the parsed string will be placed here
> - * @base: The number base to use
> - */
> -long long simple_strtoll(const char *cp,const char **endp,unsigned int base)
> -{
> -    if(*cp=='-')
> -        return -simple_strtoull(cp+1,endp,base);
> -    return simple_strtoull(cp,endp,base);
> -}
> -
>  static int skip_atoi(const char **s)
>  {
>      int i=0;
> --- a/xen/lib/Makefile
> +++ b/xen/lib/Makefile
> @@ -28,6 +28,10 @@ lib-y += strrchr.o
>  lib-y += strsep.o
>  lib-y += strspn.o
>  lib-y += strstr.o
> +lib-y += strtol.o
> +lib-y += strtoll.o
> +lib-y += strtoul.o
> +lib-y += strtoull.o
>  lib-$(CONFIG_X86) += xxhash32.o
>  lib-$(CONFIG_X86) += xxhash64.o
>  
> --- /dev/null
> +++ b/xen/lib/strtol.c
> @@ -0,0 +1,28 @@
> +/*
> + *  Copyright (C) 1991, 1992  Linus Torvalds
> + */
> +
> +#include <xen/lib.h>
> +
> +/**
> + * simple_strtol - convert a string to a signed long
> + * @cp: The start of the string
> + * @endp: A pointer to the end of the parsed string will be placed here
> + * @base: The number base to use
> + */
> +long simple_strtol(const char *cp, const char **endp, unsigned int base)
> +{
> +    if ( *cp == '-' )
> +        return -simple_strtoul(cp + 1, endp, base);
> +    return simple_strtoul(cp, endp, base);
> +}
> +
> +/*
> + * Local variables:
> + * mode: C
> + * c-file-style: "BSD"
> + * c-basic-offset: 4
> + * tab-width: 4
> + * indent-tabs-mode: nil
> + * End:
> + */
> --- /dev/null
> +++ b/xen/lib/strtoll.c
> @@ -0,0 +1,28 @@
> +/*
> + *  Copyright (C) 1991, 1992  Linus Torvalds
> + */
> +
> +#include <xen/lib.h>
> +
> +/**
> + * simple_strtoll - convert a string to a signed long long
> + * @cp: The start of the string
> + * @endp: A pointer to the end of the parsed string will be placed here
> + * @base: The number base to use
> + */
> +long long simple_strtoll(const char *cp, const char **endp, unsigned int base)
> +{
> +    if ( *cp == '-' )
> +        return -simple_strtoull(cp + 1, endp, base);
> +    return simple_strtoull(cp, endp, base);
> +}
> +
> +/*
> + * Local variables:
> + * mode: C
> + * c-file-style: "BSD"
> + * c-basic-offset: 4
> + * tab-width: 4
> + * indent-tabs-mode: nil
> + * End:
> + */
> --- /dev/null
> +++ b/xen/lib/strtoul.c
> @@ -0,0 +1,61 @@
> +/*
> + *  Copyright (C) 1991, 1992  Linus Torvalds
> + */
> +
> +#include <xen/ctype.h>
> +#include <xen/lib.h>
> +
> +/**
> + * simple_strtoul - convert a string to an unsigned long
> + * @cp: The start of the string
> + * @endp: A pointer to the end of the parsed string will be placed here
> + * @base: The number base to use
> + */
> +unsigned long simple_strtoul(
> +    const char *cp, const char **endp, unsigned int base)
> +{
> +    unsigned long result = 0, value;
> +
> +    if ( !base )
> +    {
> +        base = 10;
> +        if ( *cp == '0' )
> +        {
> +            base = 8;
> +            cp++;
> +            if ( (toupper(*cp) == 'X') && isxdigit(cp[1]) )
> +            {
> +                cp++;
> +                base = 16;
> +            }
> +        }
> +    }
> +    else if ( base == 16 )
> +    {
> +        if ( cp[0] == '0' && toupper(cp[1]) == 'X' )
> +            cp += 2;
> +    }
> +
> +    while ( isxdigit(*cp) &&
> +            (value = isdigit(*cp) ? *cp - '0'
> +                                  : toupper(*cp) - 'A' + 10) < base )
> +    {
> +        result = result * base + value;
> +        cp++;
> +    }
> +
> +    if ( endp )
> +        *endp = cp;
> +
> +    return result;
> +}
> +
> +/*
> + * Local variables:
> + * mode: C
> + * c-file-style: "BSD"
> + * c-basic-offset: 4
> + * tab-width: 4
> + * indent-tabs-mode: nil
> + * End:
> + */
> --- /dev/null
> +++ b/xen/lib/strtoull.c
> @@ -0,0 +1,62 @@
> +/*
> + *  Copyright (C) 1991, 1992  Linus Torvalds
> + */
> +
> +#include <xen/ctype.h>
> +#include <xen/lib.h>
> +
> +/**
> + * simple_strtoull - convert a string to an unsigned long long
> + * @cp: The start of the string
> + * @endp: A pointer to the end of the parsed string will be placed here
> + * @base: The number base to use
> + */
> +unsigned long long simple_strtoull(
> +    const char *cp, const char **endp, unsigned int base)
> +{
> +    unsigned long long result = 0, value;
> +
> +    if ( !base )
> +    {
> +        base = 10;
> +        if ( *cp == '0' )
> +        {
> +            base = 8;
> +            cp++;
> +            if ( (toupper(*cp) == 'X') && isxdigit(cp[1]) )
> +            {
> +                cp++;
> +                base = 16;
> +            }
> +        }
> +    }
> +    else if ( base == 16 )
> +    {
> +        if ( cp[0] == '0' && toupper(cp[1]) == 'X' )
> +            cp += 2;
> +    }
> +
> +    while ( isxdigit(*cp) &&
> +            (value = isdigit(*cp) ? *cp - '0'
> +                                  : (islower(*cp) ? toupper(*cp)
> +                                                  : *cp) - 'A' + 10) < base )
> +    {
> +        result = result * base + value;
> +        cp++;
> +    }
> +
> +    if ( endp )
> +        *endp = cp;
> +
> +    return result;
> +}
> +
> +/*
> + * Local variables:
> + * mode: C
> + * c-file-style: "BSD"
> + * c-basic-offset: 4
> + * tab-width: 4
> + * indent-tabs-mode: nil
> + * End:
> + */

Reviewed-by: Shawn Anastasio <sanastasio@raptorengineering.com>