annotate lib/mbspcasecmp.c @ 10001:facc928673d7

Declare rpmatch.
author Bruno Haible <bruno@clisp.org>
date Tue, 29 Apr 2008 02:55:59 +0200
parents bbbbbf4cd1c5
children a0bbe1a6f787
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8159
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Case-insensitive string comparison function.
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 1998-1999, 2005-2007 Free Software Foundation, Inc.
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2007.
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8159
diff changeset
5 This program is free software: you can redistribute it and/or modify
8159
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8159
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8159
diff changeset
8 (at your option) any later version.
8159
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8159
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8159
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #include <config.h>
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <string.h>
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <ctype.h>
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #if HAVE_MBRTOWC
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 # include "mbuiter.h"
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #endif
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 /* Compare the initial segment of the character string STRING consisting of
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 at most mbslen (PREFIX) characters with the character string PREFIX,
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 ignoring case, returning less than, equal to or greater than zero if this
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 initial segment is lexicographically less than, equal to or greater than
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 PREFIX.
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 Note: This function may, in multibyte locales, return 0 if STRING is of
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 smaller length than PREFIX! */
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 char *
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 mbspcasecmp (const char *string, const char *prefix)
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 {
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 /* This is essentially the same as
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 mbsncasecmp (string, prefix, mbslen (prefix))
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 just with small optimizations. */
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 if (string == prefix)
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 return (char *) (string + strlen (string));
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 /* Be careful not to look at the entire extent of STRING or PREFIX until
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 needed. This is useful because when two strings differ, the difference is
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 most often already in the very few first characters. */
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #if HAVE_MBRTOWC
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 if (MB_CUR_MAX > 1)
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 {
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 mbui_iterator_t iter1;
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 mbui_iterator_t iter2;
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 mbui_init (iter1, string);
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 mbui_init (iter2, prefix);
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 while (mbui_avail (iter1) && mbui_avail (iter2))
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 {
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 int cmp = mb_casecmp (mbui_cur (iter1), mbui_cur (iter2));
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 if (cmp != 0)
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 return NULL;
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 mbui_advance (iter1);
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 mbui_advance (iter2);
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 }
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 if (!mbui_avail (iter2))
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 /* PREFIX equals STRING or is terminated before STRING. */
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 return (char *) mbui_cur_ptr (iter1);
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 else
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 /* STRING terminated before PREFIX. */
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 return NULL;
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 }
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 else
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 #endif
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 {
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 const unsigned char *p1 = (const unsigned char *) string;
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 const unsigned char *p2 = (const unsigned char *) prefix;
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 unsigned char c1, c2;
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 for (; ; p1++, p2++)
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 {
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 c1 = TOLOWER (*p1);
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 c2 = TOLOWER (*p2);
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 if (c2 == '\0' || c1 != c2)
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 break;
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 }
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 if (c2 == '\0')
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 /* PREFIX equals STRING or is terminated before STRING. */
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 return (char *) p1;
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 else
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 /* STRING terminated before PREFIX. */
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 return NULL;
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 }
a6b23644124f New module 'mbspcasecmp'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 }