mirror of
https://github.com/systemd/systemd
synced 2024-10-01 13:55:20 +00:00
basic/utf8: modernize utf8_is_valid_n a bit
This commit is contained in:
parent
d4d90ef900
commit
7ff7161044
|
@ -136,18 +136,18 @@ char *utf8_is_valid_n(const char *str, size_t len_bytes) {
|
|||
|
||||
assert(str);
|
||||
|
||||
for (const char *p = str; len_bytes != SIZE_MAX ? (size_t) (p - str) < len_bytes : *p != '\0'; ) {
|
||||
for (size_t i = 0; len_bytes != SIZE_MAX ? i < len_bytes : str[i] != '\0'; ) {
|
||||
int len;
|
||||
|
||||
if (_unlikely_(*p == '\0') && len_bytes != SIZE_MAX)
|
||||
if (_unlikely_(str[i] == '\0'))
|
||||
return NULL; /* embedded NUL */
|
||||
|
||||
len = utf8_encoded_valid_unichar(p,
|
||||
len_bytes != SIZE_MAX ? len_bytes - (p - str) : SIZE_MAX);
|
||||
len = utf8_encoded_valid_unichar(str + i,
|
||||
len_bytes != SIZE_MAX ? len_bytes - i : SIZE_MAX);
|
||||
if (_unlikely_(len < 0))
|
||||
return NULL; /* invalid character */
|
||||
|
||||
p += len;
|
||||
i += len;
|
||||
}
|
||||
|
||||
return (char*) str;
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
bool unichar_is_valid(char32_t c);
|
||||
|
||||
char* utf8_is_valid_n(const char *str, size_t len_bytes) _pure_;
|
||||
static inline char *utf8_is_valid(const char *s) {
|
||||
return utf8_is_valid_n(s, SIZE_MAX);
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue