1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2026-10-24 00:48:56 +03:00

mbuiter: Implement multi-byte per encoding error (MEE) consistently.

* lib/mbuiter.h: Include mbiter-aux.h.
(struct mbuiter_multi): Add field is_utf8.
(mbuiter_multi_next): Invoke mbiter_is_utf8,
mbiter_utf8_maximal_subpart.
(mbui_init): Initialize the field is_utf8.
* modules/mbuiter (Depends-on): Add mbiter-aux.
* tests/test-mbsstr2.c (OR): New macro, copied from
tests/test-mbsnlen.c.
(main): Add test cases with incomplete characters.
* tests/test-mbsstr1.c: Update comments.
* tests/test-mbsstr3.c: Likewise.
* tests/test-mbscasestr2.c (OR): New macro, copied from
tests/test-mbsnlen.c.
(main): Add test cases with incomplete characters.
* tests/test-mbscasestr1.c: Update comments.
* tests/test-mbscasestr3.c: Likewise.
* tests/test-mbscasestr4.c: Likewise.
This commit is contained in:
Bruno Haible
2026-05-26 00:53:31 +02:00
parent 151d374a80
commit 8bfd5e3198
10 changed files with 204 additions and 13 deletions

View File

@@ -1,3 +1,24 @@
2026-05-25 Bruno Haible <bruno@clisp.org>
mbuiter: Implement multi-byte per encoding error (MEE) consistently.
* lib/mbuiter.h: Include mbiter-aux.h.
(struct mbuiter_multi): Add field is_utf8.
(mbuiter_multi_next): Invoke mbiter_is_utf8,
mbiter_utf8_maximal_subpart.
(mbui_init): Initialize the field is_utf8.
* modules/mbuiter (Depends-on): Add mbiter-aux.
* tests/test-mbsstr2.c (OR): New macro, copied from
tests/test-mbsnlen.c.
(main): Add test cases with incomplete characters.
* tests/test-mbsstr1.c: Update comments.
* tests/test-mbsstr3.c: Likewise.
* tests/test-mbscasestr2.c (OR): New macro, copied from
tests/test-mbsnlen.c.
(main): Add test cases with incomplete characters.
* tests/test-mbscasestr1.c: Update comments.
* tests/test-mbscasestr3.c: Likewise.
* tests/test-mbscasestr4.c: Likewise.
2026-05-25 Bruno Haible <bruno@clisp.org>
mbiterf: Implement multi-byte per encoding error (MEE) consistently.

View File

@@ -103,6 +103,7 @@
#include <wchar.h>
#include "mbchar.h"
#include "mbiter-aux.h"
#include "strnlen1.h"
_GL_INLINE_HEADER_BEGIN
@@ -128,6 +129,7 @@ struct mbuiter_multi
*/
bool next_done; /* true if mbui_avail has already filled the following */
unsigned int cur_max; /* A cache of MB_CUR_MAX. */
int is_utf8; /* A cache of mbiter_is_utf8. */
struct mbchar cur; /* the current character:
const char *cur.ptr pointer to current character
The following are only valid after mbui_avail.
@@ -164,15 +166,18 @@ mbuiter_multi_next (struct mbuiter_multi *iter)
assert (mbsinit (&iter->state));
#if !GNULIB_MBRTOC32_REGULAR
iter->in_shift = true;
with_shift:
with_shift:;
#endif
iter->cur.bytes = mbrtoc32 (&iter->cur.wc, iter->cur.ptr,
strnlen1 (iter->cur.ptr, iter->cur_max),
size_t avail_bytes = strnlen1 (iter->cur.ptr, iter->cur_max);
iter->cur.bytes = mbrtoc32 (&iter->cur.wc, iter->cur.ptr, avail_bytes,
&iter->state);
if (iter->cur.bytes == (size_t) -1)
{
/* An invalid multibyte sequence was encountered. */
iter->cur.bytes = 1;
iter->cur.bytes =
(mbiter_is_utf8 (&iter->is_utf8)
? mbiter_utf8_maximal_subpart (iter->cur.ptr, avail_bytes)
: 1);
iter->cur.wc_valid = false;
/* Allow the next invocation to continue from a sane state. */
#if !GNULIB_MBRTOC32_REGULAR
@@ -243,14 +248,14 @@ typedef struct mbuiter_multi mbui_iterator_t;
((iter).cur.ptr = (startptr), \
(iter).in_shift = false, mbszero (&(iter).state), \
(iter).next_done = false, \
(iter).cur_max = MB_CUR_MAX)
(iter).cur_max = MB_CUR_MAX, (iter).is_utf8 = -1)
#else
/* Optimized: no in_shift. */
#define mbui_init(iter, startptr) \
((iter).cur.ptr = (startptr), \
mbszero (&(iter).state), \
(iter).next_done = false, \
(iter).cur_max = MB_CUR_MAX)
(iter).cur_max = MB_CUR_MAX, (iter).is_utf8 = -1)
#endif
#define mbui_avail(iter) \
(mbuiter_multi_next (&(iter)), !mb_isnul ((iter).cur))

