]> Savannah Git Hosting - gnulib.git/commitdiff
mbsnlen: Optimize.
authorBruno Haible <bruno@clisp.org>
Tue, 18 Jul 2023 10:19:58 +0000 (12:19 +0200)
committerBruno Haible <bruno@clisp.org>
Tue, 18 Jul 2023 10:19:58 +0000 (12:19 +0200)
* 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.

ChangeLog
lib/mbsnlen.c
modules/mbsnlen

index 8581c23a957b8607b56b324d9f06aaa102d88933..a71e01cdbf6adf5484c74ee253bb17db5a6a5268 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2023-07-18  Bruno Haible  <bruno@clisp.org>
 
+       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
index 0a48ea93a020bcd3f2bf988c394f1ce378e34718..ec1dfcaec3e7aff46e4a867b06d0f918217c92d5 100644 (file)
@@ -22,7 +22,7 @@
 
 #include <stdlib.h>
 
-#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;
     }
index 307b6c34b2c2ea27a4509d71aa93be8d4c640f32..3a3486f6899876518ab2f13eedf4b585a21a0817 100644 (file)
@@ -5,7 +5,7 @@ Files:
 lib/mbsnlen.c
 
 Depends-on:
-mbiter
+mbiterf
 string
 
 configure.ac: