exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / mbspcasecmp.c
blobf01924cac50e5fc22fdb6a5995c71d6439e9af49
1 /* Case-insensitive string comparison function.
2 Copyright (C) 1998-1999, 2005-2008, 2010-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 <ctype.h>
24 #include <stdlib.h>
26 #if GNULIB_MCEL_PREFER
27 # include "mcel.h"
28 #else
29 # include "mbuiterf.h"
30 #endif
32 /* Compare the initial segment of the character string STRING consisting of
33 at most mbslen (PREFIX) characters with the character string PREFIX,
34 ignoring case. If the two match, return a pointer to the first byte
35 after this prefix in STRING. Otherwise, return NULL.
36 Note: This function may, in multibyte locales, return non-NULL if STRING
37 is of smaller length than PREFIX! */
38 char *
39 mbspcasecmp (const char *string, const char *prefix)
41 /* This is essentially the same as
42 mbsncasecmp (string, prefix, mbslen (prefix))
43 just with small optimizations. */
44 if (string == prefix)
45 return (char *) (string + strlen (string));
47 const char *iter1 = string;
48 const char *iter2 = prefix;
50 /* Be careful not to look at the entire extent of STRING or PREFIX until
51 needed. This is useful because when two strings differ, the difference is
52 most often already in the very few first characters. */
53 if (MB_CUR_MAX > 1)
55 #if GNULIB_MCEL_PREFER
56 while (*iter2)
58 if (!*iter1)
59 return NULL;
60 mcel_t g1 = mcel_scanz (iter1); iter1 += g1.len;
61 mcel_t g2 = mcel_scanz (iter2); iter2 += g2.len;
62 if (mcel_tocmp (c32tolower, g1, g2) != 0)
63 return NULL;
65 return (char *) iter1;
66 #else
67 mbuif_state_t state1;
68 mbuif_init (state1);
70 mbuif_state_t state2;
71 mbuif_init (state2);
73 while (mbuif_avail (state1, iter1) && mbuif_avail (state2, iter2))
75 mbchar_t cur1 = mbuif_next (state1, iter1);
76 mbchar_t cur2 = mbuif_next (state2, iter2);
77 int cmp = mb_casecmp (cur1, cur2);
79 if (cmp != 0)
80 return NULL;
82 iter1 += mb_len (cur1);
83 iter2 += mb_len (cur2);
85 if (!mbuif_avail (state2, iter2))
86 /* PREFIX equals STRING or is terminated before STRING. */
87 return (char *) iter1;
88 else
89 /* STRING terminated before PREFIX. */
90 return NULL;
91 #endif
93 else
94 for (;; iter1++, iter2++)
96 unsigned char c2 = *iter2;
98 if (c2 == '\0')
99 /* PREFIX equals STRING or is terminated before STRING. */
100 return (char *) iter1;
102 unsigned char c1 = *iter1;
104 if (c1 != c2)
106 c1 = tolower (c1);
107 if (c1 != c2 && c1 != tolower (c2))
108 /* STRING and PREFIX disagree,
109 or STRING terminated before PREFIX. */
110 return NULL;