2 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
3 * Copyright (c) 2002 Tim J. Robbins
6 * Copyright (c) 2011 The FreeBSD Foundation
8 * Portions of this software were developed by David Chisnall
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
43 wcscoll_l(const wchar_t *ws1, const wchar_t *ws2, locale_t locale)
45 int len1, len2, pri1, pri2, ret;
46 wchar_t *tr1 = NULL, *tr2 = NULL;
50 struct xlocale_collate *table =
51 (struct xlocale_collate*)locale->components[XLC_COLLATE];
53 if (table->__collate_load_error)
55 * Locale has no special collating order or could not be
56 * loaded, do a fast binary comparison.
58 return (wcscmp(ws1, ws2));
63 * Once upon a time we had code to try to optimize this, but
64 * it turns out that you really can't make many assumptions
65 * safely. You absolutely have to run this pass by pass,
66 * because some passes will be ignored for a given character,
67 * while others will not. Simpler locales will benefit from
68 * having fewer passes, and most comparisons should resolve
69 * during the primary pass anyway.
71 * Note that we do one final extra pass at the end to pick
72 * up UNDEFINED elements. There is special handling for them.
74 for (pass = 0; pass <= table->info->directive_count; pass++) {
76 const int32_t *st1 = NULL;
77 const int32_t *st2 = NULL;
78 const wchar_t *w1 = ws1;
79 const wchar_t *w2 = ws2;
82 /* special pass for UNDEFINED */
83 if (pass == table->info->directive_count) {
84 direc = DIRECTIVE_FORWARD | DIRECTIVE_UNDEFINED;
86 direc = table->info->directive[pass];
89 if (direc & DIRECTIVE_BACKWARD) {
91 if ((tr1 = wcsdup(w1)) == NULL)
94 fp = tr1 + wcslen(tr1) - 1;
100 if ((tr2 = wcsdup(w2)) == NULL)
103 fp = tr2 + wcslen(tr2) - 1;
113 if (direc & DIRECTIVE_POSITION) {
117 while ((pri1 == pri2) && (check1 || check2)) {
119 _collate_lookup(table, w1, &len1,
126 pri1 = COLLATE_MAX_PRIORITY;
129 check1 = (st1 != NULL);
132 _collate_lookup(table, w2, &len2,
139 pri2 = COLLATE_MAX_PRIORITY;
142 check2 = (st2 != NULL);
156 while ((pri1 == pri2) && (check1 || check2)) {
157 while (check1 && *w1) {
158 _collate_lookup(table, w1,
159 &len1, &pri1, pass, &st1);
169 check1 = (st1 != NULL);
170 while (check2 && *w2) {
171 _collate_lookup(table, w2,
172 &len2, &pri2, pass, &st2);
182 check2 = (st2 != NULL);
215 ret = wcscmp(ws1, ws2);
220 wcscoll(const wchar_t *ws1, const wchar_t *ws2)
222 return wcscoll_l(ws1, ws2, __get_locale());