openat: don’t close (-1)
[gnulib.git] / lib / mbspbrk.c
blob83be2b5206c62857021fd61d8c04e90470392893
1 /* Searching a string for a character among 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 in the character string ACCEPT. Return the pointer to it, or NULL if none
33 exists. */
34 char *
35 mbspbrk (const char *string, const char *accept)
37 /* Optimize two cases. */
38 if (accept[0] == '\0')
39 return NULL;
40 if (accept[1] == '\0')
41 return mbschr (string, accept[0]);
42 /* General case. */
43 if (MB_CUR_MAX > 1)
45 char const *iter = string;
46 #if GNULIB_MCEL_PREFER
47 mcel_t a, g;
48 for (; *iter; iter += g.len)
50 g = mcel_scanz (iter);
51 if (g.len == 1)
53 if (mbschr (accept, *iter))
54 return (char *) iter;
56 else
57 for (char const *aiter = accept; *aiter; aiter += a.len)
59 a = mcel_scanz (aiter);
60 if (mcel_cmp (a, g) == 0)
61 return (char *) iter;
64 #else
65 mbuif_state_t state;
66 for (mbuif_init (state); mbuif_avail (state, iter); )
68 mbchar_t cur = mbuif_next (state, iter);
69 if (mb_len (cur) == 1)
71 if (mbschr (accept, *iter))
72 return (char *) iter;
74 else
76 mbuif_state_t astate;
77 const char *aiter;
78 for (mbuif_init (astate), aiter = accept;
79 mbuif_avail (astate, aiter); )
81 mbchar_t acur = mbuif_next (astate, aiter);
82 if (mb_equal (acur, cur))
83 return (char *) iter;
84 aiter += mb_len (acur);
87 iter += mb_len (cur);
89 #endif
90 return NULL;
92 else
93 return strpbrk (string, accept);