Line data Source code
1 : /* Locale-specific memory comparison.
2 :
3 : Copyright (C) 1999, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
4 :
5 : This program is free software: you can redistribute it and/or modify
6 : it under the terms of the GNU General Public License as published by
7 : the Free Software Foundation; either version 3 of the License, or
8 : (at your option) any later version.
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
16 : along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 :
18 : /* Contributed by Paul Eggert <eggert@twinsun.com>. */
19 :
20 : #include <config.h>
21 :
22 : #include "memcoll.h"
23 :
24 : #include <errno.h>
25 : #include <string.h>
26 :
27 : /* Compare S1 (with length S1LEN) and S2 (with length S2LEN) according
28 : to the LC_COLLATE locale. S1 and S2 do not overlap, and are not
29 : adjacent. Perhaps temporarily modify the bytes after S1 and S2,
30 : but restore their original contents before returning. Set errno to an
31 : error number if there is an error, and to zero otherwise. */
32 : int
33 0 : memcoll (char *s1, size_t s1len, char *s2, size_t s2len)
34 : {
35 : int diff;
36 :
37 : #if HAVE_STRCOLL
38 :
39 : /* strcoll is slow on many platforms, so check for the common case
40 : where the arguments are bytewise equal. Otherwise, walk through
41 : the buffers using strcoll on each substring. */
42 :
43 0 : if (s1len == s2len && memcmp (s1, s2, s1len) == 0)
44 : {
45 0 : errno = 0;
46 0 : diff = 0;
47 : }
48 : else
49 : {
50 0 : char n1 = s1[s1len];
51 0 : char n2 = s2[s2len];
52 :
53 0 : s1[s1len++] = '\0';
54 0 : s2[s2len++] = '\0';
55 :
56 0 : while (! (errno = 0, (diff = strcoll (s1, s2)) || errno))
57 : {
58 : /* strcoll found no difference, but perhaps it was fooled by NUL
59 : characters in the data. Work around this problem by advancing
60 : past the NUL chars. */
61 0 : size_t size1 = strlen (s1) + 1;
62 0 : size_t size2 = strlen (s2) + 1;
63 0 : s1 += size1;
64 0 : s2 += size2;
65 0 : s1len -= size1;
66 0 : s2len -= size2;
67 :
68 0 : if (s1len == 0)
69 : {
70 0 : if (s2len != 0)
71 0 : diff = -1;
72 0 : break;
73 : }
74 0 : else if (s2len == 0)
75 : {
76 0 : diff = 1;
77 0 : break;
78 : }
79 : }
80 :
81 0 : s1[s1len - 1] = n1;
82 0 : s2[s2len - 1] = n2;
83 : }
84 :
85 : #else
86 :
87 : diff = memcmp (s1, s2, s1len < s2len ? s1len : s2len);
88 : if (! diff)
89 : diff = s1len < s2len ? -1 : s1len != s2len;
90 : errno = 0;
91 :
92 : #endif
93 :
94 0 : return diff;
95 : }
|