summaryrefslogtreecommitdiff
path: root/lib/mbsnlen.c
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff.email>2024-03-03 19:11:32 +0100
committerJörg Frings-Fürst <debian@jff.email>2024-03-03 19:11:32 +0100
commit00893e79fc62966067af1a106567db96bd170338 (patch)
tree52b35cd0b42ca28e62a2ffbecade2e13dd8332cf /lib/mbsnlen.c
parent26112352a774737e1ce5580c93654a26c1e82b39 (diff)
New upstream version 1.2upstream/1.2
Diffstat (limited to 'lib/mbsnlen.c')
-rw-r--r--lib/mbsnlen.c27
1 files changed, 21 insertions, 6 deletions
diff --git a/lib/mbsnlen.c b/lib/mbsnlen.c
index c6ea1fef..baadf163 100644
--- a/lib/mbsnlen.c
+++ b/lib/mbsnlen.c
@@ -1,5 +1,5 @@
/* Counting the multibyte characters in a string.
- Copyright (C) 2007-2022 Free Software Foundation, Inc.
+ Copyright (C) 2007-2024 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>, 2007.
This file is free software: you can redistribute it and/or modify
@@ -22,7 +22,11 @@
#include <stdlib.h>
-#include "mbiter.h"
+#if GNULIB_MCEL_PREFER
+# include "mcel.h"
+#else
+# include "mbiterf.h"
+#endif
/* Return the number of multibyte characters in the character string starting
at STRING and ending at STRING + LEN. */
@@ -31,12 +35,23 @@ mbsnlen (const char *string, size_t len)
{
if (MB_CUR_MAX > 1)
{
- size_t count;
- mbi_iterator_t iter;
+ size_t count = 0;
- count = 0;
- for (mbi_init (iter, string, len); mbi_avail (iter); mbi_advance (iter))
+ const char *string_end = string + len;
+
+#if GNULIB_MCEL_PREFER
+ for (; *string; string += mcel_scan (string, string_end).len)
count++;
+#else
+ 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);
+ }
+#endif
return count;
}