mirror of
https://github.com/systemd/systemd
synced 2024-09-30 05:15:19 +00:00
basic/utf8: modernize ascii_is_valid_n, make ascii_is_valid static inline
This commit is contained in:
parent
7ff7161044
commit
4d06bf5922
|
@ -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;
|
||||
|
|
|
@ -18,8 +18,11 @@ char* utf8_is_valid_n(const char *str, size_t len_bytes) _pure_;
|
|||
static inline char* utf8_is_valid(const char *str) {
|
||||
return utf8_is_valid_n(str, SIZE_MAX);
|
||||
}
|
||||
char *ascii_is_valid(const char *s) _pure_;
|
||||
char *ascii_is_valid_n(const char *str, size_t len);
|
||||
|
||||
char* ascii_is_valid_n(const char *str, size_t len) _pure_;
|
||||
static inline char* ascii_is_valid(const char *str) {
|
||||
return ascii_is_valid_n(str, SIZE_MAX);
|
||||
}
|
||||
|
||||
int utf8_to_ascii(const char *str, char replacement_char, char **ret);
|
||||
|
||||
|
|
Loading…
Reference in a new issue