Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/memcoll.c @ 9928:9a02133ad731
Add tentative support for Linux libc5.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 17 Apr 2008 02:01:23 +0200 |
parents | bbbbbf4cd1c5 |
children | e8d2c6fc33ad |
rev | line source |
---|---|
1869 | 1 /* Locale-specific memory comparison. |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
7149
diff
changeset
|
2 |
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
7149
diff
changeset
|
3 Copyright (C) 1999, 2002, 2003, 2004, 2006 Free Software Foundation, Inc. |
1869 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
1869 | 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:
7302
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:
7302
diff
changeset
|
8 (at your option) any later version. |
1869 | 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:
7302
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
1869 | 17 |
18 /* Contributed by Paul Eggert <eggert@twinsun.com>. */ | |
19 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
7149
diff
changeset
|
20 #include <config.h> |
1869 | 21 |
4347
df44e79ce676
.h files should stand alone, but we shouldn't include <sys/types.h>
Paul Eggert <eggert@cs.ucla.edu>
parents:
4120
diff
changeset
|
22 #include "memcoll.h" |
df44e79ce676
.h files should stand alone, but we shouldn't include <sys/types.h>
Paul Eggert <eggert@cs.ucla.edu>
parents:
4120
diff
changeset
|
23 |
3708
bd719c800d4b
Include errno.h, and declare errno if not defined.
Jim Meyering <jim@meyering.net>
parents:
2997
diff
changeset
|
24 #include <errno.h> |
4664 | 25 #include <string.h> |
1869 | 26 |
27 /* Compare S1 (with length S1LEN) and S2 (with length S2LEN) according | |
1992
da00e3cba254
(memcoll): The two arguments cannot be
Jim Meyering <jim@meyering.net>
parents:
1869
diff
changeset
|
28 to the LC_COLLATE locale. S1 and S2 do not overlap, and are not |
4120
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
29 adjacent. Perhaps temporarily modify the bytes after S1 and S2, |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
30 but restore their original contents before returning. Set errno to an |
3708
bd719c800d4b
Include errno.h, and declare errno if not defined.
Jim Meyering <jim@meyering.net>
parents:
2997
diff
changeset
|
31 error number if there is an error, and to zero otherwise. */ |
1869 | 32 int |
33 memcoll (char *s1, size_t s1len, char *s2, size_t s2len) | |
34 { | |
35 int diff; | |
4120
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
36 |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
37 #if HAVE_STRCOLL |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
38 |
7147
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
39 /* strcoll is slow on many platforms, so check for the common case |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
40 where the arguments are bytewise equal. Otherwise, walk through |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
41 the buffers using strcoll on each substring. */ |
1869 | 42 |
7147
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
43 if (s1len == s2len && memcmp (s1, s2, s1len) == 0) |
7149
bf3b976f47a7
(memcoll): Set errno = 0 in the shortcut case, too.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7147
diff
changeset
|
44 { |
bf3b976f47a7
(memcoll): Set errno = 0 in the shortcut case, too.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7147
diff
changeset
|
45 errno = 0; |
bf3b976f47a7
(memcoll): Set errno = 0 in the shortcut case, too.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7147
diff
changeset
|
46 diff = 0; |
bf3b976f47a7
(memcoll): Set errno = 0 in the shortcut case, too.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7147
diff
changeset
|
47 } |
7147
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
48 else |
1869 | 49 { |
7147
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
50 char n1 = s1[s1len]; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
51 char n2 = s2[s2len]; |
1869 | 52 |
7147
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
53 s1[s1len++] = '\0'; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
54 s2[s2len++] = '\0'; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
55 |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
56 while (! (errno = 0, (diff = strcoll (s1, s2)) || errno)) |
1869 | 57 { |
7147
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
58 /* strcoll found no difference, but perhaps it was fooled by NUL |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
59 characters in the data. Work around this problem by advancing |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
60 past the NUL chars. */ |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
61 size_t size1 = strlen (s1) + 1; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
62 size_t size2 = strlen (s2) + 1; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
63 s1 += size1; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
64 s2 += size2; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
65 s1len -= size1; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
66 s2len -= size2; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
67 |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
68 if (s1len == 0) |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
69 { |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
70 if (s2len != 0) |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
71 diff = -1; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
72 break; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
73 } |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
74 else if (s2len == 0) |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
75 { |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
76 diff = 1; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
77 break; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
78 } |
1869 | 79 } |
7147
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
80 |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
81 s1[s1len - 1] = n1; |
1388e7363d86
* memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
82 s2[s2len - 1] = n2; |
1869 | 83 } |
84 | |
4120
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
85 #else |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
86 |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
87 diff = memcmp (s1, s2, s1len < s2len ? s1len : s2len); |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
88 if (! diff) |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
89 diff = s1len < s2len ? -1 : s1len != s2len; |
4464
580114b1a865
(memcoll) [!HAVE_STRCOLL]: Clear errno.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4347
diff
changeset
|
90 errno = 0; |
4120
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
91 |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
92 #endif |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
93 |
1869 | 94 return diff; |
95 } |