Hash :
9452e886
Author :
Date :
2023-07-19T13:51:55
Don’t worry about Version 7 tolower Some code ported back to pre-C89 libraries where tolower (C) had undefined behavior if C is not an upper case character. Nowadays that function is _tolower which is itself obsolete, and much Gnulib code already assumes this part of C89 anyway. Assume C89 or better tolower, which simplifies the code and should improve performance slightly. * lib/mbmemcasecmp.c, lib/mbmemcasecoll.c, lib/mbscasecmp.c: * lib/mbscasestr.c, lib/mbsncasecmp.c, lib/mbspcasecmp.c: * lib/strcasecmp.c, lib/strcasestr.c, lib/strncasecmp.c: (TOLOWER): Remove. All uses replaced by tolower.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
/* Locale-specific case-ignoring memory comparison.
Copyright (C) 2001, 2009-2023 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>, 2001.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
/* Specification. */
#include "mbmemcasecoll.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
/* Get tolower(). */
#include <ctype.h>
/* Get mbstate_t. */
#include <wchar.h>
/* Get char32_t, mbrtoc32(), c32rtomb(), c32tolower(). */
#include <uchar.h>
#include "malloca.h"
#include "memcmp2.h"
#include "memcoll.h"
/* Apply c32tolower() to the multibyte character sequence in INBUF, storing the
result as a multibyte character sequence in OUTBUF. */
static size_t
apply_c32tolower (const char *inbuf, size_t inbufsize,
char *outbuf, size_t outbufsize)
{
char *outbuf_orig = outbuf;
size_t remaining;
remaining = inbufsize;
while (remaining > 0)
{
mbstate_t state;
mbszero (&state);
for (;;)
{
char32_t wc1;
size_t n1;
n1 = mbrtoc32 (&wc1, inbuf, remaining, &state);
if (n1 == (size_t)(-1))
{
/* Invalid multibyte character on input.
Copy one byte without modification. */
*outbuf++ = *inbuf++;
remaining -= 1;
break;
}
else if (n1 == (size_t)(-2))
{
/* Incomplete multibyte sequence on input.
Pass it through unmodified. */
while (remaining > 0)
{
*outbuf++ = *inbuf++;
remaining -= 1;
}
break;
}
else
{
wint_t wc2;
if (n1 == 0) /* NUL character? */
n1 = 1;
#if !GNULIB_MBRTOC32_REGULAR
else if (n1 == (size_t)(-3))
n1 = 0;
#endif
wc2 = c32tolower (wc1);
if (wc2 != wc1)
{
mbstate_t state2;
size_t n2;
mbszero (&state2);
n2 = c32rtomb (outbuf, wc2, &state2);
if (n2 != (size_t)(-1))
{
/* Store the translated multibyte character. */
outbuf += n2;
goto done_storing;
}
}
/* Nothing to translate. */
memcpy (outbuf, inbuf, n1);
outbuf += n1;
done_storing:
inbuf += n1;
remaining -= n1;
}
#if !GNULIB_MBRTOC32_REGULAR
if (mbsinit (&state))
#endif
break;
}
}
/* Verify the output buffer was large enough. */
if (outbuf - outbuf_orig > outbufsize)
abort ();
/* Return the number of written output bytes. */
return outbuf - outbuf_orig;
}
/* Apply tolower() to the unibyte character sequence in INBUF, storing the
result as a unibyte character sequence in OUTBUF. */
static void
apply_tolower (const char *inbuf, char *outbuf, size_t bufsize)
{
for (; bufsize > 0; bufsize--)
{
*outbuf = tolower ((unsigned char) *inbuf);
inbuf++;
outbuf++;
}
}
int
mbmemcasecoll (const char *s1, size_t s1len, const char *s2, size_t s2len,
bool hard_LC_COLLATE)
{
char *t1;
size_t t1len;
char *t2;
size_t t2len;
char *memory;
int cmp;
if (MB_CUR_MAX > 1)
{
/* Application of towlower grows each character by a factor 2
at most. */
t1len = 2 * s1len;
t2len = 2 * s2len;
}
else
{
/* Application of tolower doesn't change the size. */
t1len = s1len;
t2len = s2len;
}
/* Allocate memory for t1 and t2. */
memory = (char *) malloca (t1len + 1 + t2len + 1);
if (memory == NULL)
{
errno = ENOMEM;
return 0;
}
t1 = memory;
t2 = memory + t1len + 1;
/* Case-fold the two argument strings. */
if (MB_CUR_MAX > 1)
{
t1len = apply_c32tolower (s1, s1len, t1, t1len);
t2len = apply_c32tolower (s2, s2len, t2, t2len);
}
else
{
apply_tolower (s1, t1, s1len);
apply_tolower (s2, t2, s2len);
}
/* Compare the two case-folded strings. */
if (hard_LC_COLLATE)
cmp = memcoll (t1, t1len, t2, t2len);
else
{
cmp = memcmp2 (t1, t1len, t2, t2len);
errno = 0;
}
{
int saved_errno = errno;
freea (memory);
errno = saved_errno;
}
return cmp;
}