wmem: Make wmem_strbuf_utf8_validate endpptr param optional

Often we don't care about the last valid character, just if
the buffer is valid.
This commit is contained in:
John Thacker 2022-11-06 15:01:39 -05:00 committed by Guy Harris
parent 0928a25d1f
commit f4965d5dec
2 changed files with 20 additions and 5 deletions

View File

@ -419,17 +419,27 @@ static bool
string_utf8_validate(const char *str, ssize_t max_len, const char **endpptr)
{
bool valid;
const char *endp;
if (max_len <= 0)
if (max_len <= 0) {
if (endpptr) {
*endpptr = str;
}
return true;
}
valid = g_utf8_validate(str, max_len, endpptr);
if (valid || **endpptr != '\0')
valid = g_utf8_validate(str, max_len, &endp);
if (valid || *endp != '\0') {
if (endpptr) {
*endpptr = endp;
}
return valid;
}
/* Invalid because of a nul byte. Skip nuls and continue. */
max_len -= *endpptr - str;
str = *endpptr;
max_len -= endp - str;
str = endp;
while (max_len > 0 && *str == '\0') {
str++;
max_len--;

View File

@ -156,6 +156,11 @@ WS_DLL_PUBLIC
void
wmem_strbuf_destroy(wmem_strbuf_t *strbuf);
/* Validates the string buffer as UTF-8.
* Unlike g_utf8_validate(), accepts embedded NUL bytes as valid UTF-8.
* If endpptr is non-NULL, then the end of the valid range is stored there
* (i.e. the first invalid character, or the end of the buffer otherwise).
*/
WS_DLL_PUBLIC
bool
wmem_strbuf_utf8_validate(wmem_strbuf_t *strbuf, const char **endptr);