filter_sdh: optimize get_char_bytes

strlen is only relevant if the length is less than [1, 4], so this can
be replaced with strnlen instead which will only traverse characters
upto the maxlen insted of the entire string length. It also makes MPMIN
unneeded. Also fix a comment.
This commit is contained in:
Dudemanguy 2024-01-13 12:25:12 -06:00
parent 13ed292ab0
commit 9bf4b9d5d4
1 changed files with 5 additions and 5 deletions

View File

@ -67,18 +67,18 @@ static inline int append(struct sd_filter *sd, struct buffer *buf, char c)
static int get_char_bytes(char *str)
{
// In case the final character is non-ASCII.
// In case the first character is non-ASCII.
// Will only work with UTF-8 but you shouldn't be
// using anything else anyway.
if (str && str[0]) {
if (!(str[0] >> 7 & 1)) {
return MPMIN(strlen(str), 1);
return strnlen(str, 1);
} else if (!(str[0] >> 5 & 1)) {
return MPMIN(strlen(str), 2);
return strnlen(str, 2);
} else if (!(str[0] >> 4 & 1)) {
return MPMIN(strlen(str), 3);
return strnlen(str, 3);
} else if (!(str[0] >> 3 & 1)) {
return MPMIN(strlen(str), 4);
return strnlen(str, 4);
}
}
return 0;