diff mbox series

[kvm-unit-tests,2/2] lib: Add ctype.h and collect is* functions

Message ID 20220519170724.580956-3-drjones@redhat.com (mailing list archive)
State New, archived
Headers show
Series lib: Cleanups | expand

Commit Message

Andrew Jones May 19, 2022, 5:07 p.m. UTC
We've been slowly adding ctype functions to different files without
even exporting them. Let's change that.

Signed-off-by: Andrew Jones <drjones@redhat.com>
---
 lib/argv.c   |  7 ++-----
 lib/ctype.h  | 40 ++++++++++++++++++++++++++++++++++++++++
 lib/string.c |  6 +-----
 3 files changed, 43 insertions(+), 10 deletions(-)
 create mode 100644 lib/ctype.h

Comments

Nikos Nikoleris May 20, 2022, 9:10 a.m. UTC | #1
On 19/05/2022 18:07, Andrew Jones wrote:
> We've been slowly adding ctype functions to different files without
> even exporting them. Let's change that.
>
> Signed-off-by: Andrew Jones <drjones@redhat.com>
> ---
>   lib/argv.c   |  7 ++-----
>   lib/ctype.h  | 40 ++++++++++++++++++++++++++++++++++++++++
>   lib/string.c |  6 +-----
>   3 files changed, 43 insertions(+), 10 deletions(-)
>   create mode 100644 lib/ctype.h
>
> diff --git a/lib/argv.c b/lib/argv.c
> index 0312d74011d3..951b176ae8b1 100644
> --- a/lib/argv.c
> +++ b/lib/argv.c
> @@ -6,6 +6,7 @@
>    */
>
>   #include "libcflat.h"
> +#include "ctype.h"
>   #include "argv.h"
>   #include "auxinfo.h"
>
> @@ -19,10 +20,6 @@ char **environ = __environ;
>   static char args_copy[1000];
>   static char *copy_ptr = args_copy;
>
> -#define isblank(c) ((c) == ' ' || (c) == '\t')
> -#define isalpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z') || (c) == '_')
> -#define isalnum(c) (isalpha(c) || ((c) >= '0' && (c) <= '9'))
> -
>   static const char *skip_blanks(const char *p)
>   {
>       while (isblank(*p))
> @@ -92,7 +89,7 @@ static char *env_next(char *env)
>       if (!*env)
>               return env;
>
> -     if (isalpha(*env)) {
> +     if (isalpha(*env) || *env == '_') {
>               bool invalid = false;
>
>               p = env + 1;
> diff --git a/lib/ctype.h b/lib/ctype.h
> new file mode 100644
> index 000000000000..ce787a60cdf3
> --- /dev/null
> +++ b/lib/ctype.h
> @@ -0,0 +1,40 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +#ifndef _CTYPE_H_
> +#define _CTYPE_H_
> +
> +static inline int isblank(int c)
> +{
> +     return c == ' ' || c == '\t';
> +}
> +
> +static inline int islower(int c)
> +{
> +     return c >= 'a' && c <= 'z';
> +}
> +
> +static inline int isupper(int c)
> +{
> +     return c >= 'A' && c <= 'Z';
> +}
> +
> +static inline int isalpha(int c)

minor nit: I think there is a trailing whitespace in the line above,
otherwise:

Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>

Thanks,

Nikos

> +{
> +     return isupper(c) || islower(c);
> +}
> +
> +static inline int isdigit(int c)
> +{
> +     return c >= '0' && c <= '9';
> +}
> +
> +static inline int isalnum(int c)
> +{
> +     return isalpha(c) || isdigit(c);
> +}
> +
> +static inline int isspace(int c)
> +{
> +        return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\f';
> +}
> +
> +#endif /* _CTYPE_H_ */
> diff --git a/lib/string.c b/lib/string.c
> index a3a8f3b1ce0b..6d8a6380db92 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -6,6 +6,7 @@
>    */
>
>   #include "libcflat.h"
> +#include "ctype.h"
>   #include "stdlib.h"
>   #include "linux/compiler.h"
>
> @@ -163,11 +164,6 @@ void *memchr(const void *s, int c, size_t n)
>       return NULL;
>   }
>
> -static int isspace(int c)
> -{
> -     return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\f';
> -}
> -
>   static unsigned long long __strtoll(const char *nptr, char **endptr,
>                                   int base, bool is_signed, bool is_longlong)
>   {
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Andrew Jones May 20, 2022, 1:12 p.m. UTC | #2
On Fri, May 20, 2022 at 10:10:14AM +0100, Nikos Nikoleris wrote:
> > +
> > +static inline int isalpha(int c)
> 
> minor nit: I think there is a trailing whitespace in the line above,

You're right and I've fixed it now.

> otherwise:
> 
> Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>

Thanks,
drew
Andrew Jones May 20, 2022, 1:17 p.m. UTC | #3
On Thu, May 19, 2022 at 07:07:24PM +0200, Andrew Jones wrote:
> We've been slowly adding ctype functions to different files without
> even exporting them. Let's change that.
> 
> Signed-off-by: Andrew Jones <drjones@redhat.com>

I need to send a v2 of this series, because besides the trailing
space Nikos points out, I just noticed that I forgot a check
for '_' in argv.c.

Thanks,
drew
diff mbox series

Patch

diff --git a/lib/argv.c b/lib/argv.c
index 0312d74011d3..951b176ae8b1 100644
--- a/lib/argv.c
+++ b/lib/argv.c
@@ -6,6 +6,7 @@ 
  */
 
 #include "libcflat.h"
+#include "ctype.h"
 #include "argv.h"
 #include "auxinfo.h"
 
@@ -19,10 +20,6 @@  char **environ = __environ;
 static char args_copy[1000];
 static char *copy_ptr = args_copy;
 
-#define isblank(c) ((c) == ' ' || (c) == '\t')
-#define isalpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z') || (c) == '_')
-#define isalnum(c) (isalpha(c) || ((c) >= '0' && (c) <= '9'))
-
 static const char *skip_blanks(const char *p)
 {
 	while (isblank(*p))
@@ -92,7 +89,7 @@  static char *env_next(char *env)
 	if (!*env)
 		return env;
 
-	if (isalpha(*env)) {
+	if (isalpha(*env) || *env == '_') {
 		bool invalid = false;
 
 		p = env + 1;
diff --git a/lib/ctype.h b/lib/ctype.h
new file mode 100644
index 000000000000..ce787a60cdf3
--- /dev/null
+++ b/lib/ctype.h
@@ -0,0 +1,40 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _CTYPE_H_
+#define _CTYPE_H_
+
+static inline int isblank(int c)
+{
+	return c == ' ' || c == '\t';
+}
+
+static inline int islower(int c)
+{
+	return c >= 'a' && c <= 'z';
+}
+
+static inline int isupper(int c)
+{
+	return c >= 'A' && c <= 'Z';
+}
+
+static inline int isalpha(int c) 
+{
+	return isupper(c) || islower(c);
+}
+
+static inline int isdigit(int c)
+{
+	return c >= '0' && c <= '9';
+}
+
+static inline int isalnum(int c)
+{
+	return isalpha(c) || isdigit(c);
+}
+
+static inline int isspace(int c)
+{
+        return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\f';
+}
+
+#endif /* _CTYPE_H_ */
diff --git a/lib/string.c b/lib/string.c
index a3a8f3b1ce0b..6d8a6380db92 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -6,6 +6,7 @@ 
  */
 
 #include "libcflat.h"
+#include "ctype.h"
 #include "stdlib.h"
 #include "linux/compiler.h"
 
@@ -163,11 +164,6 @@  void *memchr(const void *s, int c, size_t n)
 	return NULL;
 }
 
-static int isspace(int c)
-{
-	return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\f';
-}
-
 static unsigned long long __strtoll(const char *nptr, char **endptr,
 				    int base, bool is_signed, bool is_longlong)
 {