summaryrefslogtreecommitdiffstats
path: root/src/basic/utf8.c
diff options
context:
space:
mode:
authorMike Yuan <me@yhndnzj.com>2024-05-09 15:58:36 +0200
committerMike Yuan <me@yhndnzj.com>2024-06-16 19:07:35 +0200
commit4d06bf59229ed766ba8fdd83c2712701291277f4 (patch)
tree5c3826609046808235d5135862a91043f1310fb9 /src/basic/utf8.c
parentbasic/utf8: modernize utf8_is_valid_n a bit (diff)
downloadsystemd-4d06bf59229ed766ba8fdd83c2712701291277f4.tar.xz
systemd-4d06bf59229ed766ba8fdd83c2712701291277f4.zip
basic/utf8: modernize ascii_is_valid_n, make ascii_is_valid static inline
Diffstat (limited to 'src/basic/utf8.c')
-rw-r--r--src/basic/utf8.c23
1 files changed, 5 insertions, 18 deletions
diff --git a/src/basic/utf8.c b/src/basic/utf8.c
index fd1e96c68c..4d1b31f26d 100644
--- a/src/basic/utf8.c
+++ b/src/basic/utf8.c
@@ -271,27 +271,14 @@ char *utf8_escape_non_printable_full(const char *str, size_t console_width, bool
return str_realloc(p);
}
-char *ascii_is_valid(const char *str) {
- /* Check whether the string consists of valid ASCII bytes,
- * i.e values between 0 and 127, inclusive. */
+char* ascii_is_valid_n(const char *str, size_t len) {
+ /* Check whether the string consists of valid ASCII bytes, i.e values between 1 and 127, inclusive.
+ * Stops at len, or NUL byte if len is SIZE_MAX. */
assert(str);
- for (const char *p = str; *p; p++)
- if ((unsigned char) *p >= 128)
- return NULL;
-
- return (char*) str;
-}
-
-char *ascii_is_valid_n(const char *str, size_t len) {
- /* Very similar to ascii_is_valid(), but checks exactly len
- * bytes and rejects any NULs in that range. */
-
- assert(str);
-
- for (size_t i = 0; i < len; i++)
- if ((unsigned char) str[i] >= 128 || str[i] == 0)
+ for (size_t i = 0; len != SIZE_MAX ? i < len : str[i] != '\0'; i++)
+ if ((unsigned char) str[i] >= 128 || str[i] == '\0')
return NULL;
return (char*) str;