annotate lib/strcasecmp.c @ 7304:1c4ed7637c24

Include <config.h> unconditionally.
author Bruno Haible <bruno@clisp.org>
date Thu, 14 Sep 2006 14:18:36 +0000
parents bbcddcd96f1f
children a1d177cd9523
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
1 /* Case-insensitive string comparison function.
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6355
diff changeset
2 Copyright (C) 1998-1999, 2005-2006 Free Software Foundation, Inc.
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2005,
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
4 based on earlier glibc code.
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
5
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
6 This program is free software; you can redistribute it and/or modify
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
8 the Free Software Foundation; either version 2, or (at your option)
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
9 any later version.
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
10
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
11 This program is distributed in the hope that it will be useful,
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
14 GNU General Public License for more details.
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
15
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
17 along with this program; if not, write to the Free Software Foundation,
5848
a48fb0e98c8c *** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
19
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6355
diff changeset
20 #include <config.h>
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
21
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
22 /* Specification. */
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
23 #include "strcase.h"
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
24
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
25 #include <ctype.h>
6355
bbcddcd96f1f Avoid integer overflow on exotic platforms.
Bruno Haible <bruno@clisp.org>
parents: 6056
diff changeset
26 #include <limits.h>
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
27
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
28 #if HAVE_MBRTOWC
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
29 # include "mbuiter.h"
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
30 #endif
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
31
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
32 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
33
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
34 /* Compare strings S1 and S2, ignoring case, returning less than, equal to or
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
35 greater than zero if S1 is lexicographically less than, equal to or greater
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
36 than S2.
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
37 Note: This function may, in multibyte locales, return 0 for strings of
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
38 different lengths! */
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
39 int
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
40 strcasecmp (const char *s1, const char *s2)
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
41 {
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
42 if (s1 == s2)
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
43 return 0;
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
44
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
45 /* Be careful not to look at the entire extent of s1 or s2 until needed.
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
46 This is useful because when two strings differ, the difference is
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
47 most often already in the very few first characters. */
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
48 #if HAVE_MBRTOWC
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
49 if (MB_CUR_MAX > 1)
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
50 {
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
51 mbui_iterator_t iter1;
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
52 mbui_iterator_t iter2;
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
53
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
54 mbui_init (iter1, s1);
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
55 mbui_init (iter2, s2);
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
56
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
57 while (mbui_avail (iter1) && mbui_avail (iter2))
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
58 {
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
59 int cmp = mb_casecmp (mbui_cur (iter1), mbui_cur (iter2));
1696
7a6348858488 (STRXCASECMP_FUNCTION): Don't increment within macro argument list.
Jim Meyering <jim@meyering.net>
parents: 1610
diff changeset
60
6051
c9d12df22e6e Simplify by using mb_casecmp.
Bruno Haible <bruno@clisp.org>
parents: 6048
diff changeset
61 if (cmp != 0)
c9d12df22e6e Simplify by using mb_casecmp.
Bruno Haible <bruno@clisp.org>
parents: 6048
diff changeset
62 return cmp;
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
63
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
64 mbui_advance (iter1);
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
65 mbui_advance (iter2);
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
66 }
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
67 if (mbui_avail (iter1))
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
68 /* s2 terminated before s1. */
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
69 return 1;
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
70 if (mbui_avail (iter2))
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
71 /* s1 terminated before s2. */
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
72 return -1;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
73 return 0;
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
74 }
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
75 else
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
76 #endif
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
77 {
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
78 const unsigned char *p1 = (const unsigned char *) s1;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
79 const unsigned char *p2 = (const unsigned char *) s2;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
80 unsigned char c1, c2;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
81
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
82 do
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
83 {
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
84 c1 = TOLOWER (*p1);
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
85 c2 = TOLOWER (*p2);
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
86
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
87 if (c1 == '\0')
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
88 break;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
89
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
90 ++p1;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
91 ++p2;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
92 }
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
93 while (c1 == c2);
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
94
6355
bbcddcd96f1f Avoid integer overflow on exotic platforms.
Bruno Haible <bruno@clisp.org>
parents: 6056
diff changeset
95 if (UCHAR_MAX <= INT_MAX)
bbcddcd96f1f Avoid integer overflow on exotic platforms.
Bruno Haible <bruno@clisp.org>
parents: 6056
diff changeset
96 return c1 - c2;
bbcddcd96f1f Avoid integer overflow on exotic platforms.
Bruno Haible <bruno@clisp.org>
parents: 6056
diff changeset
97 else
bbcddcd96f1f Avoid integer overflow on exotic platforms.
Bruno Haible <bruno@clisp.org>
parents: 6056
diff changeset
98 /* On machines where 'char' and 'int' are types of the same size, the
bbcddcd96f1f Avoid integer overflow on exotic platforms.
Bruno Haible <bruno@clisp.org>
parents: 6056
diff changeset
99 difference of two 'unsigned char' values - including the sign bit -
bbcddcd96f1f Avoid integer overflow on exotic platforms.
Bruno Haible <bruno@clisp.org>
parents: 6056
diff changeset
100 doesn't fit in an 'int'. */
bbcddcd96f1f Avoid integer overflow on exotic platforms.
Bruno Haible <bruno@clisp.org>
parents: 6056
diff changeset
101 return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
102 }
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
103 }