Update.
[glibc.git] / string / strcoll.c
blob8457ef8df1e4c18a45a8885a55b22a7befd3c1e2
1 /* Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <endian.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #ifndef STRING_TYPE
27 # define STRING_TYPE char
28 # define USTRING_TYPE unsigned char
29 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
30 # define STRCOLL __strcoll_l
31 # else
32 # define STRCOLL strcoll
33 # endif
34 # define STRCMP strcmp
35 #endif
37 /* Include the shared helper functions. `strxfrm'/`wcsxfrm' also use
38 these functions. */
39 #include "../locale/weight.h"
42 /* Compare S1 and S2, returning less than, equal to or
43 greater than zero if the collated form of S1 is lexicographically
44 less than, equal to or greater than the collated form of S2. */
45 #ifndef USE_IN_EXTENDED_LOCALE_MODEL
46 int
47 STRCOLL (s1, s2)
48 const STRING_TYPE *s1;
49 const STRING_TYPE *s2;
50 #else
51 int
52 STRCOLL (s1, s2, l)
53 const STRING_TYPE *s1;
54 const STRING_TYPE *s2;
55 __locale_t l;
56 #endif
58 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
59 struct locale_data *current = l->__locales[LC_COLLATE];
60 # if BYTE_ORDER == BIG_ENDIAN
61 const uint32_t *collate_table = (const uint32_t *)
62 current->values[_NL_ITEM_INDEX (_NL_COLLATE_TABLE_EB)].string;
63 const uint32_t *collate_extra = (const uint32_t *)
64 current->values[_NL_ITEM_INDEX (_NL_COLLATE_EXTRA_EB)].string;
65 # elif BYTE_ORDER == LITTLE_ENDIAN
66 const uint32_t *collate_table = (const uint32_t *)
67 current->values[_NL_ITEM_INDEX (_NL_COLLATE_TABLE_EL)].string;
68 const uint32_t *collate_extra = (const uint32_t *)
69 current->values[_NL_ITEM_INDEX (_NL_COLLATE_EXTRA_EL)].string;
70 # else
71 # error bizarre byte order
72 # endif
73 #endif
74 weight_t *s1forw = NULL;
75 weight_t *s1backw = NULL;
76 weight_t *s2forw = NULL;
77 weight_t *s2backw = NULL;
78 size_t pass;
80 /* If the current locale does not specify locale data we use normal
81 8-bit string comparison. */
82 if (collate_nrules == 0)
83 return STRCMP (s1, s2);
85 /* Handle empty strings as a special case. */
86 if (*s1 == '\0')
87 return *s2 == '\0' ? 0 : -1;
88 else if (*s2 == '\0')
89 return 1;
91 /* Get full information about the strings. This means we get
92 information for all passes in a special data structure. */
93 get_string (s1, s1forw, s1backw);
94 get_string (s2, s2forw, s2backw);
96 /* Now we have all the information. In at most the given number of
97 passes we can finally decide about the order. */
98 for (pass = 0; pass < collate_nrules; ++pass)
100 int forward = (collate_rules[pass] & sort_forward) != 0;
101 const weight_t *s1run = forward ? s1forw : s1backw;
102 const weight_t *s2run = forward ? s2forw : s2backw;
103 int s1idx = forward ? 0 : s1run->data[pass].number - 1;
104 int s2idx = forward ? 0 : s2run->data[pass].number - 1;
106 while (1)
108 int s1ignore = 0;
109 int s2ignore = 0;
110 uint32_t w1 = 0;
111 uint32_t w2 = 0;
113 /* Here we have to check for IGNORE entries. If these are
114 found we count them and go on with the next value. */
115 while (s1run != NULL
116 && ((w1 = s1run->data[pass].value[s1idx])
117 == (uint32_t) IGNORE_CHAR))
119 ++s1ignore;
120 if ((forward && ++s1idx >= s1run->data[pass].number)
121 || (!forward && --s1idx < 0))
123 weight_t *nextp = forward ? s1run->next : s1run->prev;
124 if (nextp == NULL)
126 w1 = 0;
127 /* No more non-INGOREd elements means lowest
128 possible value. */
129 s1ignore = -1;
131 else
132 s1idx = forward ? 0 : nextp->data[pass].number - 1;
133 s1run = nextp;
137 while (s2run != NULL
138 && ((w2 = s2run->data[pass].value[s2idx])
139 == (uint32_t) IGNORE_CHAR))
141 ++s2ignore;
142 if ((forward && ++s2idx >= s2run->data[pass].number)
143 || (!forward && --s2idx < 0))
145 weight_t *nextp = forward ? s2run->next : s2run->prev;
146 if (nextp == NULL)
148 w2 = 0;
149 /* No more non-INGOREd elements means lowest
150 possible value. */
151 s2ignore = -1;
153 else
154 s2idx = forward ? 0 : nextp->data[pass].number - 1;
155 s2run = nextp;
159 /* If one string is completely processed stop. */
160 if (s1run == NULL || s2run == NULL)
161 break;
163 /* Now we have information of the number of ignored
164 weights and the value of the next weight. */
165 if ((collate_rules[pass] & sort_position) != 0
166 && s1ignore != s2ignore && (w1 != 0 || w2 != 0))
167 return s1ignore < s2ignore ? -1 : 1;
169 if (w1 != w2)
170 return w1 < w2 ? -1 : 1;
172 /* We have to increment the index counters. */
173 if ((forward && ++s1idx >= s1run->data[pass].number)
174 || (!forward && --s1idx < 0))
175 if (forward)
177 s1run = s1run->next;
178 s1idx = 0;
180 else
182 s1run = s1run->prev;
183 if (s1run != NULL)
184 s1idx = s1run->data[pass].number - 1;
187 if ((forward && ++s2idx >= s2run->data[pass].number)
188 || (!forward && --s2idx < 0))
189 if (forward)
191 s2run = s2run->next;
192 s2idx = 0;
194 else
196 s2run = s2run->prev;
197 if (s2run != NULL)
198 s2idx = s2run->data[pass].number - 1;
203 if (s1run != s2run)
204 return s1run != NULL ? 1 : -1;
207 return 0;