1 /* Locale-specific case-ignoring memory comparison.
2 Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2001.
5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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 GNU
13 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 <http://www.gnu.org/licenses/>. */
21 #include "mbmemcasecoll.h"
30 /* Get mbstate_t, mbrtowc(), wcrtomb(). */
40 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
42 /* Apply towlower() to the multibyte character sequence in INBUF, storing the
43 result as a multibyte character sequence in OUTBUF. */
45 apply_towlower (const char *inbuf, size_t inbufsize,
46 char *outbuf, size_t outbufsize)
48 char *outbuf_orig = outbuf;
51 remaining = inbufsize;
58 memset (&state, '\0', sizeof (mbstate_t));
59 n1 = mbrtowc (&wc1, inbuf, remaining, &state);
60 if (n1 == (size_t)(-2))
62 if (n1 != (size_t)(-1))
64 wint_t wc2 = towlower (wc1);
70 memset (&state, '\0', sizeof (mbstate_t));
71 n2 = wcrtomb (outbuf, wc2, &state);
72 if (n2 != (size_t)(-1))
74 /* Store the translated multibyte character. */
82 /* Nothing to translate. */
83 memcpy (outbuf, inbuf, n1);
90 /* Invalid multibyte character on input.
91 Copy one byte without modification. */
95 /* Incomplete multibyte sequence on input.
96 Pass it through unmodified. */
103 /* Verify the output buffer was large enough. */
104 if (outbuf - outbuf_orig > outbufsize)
107 /* Return the number of written output bytes. */
108 return outbuf - outbuf_orig;
111 /* Apply tolower() to the unibyte character sequence in INBUF, storing the
112 result as a unibyte character sequence in OUTBUF. */
114 apply_tolower (const char *inbuf, char *outbuf, size_t bufsize)
116 for (; bufsize > 0; bufsize--)
118 *outbuf = TOLOWER ((unsigned char) *inbuf);
125 mbmemcasecoll (const char *s1, size_t s1len, const char *s2, size_t s2len,
126 bool hard_LC_COLLATE)
137 /* Application of towlower grows each character by a factor 2
144 /* Application of tolower doesn't change the size. */
148 /* Allocate memory for t1 and t2. */
149 memory = (char *) malloca (t1len + 1 + t2len + 1);
156 t2 = memory + t1len + 1;
158 /* Csae-fold the two argument strings. */
161 t1len = apply_towlower (s1, s1len, t1, t1len);
162 t2len = apply_towlower (s2, s2len, t2, t2len);
166 apply_tolower (s1, t1, s1len);
167 apply_tolower (s2, t2, s2len);
170 /* Compare the two case-folded strings. */
172 cmp = memcoll (t1, t1len, t2, t2len);
175 cmp = memcmp2 (t1, t1len, t2, t2len);
180 int saved_errno = errno;