2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2017 Nexenta Systems, Inc.
5 * Copyright (c) 2002 Tim J. Robbins
8 * Copyright (c) 2011 The FreeBSD Foundation
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
45 wcscoll_l(const wchar_t *ws1, const wchar_t *ws2, locale_t locale)
47 int len1, len2, pri1, pri2;
48 wchar_t *tr1 = NULL, *tr2 = NULL;
50 int ret = wcscmp(ws1, ws2);
53 struct xlocale_collate *table =
54 (struct xlocale_collate*)locale->components[XLC_COLLATE];
56 if (table->__collate_load_error || ret == 0)
59 if (*ws1 == 0 && *ws2 != 0)
61 if (*ws1 != 0 && *ws2 == 0)
65 * Once upon a time we had code to try to optimize this, but
66 * it turns out that you really can't make many assumptions
67 * safely. You absolutely have to run this pass by pass,
68 * because some passes will be ignored for a given character,
69 * while others will not. Simpler locales will benefit from
70 * having fewer passes, and most comparisons should resolve
71 * during the primary pass anyway.
73 * Note that we do one final extra pass at the end to pick
74 * up UNDEFINED elements. There is special handling for them.
76 for (pass = 0; pass <= table->info->directive_count; pass++) {
78 const int32_t *st1 = NULL;
79 const int32_t *st2 = NULL;
80 const wchar_t *w1 = ws1;
81 const wchar_t *w2 = ws2;
83 /* special pass for UNDEFINED */
84 if (pass == table->info->directive_count) {
85 direc = DIRECTIVE_FORWARD;
87 direc = table->info->directive[pass];
90 if (direc & DIRECTIVE_BACKWARD) {
93 if ((tr1 = wcsdup(w1)) == NULL)
96 fp = tr1 + wcslen(tr1) - 1;
103 if ((tr2 = wcsdup(w2)) == NULL)
106 fp = tr2 + wcslen(tr2) - 1;
116 if (direc & DIRECTIVE_POSITION) {
121 while ((pri1 == pri2) && (check1 || check2)) {
123 _collate_lookup(table, w1, &len1,
130 pri1 = COLLATE_MAX_PRIORITY;
133 check1 = (st1 != NULL);
136 _collate_lookup(table, w2, &len2,
143 pri2 = COLLATE_MAX_PRIORITY;
146 check2 = (st2 != NULL);
166 int vpri1 = 0, vpri2 = 0;
167 while (*w1 || *w2 || st1 || st2) {
170 _collate_lookup(table, w1, &len1, &pri1,
186 _collate_lookup(table, w2, &len2, &pri2,
199 if ((!pri1 || !pri2) && (vpri1 == vpri2))
206 if (vpri1 && !vpri2) {
210 if (!vpri1 && vpri2) {
226 wcscoll(const wchar_t *ws1, const wchar_t *ws2)
228 return wcscoll_l(ws1, ws2, __get_locale());