]> git.saurik.com Git - apple/libc.git/blob - string/wcscoll-fbsd.c
ce09e41fd9d2f68518e58d6326f1c1c435d8c31a
[apple/libc.git] / string / wcscoll-fbsd.c
1 /*-
2 * Copyright (c) 2002 Tim J. Robbins
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: src/lib/libc/string/wcscoll.c,v 1.3 2004/04/07 09:47:56 tjr Exp $");
29
30 #include "xlocale_private.h"
31
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <wchar.h>
36 #include "collate.h"
37
38 #define NOTFORWARD (DIRECTIVE_BACKWARD | DIRECTIVE_POSITION)
39
40 int
41 wcscoll_l(const wchar_t *ws1, const wchar_t *ws2, locale_t loc)
42 {
43 int sverrno;
44 int len, len2, prim, prim2, sec, sec2, ret, ret2;
45 const wchar_t *t, *t2;
46 wchar_t *tt = NULL, *tt2 = NULL;
47 wchar_t *tr = NULL, *tr2 = NULL;
48 wchar_t w, w2;
49 struct __collate_st_info *info;
50
51 NORMALIZE_LOCALE(loc);
52 if (loc->__collate_load_error)
53 /*
54 * Locale has no special collating order or could not be
55 * loaded, do a fast binary comparison.
56 */
57 return (wcscmp(ws1, ws2));
58
59 info = &loc->__lc_collate->__info;
60 len = len2 = 1;
61 ret = ret2 = 0;
62
63 if ((info->directive[0] & NOTFORWARD) ||
64 (info->directive[1] & NOTFORWARD) ||
65 (!(info->flags && COLLATE_SUBST_DUP) &&
66 (info->subst_count[0] > 0 || info->subst_count[1] > 0))) {
67 int direc, pass;
68 for(pass = 0; pass < info->directive_count; pass++) {
69 direc = info->directive[pass];
70 if (pass == 0 || !(info->flags & COLLATE_SUBST_DUP)) {
71 free(tt);
72 tt = __collate_substitute(ws1, pass, loc);
73 free(tt2);
74 tt2 = tt ? __collate_substitute(ws2, pass, loc) : NULL;
75 }
76 if (direc & DIRECTIVE_BACKWARD) {
77 wchar_t *bp, *fp, c;
78 tr = __collate_wcsdup(tt ? tt : ws1);
79 bp = tr;
80 fp = tr + wcslen(tr) - 1;
81 while(bp < fp) {
82 c = *bp;
83 *bp++ = *fp;
84 *fp-- = c;
85 }
86 tr2 = __collate_wcsdup(tt2 ? tt2 : ws2);
87 bp = tr2;
88 fp = tr2 + wcslen(tr2) - 1;
89 while(bp < fp) {
90 c = *bp;
91 *bp++ = *fp;
92 *fp-- = c;
93 }
94 t = (const wchar_t *)tr;
95 t2 = (const wchar_t *)tr2;
96 } else if (tt) {
97 t = (const wchar_t *)tt;
98 t2 = (const wchar_t *)tt2;
99 } else {
100 t = (const wchar_t *)ws1;
101 t2 = (const wchar_t *)ws2;
102 }
103 if(direc & DIRECTIVE_POSITION) {
104 while(*t && *t2) {
105 prim = prim2 = 0;
106 __collate_lookup_which(t, &len, &prim, pass, loc);
107 if (prim <= 0) {
108 if (prim < 0) {
109 errno = EINVAL;
110 ret = -1;
111 goto end;
112 }
113 prim = COLLATE_MAX_PRIORITY;
114 }
115 __collate_lookup_which(t2, &len2, &prim2, pass, loc);
116 if (prim2 <= 0) {
117 if (prim2 < 0) {
118 errno = EINVAL;
119 ret = -1;
120 goto end;
121 }
122 prim2 = COLLATE_MAX_PRIORITY;
123 }
124 if(prim != prim2) {
125 ret = prim - prim2;
126 goto end;
127 }
128 t += len;
129 t2 += len2;
130 }
131 } else {
132 while(*t && *t2) {
133 prim = prim2 = 0;
134 while(*t) {
135 __collate_lookup_which(t, &len, &prim, pass, loc);
136 if(prim > 0)
137 break;
138 if (prim < 0) {
139 errno = EINVAL;
140 ret = -1;
141 goto end;
142 }
143 t += len;
144 }
145 while(*t2) {
146 __collate_lookup_which(t2, &len2, &prim2, pass, loc);
147 if(prim2 > 0)
148 break;
149 if (prim2 < 0) {
150 errno = EINVAL;
151 ret = -1;
152 goto end;
153 }
154 t2 += len2;
155 }
156 if(!prim || !prim2)
157 break;
158 if(prim != prim2) {
159 ret = prim - prim2;
160 goto end;
161 }
162 t += len;
163 t2 += len2;
164 }
165 }
166 if(!*t) {
167 if(*t2) {
168 ret = -(int)*t2;
169 goto end;
170 }
171 } else {
172 ret = *t;
173 goto end;
174 }
175 }
176 ret = 0;
177 goto end;
178 }
179
180 /* optimized common case: order_start forward;forward and duplicate
181 * (or no) substitute tables */
182 tt = __collate_substitute(ws1, 0, loc);
183 if (tt == NULL) {
184 tt2 = NULL;
185 t = (const wchar_t *)ws1;
186 t2 = (const wchar_t *)ws2;
187 } else {
188 tt2 = __collate_substitute(ws2, 0, loc);
189 t = (const wchar_t *)tt;
190 t2 = (const wchar_t *)tt2;
191 }
192 while(*t && *t2) {
193 prim = prim2 = 0;
194 while(*t) {
195 __collate_lookup_l(t, &len, &prim, &sec, loc);
196 if (prim > 0)
197 break;
198 if (prim < 0) {
199 errno = EINVAL;
200 ret = -1;
201 goto end;
202 }
203 t += len;
204 }
205 while(*t2) {
206 __collate_lookup_l(t2, &len2, &prim2, &sec2, loc);
207 if (prim2 > 0)
208 break;
209 if (prim2 < 0) {
210 errno = EINVAL;
211 ret = -1;
212 goto end;
213 }
214 t2 += len2;
215 }
216 if(!prim || !prim2)
217 break;
218 if(prim != prim2) {
219 ret = prim - prim2;
220 goto end;
221 }
222 if(!ret2)
223 ret2 = sec - sec2;
224 t += len;
225 t2 += len2;
226 }
227 if(!*t && *t2)
228 ret = -(int)*t2;
229 else if(*t && !*t2)
230 ret = *t;
231 else if(!*t && !*t2)
232 ret = ret2;
233 end:
234 sverrno = errno;
235 free(tt);
236 free(tt2);
237 free(tr);
238 free(tr2);
239 errno = sverrno;
240
241 return ret;
242 }
243
244 int
245 wcscoll(const wchar_t *ws1, const wchar_t *ws2)
246 {
247 return wcscoll_l(ws1, ws2, __current_locale());
248 }