1 /* Searching a string for a character outside a given set of characters.
2 Copyright (C) 1999, 2002, 2006-2025 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/>. */
25 #if GNULIB_MCEL_PREFER
28 # include "mbuiterf.h"
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
36 mbsspn (const char *string
, const char *reject
)
38 /* Optimize two cases. */
39 if (reject
[0] == '\0')
41 if (reject
[1] == '\0')
43 unsigned char uc
= (unsigned char) reject
[0];
44 const char *iter
= string
;
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
))
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
))
68 for (; *iter
!= '\0'; iter
++)
69 if ((unsigned char) *iter
!= uc
)
77 #if GNULIB_MCEL_PREFER
78 for (size_t i
= 0; ; )
83 mcel_t g
= mcel_scanz (string
+ i
);
86 if (!mbschr (reject
, c
))
91 for (char const *aiter
= reject
; ; )
95 mcel_t a
= mcel_scanz (aiter
);
96 if (mcel_cmp (a
, g
) == 0)
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
)
116 mbuif_state_t astate
;
118 for (mbuif_init (astate
), aiter
= reject
; ; )
120 if (!mbuif_avail (astate
, aiter
))
122 mbchar_t acur
= mbuif_next (astate
, aiter
);
123 if (mb_equal (acur
, cur
))
125 aiter
+= mb_len (acur
);
128 iter
+= mb_len (cur
);
131 return iter
- string
;
135 return strspn (string
, reject
);