View File

@@ -13,6 +13,7 @@ mbchar
mbrtoc32
mbsinit
mbszero
mbiter-aux
uchar-h
bool
strnlen1

View File

@@ -1,4 +1,4 @@
/* Test of case-insensitive searching in a string.
/* Test of case-insensitive searching in a string in the "C" locale.
Copyright (C) 2007-2026 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify

View File

@@ -1,4 +1,4 @@
/* Test of searching in a string.
/* Test of searching in a string in a UTF-8 locale.
Copyright (C) 2007-2026 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
@@ -25,6 +25,20 @@
#include "macros.h"
/* The mcel-based implementation of mbsnlen behaves differently than the
original one. Namely, for invalid/incomplete byte sequences:
Where we ideally should have multi-byte-per-encoding-error (MEE) behaviour
everywhere, mcel implements single-byte-per-encoding-error (SEE) behaviour.
See <https://lists.gnu.org/archive/html/bug-gnulib/2023-07/msg00131.html>,
<https://lists.gnu.org/archive/html/bug-gnulib/2023-07/msg00145.html>.
Therefore, here we have different expected results, depending on the
implementation. */
#if GNULIB_MCEL_PREFER
# define OR(a,b) b
#else
# define OR(a,b) a
#endif
int
main ()
{
@@ -50,6 +64,74 @@ main ()
ASSERT (result == NULL);
}
/* Incomplete characters. See
https://www.unicode.org/versions/Unicode15.0.0/ch03.pdf
page 128 table 3-11. */
/* "\341\200\240" = 0xE1 0x80 0xA0 = U+1020. */
{
const char input[] = "f\341\200\341\200";
const char *result = mbscasestr (input, "");
ASSERT (result == input);
}
{
const char input[] = "f\341\200\341\200";
const char *result = mbscasestr (input, "\341\200");
ASSERT (result == input + 1);
}
{
const char input[] = "f\341\200\341\200";
const char *result = mbscasestr (input, "\200\341");
ASSERT (result == OR (NULL, input + 2));
}
/* "\360\221\222\240" = 0xF0 0x91 0x92 0xA0 = U+114A0. */
{
const char input[] = "f\360\221\222\360\221\222";
const char *result = mbscasestr (input, "");
ASSERT (result == input);
}
{
const char input[] = "f\360\221\222\360\221\222";
const char *result = mbscasestr (input, "\360\221\222");
ASSERT (result == input + 1);
}
{
const char input[] = "f\360\221\222\360\221\222";
const char *result = mbscasestr (input, "\221\222\360\221");
ASSERT (result == OR (NULL, input + 2));
}
{
const char input[] = "f\360\221\222\360\221\222";
const char *result = mbscasestr (input, "\221\222\360");
ASSERT (result == OR (NULL, input + 2));
}
{
const char input[] = "f\360\221\222\360\221\222";
const char *result = mbscasestr (input, "\222\360\221");
ASSERT (result == OR (NULL, input + 3));
}
{
const char input[] = "f\360\221\222\360\221\222";
const char *result = mbscasestr (input, "\222\360");
ASSERT (result == OR (NULL, input + 3));
}
{
const char input[] = "f\360\221\360\221";
const char *result = mbscasestr (input, "");
ASSERT (result == input);
}
{
const char input[] = "f\360\221\360\221";
const char *result = mbscasestr (input, "\360\221");
ASSERT (result == input + 1);
}
{
const char input[] = "f\360\221\360\221";
const char *result = mbscasestr (input, "\221\360");
ASSERT (result == OR (NULL, input + 2));
}
{
const char input[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÄBC ÄBCDÄB ÄBCDÄBCDÄBDE" */
const char *result = mbscasestr (input, "\303\244BCD\303\204BD"); /* "äBCDÄBD" */

View File

@@ -1,4 +1,4 @@
/* Test of case-insensitive searching in a string.
/* Test of case-insensitive searching in a string in a GB18030 locale.
Copyright (C) 2007-2026 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify

View File

@@ -1,4 +1,4 @@
/* Test of case-insensitive searching in a string.
/* Test of case-insensitive searching in a string in a Turkish locale.
Copyright (C) 2007-2026 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify

View File

@@ -1,4 +1,4 @@
/* Test of searching in a string.
/* Test of searching in a string in the "C" locale.
Copyright (C) 2007-2026 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify

View File

@@ -1,4 +1,4 @@
/* Test of searching in a string.
/* Test of searching in a string in a UTF-8 locale.
Copyright (C) 2007-2026 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
@@ -25,6 +25,20 @@
#include "macros.h"
/* The mcel-based implementation of mbsnlen behaves differently than the
original one. Namely, for invalid/incomplete byte sequences:
Where we ideally should have multi-byte-per-encoding-error (MEE) behaviour
everywhere, mcel implements single-byte-per-encoding-error (SEE) behaviour.
See <https://lists.gnu.org/archive/html/bug-gnulib/2023-07/msg00131.html>,
<https://lists.gnu.org/archive/html/bug-gnulib/2023-07/msg00145.html>.
Therefore, here we have different expected results, depending on the
implementation. */
#if GNULIB_MCEL_PREFER
# define OR(a,b) b
#else
# define OR(a,b) a
#endif
int
main ()
{
@@ -50,6 +64,74 @@ main ()
ASSERT (result == NULL);
}
/* Incomplete characters. See
https://www.unicode.org/versions/Unicode15.0.0/ch03.pdf
page 128 table 3-11. */
/* "\341\200\240" = 0xE1 0x80 0xA0 = U+1020. */
{
const char input[] = "f\341\200\341\200";
const char *result = mbsstr (input, "");
ASSERT (result == input);
}
{
const char input[] = "f\341\200\341\200";
const char *result = mbsstr (input, "\341\200");
ASSERT (result == input + 1);
}
{
const char input[] = "f\341\200\341\200";
const char *result = mbsstr (input, "\200\341");
ASSERT (result == OR (NULL, input + 2));
}
/* "\360\221\222\240" = 0xF0 0x91 0x92 0xA0 = U+114A0. */
{
const char input[] = "f\360\221\222\360\221\222";
const char *result = mbsstr (input, "");
ASSERT (result == input);
}
{
const char input[] = "f\360\221\222\360\221\222";
const char *result = mbsstr (input, "\360\221\222");
ASSERT (result == input + 1);
}
{
const char input[] = "f\360\221\222\360\221\222";
const char *result = mbsstr (input, "\221\222\360\221");
ASSERT (result == OR (NULL, input + 2));
}
{
const char input[] = "f\360\221\222\360\221\222";
const char *result = mbsstr (input, "\221\222\360");
ASSERT (result == OR (NULL, input + 2));
}
{
const char input[] = "f\360\221\222\360\221\222";
const char *result = mbsstr (input, "\222\360\221");
ASSERT (result == OR (NULL, input + 3));
}
{
const char input[] = "f\360\221\222\360\221\222";
const char *result = mbsstr (input, "\222\360");
ASSERT (result == OR (NULL, input + 3));
}
{
const char input[] = "f\360\221\360\221";
const char *result = mbsstr (input, "");
ASSERT (result == input);
}
{
const char input[] = "f\360\221\360\221";
const char *result = mbsstr (input, "\360\221");
ASSERT (result == input + 1);
}
{
const char input[] = "f\360\221\360\221";
const char *result = mbsstr (input, "\221\360");
ASSERT (result == OR (NULL, input + 2));
}
{
const char input[] = "\303\204BC \303\204BCD\303\204B \303\204BCD\303\204BCD\303\204BDE"; /* "ÄBC ÄBCDÄB ÄBCDÄBCDÄBDE" */
const char *result = mbsstr (input, "\303\204BCD\303\204BD"); /* "ÄBCDÄBD" */

View File

@@ -1,4 +1,4 @@
/* Test of searching in a string.
/* Test of searching in a string in a GB18030 locale.
Copyright (C) 2007-2026 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify