From: Bruno Haible Date: Tue, 18 Jul 2023 10:19:58 +0000 (+0200) Subject: mbsnlen: Optimize. X-Git-Tag: v1.0~1079 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=cb55e214f9dd4f1eed6f8e488b7d0b3bd7c8505f;p=gnulib.git mbsnlen: Optimize. * lib/mbsnlen.c: Include mbiterf.h instead of mbiter.h. (mbsnlen): Use mbif_* macros instead of mbi_* macros. * modules/mbsnlen (Depends-on): Add mbiterf. Remove mbiter. --- diff --git a/ChangeLog b/ChangeLog index 8581c23a95..a71e01cdbf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2023-07-18 Bruno Haible + mbsnlen: Optimize. + * lib/mbsnlen.c: Include mbiterf.h instead of mbiter.h. + (mbsnlen): Use mbif_* macros instead of mbi_* macros. + * modules/mbsnlen (Depends-on): Add mbiterf. Remove mbiter. + mbiterf: Add a benchmark. * tests/bench-mbiterf.c: New file, based on tests/bench-mbiter.c. * modules/mbiterf-bench-tests: New file, based on diff --git a/lib/mbsnlen.c b/lib/mbsnlen.c index 0a48ea93a0..ec1dfcaec3 100644 --- a/lib/mbsnlen.c +++ b/lib/mbsnlen.c @@ -22,7 +22,7 @@ #include -#include "mbiter.h" +#include "mbiterf.h" /* Return the number of multibyte characters in the character string starting at STRING and ending at STRING + LEN. */ @@ -31,12 +31,17 @@ mbsnlen (const char *string, size_t len) { if (MB_CUR_MAX > 1) { - size_t count; - mbi_iterator_t iter; - - count = 0; - for (mbi_init (iter, string, len); mbi_avail (iter); mbi_advance (iter)) - count++; + size_t count = 0; + + const char *string_end = string + len; + mbif_state_t state; + const char *iter; + for (mbif_init (state), iter = string; mbif_avail (state, iter, string_end); ) + { + mbchar_t cur = mbif_next (state, iter, string_end); + count++; + iter += mb_len (cur); + } return count; } diff --git a/modules/mbsnlen b/modules/mbsnlen index 307b6c34b2..3a3486f689 100644 --- a/modules/mbsnlen +++ b/modules/mbsnlen @@ -5,7 +5,7 @@ Files: lib/mbsnlen.c Depends-on: -mbiter +mbiterf string configure.ac: