Skip to content

Commit 554e412

Browse files
lib/string/ctype/strisascii.*: Don't special-case ""
It is not intuitive or clear what the right behavior should be for an empty string. If we define these APIs as "return true if all characters in the string belong to the specified character set", then an empty string should return true. On the other hand, if you ask me if an empty string is a numeric string, I might naively say no. It is irrelevant whether we return true or false for an empty string. All of the callers already handle correctly the case of an empty string. This makes the implementation simpler, using the argument only once. This allows implementing these as macros. Signed-off-by: Alejandro Colomar <alx@kernel.org>
1 parent 7c37e88 commit 554e412

File tree

3 files changed

+4
-24
lines changed

3 files changed

+4
-24
lines changed

lib/string/README

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ ctype/ - Character classification and conversion functions
8585
The functions defined in this file
8686
return true
8787
if all of the characters of the string
88-
belong to the category specified in the function name
89-
and the string is not an empty string.
88+
belong to the category specified in the function name.
9089

9190
strtoascii.h
9291
The functions defined in this file

lib/string/ctype/strisascii.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,3 @@
55
#include "config.h"
66

77
#include "string/ctype/strisascii.h"
8-
9-
#include <stdbool.h>
10-
11-
12-
extern inline bool strisdigit_c(const char *s);
13-
extern inline bool strisprint_c(const char *s);

lib/string/ctype/strisascii.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,14 @@
88

99
#include "config.h"
1010

11-
#include <stdbool.h>
12-
1311
#include "string/ctype/isascii.h"
1412
#include "string/strcmp/streq.h"
1513
#include "string/strspn/stpspn.h"
1614

1715

18-
inline bool strisdigit_c(const char *s); // strisdigit - string is [:digit:] C-locale
19-
inline bool strisprint_c(const char *s); // strisprint - string is [:print:] C-locale
20-
21-
22-
inline bool
23-
strisdigit_c(const char *s)
24-
{
25-
return !streq(s, "") && streq(stpspn(s, CTYPE_DIGIT_C), "");
26-
}
27-
inline bool
28-
strisprint_c(const char *s)
29-
{
30-
return !streq(s, "") && streq(stpspn(s, CTYPE_PRINT_C), "");
31-
}
16+
// strisascii_c - string is [:ascii:] C-locale
17+
#define strisdigit_c(s) streq(stpspn(s, CTYPE_DIGIT_C), "")
18+
#define strisprint_c(s) streq(stpspn(s, CTYPE_PRINT_C), "")
3219

3320

3421
#endif // include guard

0 commit comments

Comments
 (0)