From: Florian Forster Date: Fri, 22 Dec 2023 14:37:57 +0000 (+0100) Subject: utf8: Make `decode` idempotent by returning the new state. X-Git-Tag: 6.0.0-rc0~22^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fbf76c2d6ea7334acaca6a4f23279ced94e66155;p=thirdparty%2Fcollectd.git utf8: Make `decode` idempotent by returning the new state. --- diff --git a/src/utils/utf8/utf8.c b/src/utils/utf8/utf8.c index 68ce2f7fe..e00770cc0 100644 --- a/src/utils/utf8/utf8.c +++ b/src/utils/utf8/utf8.c @@ -46,17 +46,15 @@ static const uint8_t utf8d[] = { }; // clang-format on -static void decode(uint32_t *state, uint32_t byte) { +static uint32_t decode(uint32_t state, uint32_t byte) { uint32_t type = utf8d[byte]; - - *state = utf8d[256 + (*state) * 16 + type]; + return utf8d[256 + state * 16 + type]; } bool utf8_valid(char const *s) { uint32_t state = 0; - for (size_t i = 0; s[i] != 0; i++) { - decode(&state, (uint8_t)s[i]); + state = decode(state, (uint8_t)s[i]); } return state == UTF8_ACCEPT;