annotate lib/strcasecmp.c @ 6056:4838606fdc03

strcasecmp.c now uses mbuiter.
author Bruno Haible <bruno@clisp.org>
date Wed, 17 Aug 2005 14:01:07 +0000
parents 4cfaf4a21df2
children bbcddcd96f1f
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.
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
2 Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc.
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
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
20 #ifdef HAVE_CONFIG_H
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
21 # include <config.h>
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
22 #endif
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
23
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
24 /* Specification. */
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
25 #include "strcase.h"
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
26
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
27 #include <ctype.h>
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
28
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
29 #if HAVE_MBRTOWC
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
30 # include "mbuiter.h"
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
31 #endif
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
32
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
33 #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
34
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
35 /* 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
36 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
37 than S2.
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
38 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
39 different lengths! */
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
40 int
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
41 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
42 {
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
43 if (s1 == s2)
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
44 return 0;
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
45
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
46 /* 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
47 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
48 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
49 #if HAVE_MBRTOWC
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
50 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
51 {
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
52 mbui_iterator_t iter1;
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
53 mbui_iterator_t iter2;
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
54
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
55 mbui_init (iter1, s1);
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
56 mbui_init (iter2, s2);
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
57
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
58 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
59 {
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
60 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
61
6051
c9d12df22e6e Simplify by using mb_casecmp.
Bruno Haible <bruno@clisp.org>
parents: 6048
diff changeset
62 if (cmp != 0)
c9d12df22e6e Simplify by using mb_casecmp.
Bruno Haible <bruno@clisp.org>
parents: 6048
diff changeset
63 return cmp;
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
64
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
65 mbui_advance (iter1);
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
66 mbui_advance (iter2);
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
67 }
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
68 if (mbui_avail (iter1))
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
69 /* s2 terminated before s1. */
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
70 return 1;
6056
4838606fdc03 strcasecmp.c now uses mbuiter.
Bruno Haible <bruno@clisp.org>
parents: 6053
diff changeset
71 if (mbui_avail (iter2))
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
72 /* s1 terminated before s2. */
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
73 return -1;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
74 return 0;
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
75 }
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
76 else
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
77 #endif
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
78 {
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
79 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
80 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
81 unsigned char c1, c2;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
82
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
83 do
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
84 {
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
85 c1 = TOLOWER (*p1);
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
86 c2 = TOLOWER (*p2);
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
87
6048
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
88 if (c1 == '\0')
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
89 break;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
90
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
91 ++p1;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
92 ++p2;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
93 }
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
94 while (c1 == c2);
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
95
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
96 return c1 - c2;
5943d7de8c5d Make strcasecmp() work right in multibyte locales.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
97 }
1610
055e7427bd19 Add #ifdefs so it can be used for strncasecmp, too.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
98 }