annotate lib/memcoll.c @ 7302:8a1a9361108c

* _fpending.c: Include <config.h> unconditionally, since we no longer worry about uses that don't define HAVE_CONFIG_H. * acl.c, alloca.c, argmatch.c, atexit.c, backupfile.c: * basename.c, c-stack.c, c-strtod.c, calloc.c, canon-host.c: * canonicalize.c, chdir-long.c, chdir-safer.c, chown.c: * cloexec.c, close-stream.c, closeout.c, creat-safer.c: * cycle-check.c, diacrit.c, dirchownmod.c, dirfd.c, dirname.c: * dup-safer.c, dup2.c, error.c, euidaccess.c, exclude.c: * exitfail.c, fchmodat.c, fchown-stub.c, fd-safer.c: * file-type.c, fileblocks.c, filemode.c, filenamecat.c: * fnmatch.c, fopen-safer.c, fprintftime.c, free.c, fsusage.c: * ftruncate.c, fts-cycle.c, fts.c, full-write.c, gai_strerror.c: * getcwd.c, getdate.y, getdomainname.c, getgroups.c: * gethostname.c, gethrxtime.c, getloadavg.c, getlogin_r.c: * getndelim2.c, getnline.c, getopt.c, getopt1.c, getpass.c: * gettime.c, gettimeofday.c, getugroups.c, getusershell.c: * glob.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c: * human.c, idcache.c, inet_ntop.c, inet_pton.c, inttostr.c: * isdir.c, lchown.c, linebuffer.c, long-options.c, lstat.c: * malloc.c, md5.c, memcasecmp.c, memchr.c, memcmp.c, memcoll.c: * memcpy.c, memmove.c, memrchr.c, mkancesdirs.c, mkdir-p.c: * mkdir.c, mkdirat.c, mkstemp-safer.c, mkstemp.c, modechange.c: * mountlist.c, nanosleep.c, obstack.c, open-safer.c: * openat-die.c, openat.c, pagealign_alloc.c, physmem.c: * pipe-safer.c, posixtm.c, posixver.c, putenv.c, quote.c: * quotearg.c, raise.c, readtokens.c, readtokens0.c, readutmp.c: * realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c: * same.c, save-cwd.c, savedir.c, setenv.c, settime.c, sha1.c: * sig2str.c, snprintf.c, strdup.c, strerror.c, strftime.c: * stripslash.c, strndup.c, strnlen.c, strpbrk.c, strtod.c: * strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c: * timegm.c, tmpfile-safer.c, unlinkdir.c, userspec.c, utime.c: * utimecmp.c, utimens.c, version-etc-fsf.c, version-etc.c: * xalloc-die.c, xgetcwd.c, xgethostname.c, xmalloc.c: * xmemcoll.c, xnanosleep.c, xreadlink.c, xstrtod.c: * xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c: Likewise.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 13 Sep 2006 22:38:14 +0000
parents bf3b976f47a7
children bbbbbf4cd1c5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1869
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
Jim Meyering <jim@meyering.net>
parents:
diff changeset
4
Jim Meyering <jim@meyering.net>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
Jim Meyering <jim@meyering.net>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
Jim Meyering <jim@meyering.net>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
Jim Meyering <jim@meyering.net>
parents:
diff changeset
8 any later version.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
9
Jim Meyering <jim@meyering.net>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
Jim Meyering <jim@meyering.net>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
Jim Meyering <jim@meyering.net>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Jim Meyering <jim@meyering.net>
parents:
diff changeset
13 GNU General Public License for more details.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
14
Jim Meyering <jim@meyering.net>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
Jim Meyering <jim@meyering.net>
parents:
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
5848
a48fb0e98c8c *** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
1869
Jim Meyering <jim@meyering.net>
parents:
diff changeset
18
Jim Meyering <jim@meyering.net>
parents:
diff changeset
19 /* Contributed by Paul Eggert <eggert@twinsun.com>. */
Jim Meyering <jim@meyering.net>
parents:
diff changeset
20
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 7149
diff changeset
21 #include <config.h>
1869
Jim Meyering <jim@meyering.net>
parents:
diff changeset
22
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
23 #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
24
3708
bd719c800d4b Include errno.h, and declare errno if not defined.
Jim Meyering <jim@meyering.net>
parents: 2997
diff changeset
25 #include <errno.h>
4664
42147e1c0cee Remove K&R cruft.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4464
diff changeset
26 #include <string.h>
1869
Jim Meyering <jim@meyering.net>
parents:
diff changeset
27
Jim Meyering <jim@meyering.net>
parents:
diff changeset
28 /* 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
29 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
30 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
31 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
32 error number if there is an error, and to zero otherwise. */
1869
Jim Meyering <jim@meyering.net>
parents:
diff changeset
33 int
Jim Meyering <jim@meyering.net>
parents:
diff changeset
34 memcoll (char *s1, size_t s1len, char *s2, size_t s2len)
Jim Meyering <jim@meyering.net>
parents:
diff changeset
35 {
Jim Meyering <jim@meyering.net>
parents:
diff changeset
36 int diff;
4120
77d7a6d46166 (memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents: 4104
diff changeset
37
77d7a6d46166 (memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents: 4104
diff changeset
38 #if HAVE_STRCOLL
77d7a6d46166 (memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents: 4104
diff changeset
39
7147
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
40 /* 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
41 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
42 the buffers using strcoll on each substring. */
1869
Jim Meyering <jim@meyering.net>
parents:
diff changeset
43
7147
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
44 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
45 {
bf3b976f47a7 (memcoll): Set errno = 0 in the shortcut case, too.
Paul Eggert <eggert@cs.ucla.edu>
parents: 7147
diff changeset
46 errno = 0;
bf3b976f47a7 (memcoll): Set errno = 0 in the shortcut case, too.
Paul Eggert <eggert@cs.ucla.edu>
parents: 7147
diff changeset
47 diff = 0;
bf3b976f47a7 (memcoll): Set errno = 0 in the shortcut case, too.
Paul Eggert <eggert@cs.ucla.edu>
parents: 7147
diff changeset
48 }
7147
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
49 else
1869
Jim Meyering <jim@meyering.net>
parents:
diff changeset
50 {
7147
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
51 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
52 char n2 = s2[s2len];
1869
Jim Meyering <jim@meyering.net>
parents:
diff changeset
53
7147
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
54 s1[s1len++] = '\0';
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
55 s2[s2len++] = '\0';
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
56
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
57 while (! (errno = 0, (diff = strcoll (s1, s2)) || errno))
1869
Jim Meyering <jim@meyering.net>
parents:
diff changeset
58 {
7147
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
59 /* 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
60 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
61 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
62 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
63 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
64 s1 += size1;
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
65 s2 += size2;
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
66 s1len -= size1;
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
67 s2len -= size2;
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
68
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
69 if (s1len == 0)
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
70 {
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
71 if (s2len != 0)
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
72 diff = -1;
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
73 break;
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
74 }
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
75 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
76 {
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
77 diff = 1;
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
78 break;
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
79 }
1869
Jim Meyering <jim@meyering.net>
parents:
diff changeset
80 }
7147
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
81
1388e7363d86 * memcoll.c (memcoll): Optimize for the common case where the
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
82 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
83 s2[s2len - 1] = n2;
1869
Jim Meyering <jim@meyering.net>
parents:
diff changeset
84 }
Jim Meyering <jim@meyering.net>
parents:
diff changeset
85
4120
77d7a6d46166 (memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents: 4104
diff changeset
86 #else
77d7a6d46166 (memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents: 4104
diff changeset
87
77d7a6d46166 (memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents: 4104
diff changeset
88 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
89 if (! diff)
77d7a6d46166 (memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents: 4104
diff changeset
90 diff = s1len < s2len ? -1 : s1len != s2len;
4464
580114b1a865 (memcoll) [!HAVE_STRCOLL]: Clear errno.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4347
diff changeset
91 errno = 0;
4120
77d7a6d46166 (memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents: 4104
diff changeset
92
77d7a6d46166 (memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents: 4104
diff changeset
93 #endif
77d7a6d46166 (memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents: 4104
diff changeset
94
1869
Jim Meyering <jim@meyering.net>
parents:
diff changeset
95 return diff;
Jim Meyering <jim@meyering.net>
parents:
diff changeset
96 }