exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / mbssep.c
blobeb9f2184a12ef366b56a2816534002005638a13c
1 /* Tokenizing a string.
2 Copyright (C) 2007-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 char *
32 mbssep (char **stringp, const char *delim)
34 if (MB_CUR_MAX > 1)
36 char *start = *stringp;
38 if (start == NULL)
39 return NULL;
41 /* No need to optimize the cases of 0 or 1 delimiters specially,
42 since mbspbrk already optimizes them. */
44 char *ptr = mbspbrk (start, delim);
46 if (ptr == NULL)
47 *stringp = NULL;
48 else
50 #if GNULIB_MCEL_PREFER
51 *stringp = ptr + mcel_scanz (ptr).len;
52 #else
53 mbuif_state_t state;
54 mbuif_init (state);
55 if (!mbuif_avail (state, ptr))
56 abort ();
57 mbchar_t cur = mbuif_next (state, ptr);
58 *stringp = ptr + mb_len (cur);
59 #endif
60 *ptr = '\0';
62 return start;
64 else
65 return strsep (stringp, delim);