New module 'mbspcasecmp'.
[gnulib.git] / lib / mbspcasecmp.c
1 /* Case-insensitive string comparison function.
2    Copyright (C) 1998-1999, 2005-2007 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2007.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
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
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <string.h>
23
24 #include <ctype.h>
25
26 #if HAVE_MBRTOWC
27 # include "mbuiter.h"
28 #endif
29
30 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
31
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, returning less than, equal to or greater than zero if this
35    initial segment is lexicographically less than, equal to or greater than
36    PREFIX.
37    Note: This function may, in multibyte locales, return 0 if STRING is of
38    smaller length than PREFIX!  */
39 char *
40 mbspcasecmp (const char *string, const char *prefix)
41 {
42   /* This is essentially the same as
43        mbsncasecmp (string, prefix, mbslen (prefix))
44      just with small optimizations.  */
45   if (string == prefix)
46     return (char *) (string + strlen (string));
47
48   /* Be careful not to look at the entire extent of STRING or PREFIX until
49      needed.  This is useful because when two strings differ, the difference is
50      most often already in the very few first characters.  */
51 #if HAVE_MBRTOWC
52   if (MB_CUR_MAX > 1)
53     {
54       mbui_iterator_t iter1;
55       mbui_iterator_t iter2;
56
57       mbui_init (iter1, string);
58       mbui_init (iter2, prefix);
59
60       while (mbui_avail (iter1) && mbui_avail (iter2))
61         {
62           int cmp = mb_casecmp (mbui_cur (iter1), mbui_cur (iter2));
63
64           if (cmp != 0)
65             return NULL;
66
67           mbui_advance (iter1);
68           mbui_advance (iter2);
69         }
70       if (!mbui_avail (iter2))
71         /* PREFIX equals STRING or is terminated before STRING.  */
72         return (char *) mbui_cur_ptr (iter1);
73       else
74         /* STRING terminated before PREFIX.  */
75         return NULL;
76     }
77   else
78 #endif
79     {
80       const unsigned char *p1 = (const unsigned char *) string;
81       const unsigned char *p2 = (const unsigned char *) prefix;
82       unsigned char c1, c2;
83
84       for (; ; p1++, p2++)
85         {
86           c1 = TOLOWER (*p1);
87           c2 = TOLOWER (*p2);
88
89           if (c2 == '\0' || c1 != c2)
90             break;
91         }
92
93       if (c2 == '\0')
94         /* PREFIX equals STRING or is terminated before STRING.  */
95         return (char *) p1;
96       else
97         /* STRING terminated before PREFIX.  */
98         return NULL;
99     }
100 }