1 /* Searching a string for a character among 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 in the character string ACCEPT. Return the number of bytes from the
33 beginning of the string to this occurrence, or to the end of the string
36 mbscspn (const char *string
, const char *accept
)
38 /* Optimize two cases. */
39 if (accept
[0] == '\0')
40 return strlen (string
);
41 if (accept
[1] == '\0')
43 const char *ptr
= mbschr (string
, accept
[0]);
44 return (ptr
!= NULL
? ptr
- string
: strlen (string
));
49 #if GNULIB_MCEL_PREFER
52 for (i
= 0; string
[i
]; i
+= g
.len
)
54 g
= mcel_scanz (string
+ i
);
57 if (mbschr (accept
, string
[i
]))
61 for (char const *aiter
= accept
; *aiter
; aiter
+= a
.len
)
63 a
= mcel_scanz (aiter
);
64 if (mcel_cmp (g
, a
) == 0)
72 for (mbuif_init (state
), iter
= string
; mbuif_avail (state
, iter
); )
74 mbchar_t cur
= mbuif_next (state
, iter
);
75 if (mb_len (cur
) == 1)
77 if (mbschr (accept
, *iter
))
84 for (mbuif_init (astate
), aiter
= accept
;
85 mbuif_avail (astate
, aiter
); )
87 mbchar_t acur
= mbuif_next (astate
, aiter
);
88 if (mb_equal (acur
, cur
))
90 aiter
+= mb_len (acur
);
100 return strcspn (string
, accept
);