dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libc / port / locale / wcscoll.c
blob5fe23dde77e6d31edbfa4e25fb401c5127518f0b
1 /*
2 * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4 * Copyright (c) 2002 Tim J. Robbins
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include "lint.h"
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <wchar.h>
34 #include <assert.h>
35 #include "collate.h"
36 #include "localeimpl.h"
38 int
39 wcscoll_l(const wchar_t *ws1, const wchar_t *ws2, locale_t loc)
41 int len1, len2, pri1, pri2, ret;
42 wchar_t *tr1 = NULL, *tr2 = NULL;
43 int direc, pass;
44 const struct lc_collate *lcc = loc->collate;
46 if (lcc->lc_is_posix)
48 * Locale has no special collating order or could not be
49 * loaded, do a fast binary comparison.
51 return (wcscmp(ws1, ws2));
53 ret = 0;
56 * Once upon a time we had code to try to optimize this, but
57 * it turns out that you really can't make many assumptions
58 * safely. You absolutely have to run this pass by pass,
59 * because some passes will be ignored for a given character,
60 * while others will not. Simpler locales will benefit from
61 * having fewer passes, and most comparisons should resolve
62 * during the primary pass anyway.
64 * Note that we do one final extra pass at the end to pick
65 * up UNDEFINED elements. There is special handling for them.
67 for (pass = 0; pass <= lcc->lc_directive_count; pass++) {
69 const int32_t *st1 = NULL;
70 const int32_t *st2 = NULL;
71 const wchar_t *w1 = ws1;
72 const wchar_t *w2 = ws2;
73 int check1, check2;
75 /* special pass for UNDEFINED */
76 if (pass == lcc->lc_directive_count) {
77 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
78 } else {
79 direc = lcc->lc_directive[pass];
82 if (direc & DIRECTIVE_BACKWARD) {
83 wchar_t *bp, *fp, c;
84 if ((tr1 = wcsdup(w1)) == NULL)
85 goto fail;
86 bp = tr1;
87 fp = tr1 + wcslen(tr1) - 1;
88 while (bp < fp) {
89 c = *bp;
90 *bp++ = *fp;
91 *fp-- = c;
93 if ((tr2 = wcsdup(w2)) == NULL)
94 goto fail;
95 bp = tr2;
96 fp = tr2 + wcslen(tr2) - 1;
97 while (bp < fp) {
98 c = *bp;
99 *bp++ = *fp;
100 *fp-- = c;
102 w1 = tr1;
103 w2 = tr2;
106 if (direc & DIRECTIVE_POSITION) {
107 while (*w1 && *w2) {
108 pri1 = pri2 = 0;
109 check1 = check2 = 1;
110 while ((pri1 == pri2) && (check1 || check2)) {
111 if (check1) {
112 _collate_lookup(lcc, w1, &len1,
113 &pri1, pass, &st1);
114 if (pri1 < 0) {
115 errno = EINVAL;
116 goto fail;
118 if (!pri1) {
119 pri1 = COLLATE_MAX_PRIORITY;
120 st1 = NULL;
122 check1 = (st1 != NULL);
124 if (check2) {
125 _collate_lookup(lcc, w2, &len2,
126 &pri2, pass, &st2);
127 if (pri2 < 0) {
128 errno = EINVAL;
129 goto fail;
131 if (!pri2) {
132 pri2 = COLLATE_MAX_PRIORITY;
133 st2 = NULL;
135 check2 = (st2 != NULL);
138 if (pri1 != pri2) {
139 ret = pri1 - pri2;
140 goto end;
142 w1 += len1;
143 w2 += len2;
145 } else {
146 while (*w1 && *w2) {
147 pri1 = pri2 = 0;
148 check1 = check2 = 1;
149 while ((pri1 == pri2) && (check1 || check2)) {
150 while (check1 && *w1) {
151 _collate_lookup(lcc, w1, &len1,
152 &pri1, pass, &st1);
153 if (pri1 > 0)
154 break;
155 if (pri1 < 0) {
156 errno = EINVAL;
157 goto fail;
159 st1 = NULL;
160 w1 += 1;
162 check1 = (st1 != NULL);
163 while (check2 && *w2) {
164 _collate_lookup(lcc, w2, &len2,
165 &pri2, pass, &st2);
166 if (pri2 > 0)
167 break;
168 if (pri2 < 0) {
169 errno = EINVAL;
170 goto fail;
172 st2 = NULL;
173 w2 += 1;
175 check2 = (st2 != NULL);
176 if (!pri1 || !pri2)
177 break;
179 if (!pri1 || !pri2)
180 break;
181 if (pri1 != pri2) {
182 ret = pri1 - pri2;
183 goto end;
185 w1 += len1;
186 w2 += len2;
189 if (!*w1) {
190 if (*w2) {
191 ret = -(int)*w2;
192 goto end;
194 } else {
195 ret = *w1;
196 goto end;
199 ret = 0;
201 end:
202 free(tr1);
203 free(tr2);
205 return (ret);
207 fail:
208 ret = wcscmp(ws1, ws2);
209 goto end;
214 wcscoll(const wchar_t *ws1, const wchar_t *ws2)
216 return (wcscoll_l(ws1, ws2, uselocale(NULL)));