* lib/mbs_endswith.c (mbs_endswith): * lib/mbs_startswith.c (mbs_startswith): If GNULIB_MCEL_PREFER, prefer the mcel.h API and semantics. --- ChangeLog | 5 +++++ lib/mbs_endswith.c | 15 +++++++++++++-- lib/mbs_startswith.c | 24 ++++++++++++++++++++---- 3 files changed, 38 insertions(+), 6 deletions(-)
diff --git a/ChangeLog b/ChangeLog index 572f164866..c0387da2a7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2026-04-01 Paul Eggert <[email protected]> + mbs_endswith, mbs_startswith: port to mcel-prefer + * lib/mbs_endswith.c (mbs_endswith): + * lib/mbs_startswith.c (mbs_startswith): + If GNULIB_MCEL_PREFER, prefer the mcel.h API and semantics. + yesno: treat unlocking similarly if !ENABLE_NLS If the application is using unlocked-io, do that in the !ENABLE_NLS case too. This is more for consistency than for diff --git a/lib/mbs_endswith.c b/lib/mbs_endswith.c index 0a15239cb9..5494c0662f 100644 --- a/lib/mbs_endswith.c +++ b/lib/mbs_endswith.c @@ -21,7 +21,11 @@ /* Specification. */ #include <string.h> -#include "mbiter.h" +#if GNULIB_MCEL_PREFER +# include "mcel.h" +#else +# include "mbiter.h" +#endif bool mbs_endswith (const char *string, const char *suffix) @@ -54,6 +58,11 @@ mbs_endswith (const char *string, const char *suffix) size_t n = mbslen (suffix); if (len >= n) { +#if GNULIB_MCEL_PREFER + char const *stringlim = string + nbytes; + for (; len > n; len--) + string += mcel_scan (string, stringlim).len; +#else mbi_iterator_t iter; mbi_init (iter, string, nbytes); /* Advance past (len - n) multibyte characters. */ @@ -67,7 +76,9 @@ mbs_endswith (const char *string, const char *suffix) if (!mbi_avail (iter)) /* We can get here due to incomplete multibyte characters. */ return false; - return streq (mbi_cur_ptr (iter), suffix); + string = mbi_cur_ptr (iter); +#endif + return streq (string, suffix); } } return false; diff --git a/lib/mbs_startswith.c b/lib/mbs_startswith.c index b481f3d072..97e85ff8fb 100644 --- a/lib/mbs_startswith.c +++ b/lib/mbs_startswith.c @@ -21,7 +21,11 @@ /* Specification. */ #include <string.h> -#include "mbiter.h" +#if GNULIB_MCEL_PREFER +# include "mcel.h" +#else +# include "mbiter.h" +#endif #include <stdlib.h> @@ -33,15 +37,26 @@ mbs_startswith (const char *string, const char *prefix) if (str_startswith (string, prefix)) { /* It could be that PREFIX ends in an incomplete character and STRING - continues with a valid character or a longer incomplete character - instead. In this case, mbs_startswith needs to return false. */ + continues with a valid character or (if !GNULIB_MCEL_PREFER) + a longer incomplete character instead. + In this case, mbs_startswith needs to return false. */ size_t mb_max = MB_CUR_MAX; if (mb_max == 1) /* In unibyte locales, there are no incomplete characters. */ return true; /* Determine where we can stop the comparison. */ size_t p_len = strlen (prefix); - size_t s_len = p_len + strnlen (string + p_len, mb_max); + char const *sp_lim = string + p_len; + size_t s_len = p_len + strnlen (sp_lim, mb_max); +#if GNULIB_MCEL_PREFER + char const *s_lim = string + s_len; + while (string < sp_lim) + { + string += mcel_scan (string, s_lim).len; + if (sp_lim < string) + return false; + } +#else mbi_iterator_t s_iter; mbi_init (s_iter, string, s_len); mbi_iterator_t p_iter; @@ -53,6 +68,7 @@ mbs_startswith (const char *string, const char *prefix) return false; mbi_advance (s_iter); } +#endif return true; } return false; -- 2.53.0
