summaryrefslogtreecommitdiff
path: root/src/fundamental
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2022-07-05 11:55:26 +0200
committerLennart Poettering <lennart@poettering.net>2022-07-05 14:25:07 +0200
commitff25d3385dfbf493c878c9e227df56db3dc10b6a (patch)
treea4302f4eb82e157abfa94c3045a65abeafdbb3af /src/fundamental
parent82c3a0b74c8decccf2e1e384e7ad02def4a07459 (diff)
downloadsystemd-ff25d3385dfbf493c878c9e227df56db3dc10b6a.tar.gz
tree-wide: add global ascii_isdigit() + ascii_isalpha()
We now have a local implementation in string-util-fundamental.c, but it's useful at a lot of other places, hence let's give it a more expressive name and share it across the tree. Follow-up for: 8d9156660d6958c8d63b1d44692968f1b5d33920
Diffstat (limited to 'src/fundamental')
-rw-r--r--src/fundamental/string-util-fundamental.c22
-rw-r--r--src/fundamental/string-util-fundamental.h10
2 files changed, 16 insertions, 16 deletions
diff --git a/src/fundamental/string-util-fundamental.c b/src/fundamental/string-util-fundamental.c
index 6f690b9c90..11701ebe52 100644
--- a/src/fundamental/string-util-fundamental.c
+++ b/src/fundamental/string-util-fundamental.c
@@ -77,18 +77,8 @@ sd_char* endswith_no_case(const sd_char *s, const sd_char *postfix) {
return (sd_char*) s + sl - pl;
}
-static bool is_digit(sd_char a) {
- /* Locale-independent version of isdigit(). */
- return a >= '0' && a <= '9';
-}
-
-static bool is_alpha(sd_char a) {
- /* Locale-independent version of isalpha(). */
- return (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z');
-}
-
static bool is_valid_version_char(sd_char a) {
- return is_digit(a) || is_alpha(a) || IN_SET(a, '~', '-', '^', '.');
+ return ascii_isdigit(a) || ascii_isalpha(a) || IN_SET(a, '~', '-', '^', '.');
}
int strverscmp_improved(const sd_char *a, const sd_char *b) {
@@ -186,12 +176,12 @@ int strverscmp_improved(const sd_char *a, const sd_char *b) {
b++;
}
- if (is_digit(*a) || is_digit(*b)) {
+ if (ascii_isdigit(*a) || ascii_isdigit(*b)) {
/* Find the leading numeric segments. One may be an empty string. So,
* numeric segments are always newer than alpha segments. */
- for (aa = a; is_digit(*aa); aa++)
+ for (aa = a; ascii_isdigit(*aa); aa++)
;
- for (bb = b; is_digit(*bb); bb++)
+ for (bb = b; ascii_isdigit(*bb); bb++)
;
/* Check if one of the strings was empty, but the other not. */
@@ -217,9 +207,9 @@ int strverscmp_improved(const sd_char *a, const sd_char *b) {
return r;
} else {
/* Find the leading non-numeric segments. */
- for (aa = a; is_alpha(*aa); aa++)
+ for (aa = a; ascii_isalpha(*aa); aa++)
;
- for (bb = b; is_alpha(*bb); bb++)
+ for (bb = b; ascii_isalpha(*bb); bb++)
;
/* Note that the segments are usually not NUL-terminated. */
diff --git a/src/fundamental/string-util-fundamental.h b/src/fundamental/string-util-fundamental.h
index 72fa0d7c90..ecf32e519f 100644
--- a/src/fundamental/string-util-fundamental.h
+++ b/src/fundamental/string-util-fundamental.h
@@ -104,3 +104,13 @@ static inline void *memory_startswith(const void *p, size_t sz, const sd_char *t
#define STRV_FOREACH(s, l) \
_STRV_FOREACH(s, l, UNIQ_T(i, UNIQ))
+
+static inline bool ascii_isdigit(sd_char a) {
+ /* A pure ASCII, locale independent version of isdigit() */
+ return a >= '0' && a <= '9';
+}
+
+static inline bool ascii_isalpha(sd_char a) {
+ /* A pure ASCII, locale independent version of isalpha() */
+ return (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z');
+}