1 /* xstrchr.c - strchr(3) that handles multibyte characters. */
3 /* Copyright (C) 2002 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
32 /* In some locales, the non-first byte of some multibyte characters have
33 the same value as some ascii character. Faced with these strings, a
34 legacy strchr() might return the wrong value. */
37 #if defined (PROTOTYPES)
38 xstrchr (const char *s, int c)
48 size_t strlength, mblength;
50 /* The locale encodings with said weird property are BIG5, BIG5-HKSCS,
51 GBK, GB18030, SHIFT_JIS, and JOHAB. They exhibit the problem only
52 when c >= 0x30. We can therefore use the faster bytewise search if
54 if ((unsigned char)c >= '0' && MB_CUR_MAX > 1)
57 memset (&state, '\0', sizeof(mbstate_t));
58 strlength = strlen (s);
62 mblength = mbrlen (pos, strlength, &state);
63 if (mblength == (size_t)-2 || mblength == (size_t)-1 || mblength == (size_t)0)
66 if (c == (unsigned char)*pos)
69 strlength -= mblength;
73 return ((char *)NULL);
77 return (strchr (s, c));