exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / mbsspn.c
blobc06a86ff99998a05efc64b00aabee247a7cf9f33
1 /* Searching a string for a character outside a given set of characters.
2 Copyright (C) 1999, 2002, 2006-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2007.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include <string.h>
23 #include <stdlib.h>
25 #if GNULIB_MCEL_PREFER
26 # include "mcel.h"
27 #else
28 # include "mbuiterf.h"
29 #endif
31 /* Find the first occurrence in the character string STRING of any character
32 not in the character string REJECT. Return the number of bytes from the
33 beginning of the string to this occurrence, or to the end of the string
34 if none exists. */
35 size_t
36 mbsspn (const char *string, const char *reject)
38 /* Optimize two cases. */
39 if (reject[0] == '\0')
40 return 0;
41 if (reject[1] == '\0')
43 unsigned char uc = (unsigned char) reject[0];
44 const char *iter = string;
46 if (MB_CUR_MAX > 1)
48 #if GNULIB_MCEL_PREFER
49 for (mcel_t g; *iter; iter += g.len)
51 g = mcel_scanz (iter);
52 if (! (g.len == 1 && (unsigned char) *iter == uc))
53 break;
55 #else
56 mbuif_state_t state;
57 for (mbuif_init (state); mbuif_avail (state, iter); )
59 mbchar_t cur = mbuif_next (state, iter);
60 if (!(mb_len (cur) == 1 && (unsigned char) *iter == uc))
61 break;
62 iter += mb_len (cur);
64 #endif
66 else
68 for (; *iter != '\0'; iter++)
69 if ((unsigned char) *iter != uc)
70 break;
72 return iter - string;
74 /* General case. */
75 if (MB_CUR_MAX > 1)
77 #if GNULIB_MCEL_PREFER
78 for (size_t i = 0; ; )
80 char c = string[i];
81 if (!c)
82 return i;
83 mcel_t g = mcel_scanz (string + i);
84 if (g.len == 1)
86 if (!mbschr (reject, c))
87 return i;
89 else
91 for (char const *aiter = reject; ; )
93 if (!*aiter)
94 return i;
95 mcel_t a = mcel_scanz (aiter);
96 if (mcel_cmp (a, g) == 0)
97 break;
98 aiter += a.len;
101 i += g.len;
103 #else
104 mbuif_state_t state;
105 const char *iter;
106 for (mbuif_init (state), iter = string; mbuif_avail (state, iter); )
108 mbchar_t cur = mbuif_next (state, iter);
109 if (mb_len (cur) == 1)
111 if (mbschr (reject, *iter) == NULL)
112 goto found;
114 else
116 mbuif_state_t astate;
117 const char *aiter;
118 for (mbuif_init (astate), aiter = reject; ; )
120 if (!mbuif_avail (astate, aiter))
121 goto found;
122 mbchar_t acur = mbuif_next (astate, aiter);
123 if (mb_equal (acur, cur))
124 break;
125 aiter += mb_len (acur);
128 iter += mb_len (cur);
130 found:
131 return iter - string;
132 #endif
134 else
135 return strspn (string, reject);