Mercurial > hg > octave-lojdl > gnulib-hg
annotate lib/mbspcasecmp.c @ 17426:90f3d53e01f5
sig2str: port to C++
* lib/sig2str.h (sig2str, str2sig): Declare as extern "C".
Reported by Daniel J Sebald in
<http://lists.gnu.org/archive/html/bug-gnulib/2013-06/msg00000.html>.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Sun, 02 Jun 2013 11:52:41 -0700 |
parents | e542fd46ad6f |
children |
rev | line source |
---|---|
8159 | 1 /* Case-insensitive string comparison function. |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
2 Copyright (C) 1998-1999, 2005-2008, 2010-2013 Free Software Foundation, Inc. |
8159 | 3 Written by Bruno Haible <bruno@clisp.org>, 2007. |
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 | 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 | 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 | |
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 | 17 |
18 #include <config.h> | |
19 | |
20 /* Specification. */ | |
21 #include <string.h> | |
22 | |
23 #include <ctype.h> | |
24 | |
10954
a0bbe1a6f787
Remove HAVE_MBRTOWC conditionals. Use mbrtowc unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
25 #include "mbuiter.h" |
8159 | 26 |
27 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) | |
28 | |
29 /* Compare the initial segment of the character string STRING consisting of | |
30 at most mbslen (PREFIX) characters with the character string PREFIX, | |
13478
957a79f455ba
mbspcasecmp: Fix function specification.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
31 ignoring case. If the two match, return a pointer to the first byte |
957a79f455ba
mbspcasecmp: Fix function specification.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
32 after this prefix in STRING. Otherwise, return NULL. |
957a79f455ba
mbspcasecmp: Fix function specification.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
33 Note: This function may, in multibyte locales, return non-NULL if STRING |
957a79f455ba
mbspcasecmp: Fix function specification.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
34 is of smaller length than PREFIX! */ |
8159 | 35 char * |
36 mbspcasecmp (const char *string, const char *prefix) | |
37 { | |
38 /* This is essentially the same as | |
39 mbsncasecmp (string, prefix, mbslen (prefix)) | |
40 just with small optimizations. */ | |
41 if (string == prefix) | |
42 return (char *) (string + strlen (string)); | |
43 | |
44 /* Be careful not to look at the entire extent of STRING or PREFIX until | |
45 needed. This is useful because when two strings differ, the difference is | |
46 most often already in the very few first characters. */ | |
47 if (MB_CUR_MAX > 1) | |
48 { | |
49 mbui_iterator_t iter1; | |
50 mbui_iterator_t iter2; | |
51 | |
52 mbui_init (iter1, string); | |
53 mbui_init (iter2, prefix); | |
54 | |
55 while (mbui_avail (iter1) && mbui_avail (iter2)) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
56 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
57 int cmp = mb_casecmp (mbui_cur (iter1), mbui_cur (iter2)); |
8159 | 58 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
59 if (cmp != 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
60 return NULL; |
8159 | 61 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
62 mbui_advance (iter1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
63 mbui_advance (iter2); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
64 } |
8159 | 65 if (!mbui_avail (iter2)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
66 /* PREFIX equals STRING or is terminated before STRING. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
67 return (char *) mbui_cur_ptr (iter1); |
8159 | 68 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
69 /* STRING terminated before PREFIX. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
70 return NULL; |
8159 | 71 } |
72 else | |
73 { | |
74 const unsigned char *p1 = (const unsigned char *) string; | |
75 const unsigned char *p2 = (const unsigned char *) prefix; | |
76 unsigned char c1, c2; | |
77 | |
78 for (; ; p1++, p2++) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
79 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
80 c1 = TOLOWER (*p1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
81 c2 = TOLOWER (*p2); |
8159 | 82 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
83 if (c2 == '\0' || c1 != c2) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
84 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
85 } |
8159 | 86 |
87 if (c2 == '\0') | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
88 /* PREFIX equals STRING or is terminated before STRING. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
89 return (char *) p1; |
8159 | 90 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
91 /* STRING terminated before PREFIX. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10954
diff
changeset
|
92 return NULL; |
8159 | 93 } |
94 } |