xref: /freebsd/lib/libc/string/wcscoll.c (revision d93a896e)
1 /*-
2  * Copyright 2017 Nexenta Systems, Inc.
3  * Copyright (c) 2002 Tim J. Robbins
4  * All rights reserved.
5  *
6  * Copyright (c) 2011 The FreeBSD Foundation
7  * All rights reserved.
8  * Portions of this software were developed by David Chisnall
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <wchar.h>
40 #include "collate.h"
41 
42 int
43 wcscoll_l(const wchar_t *ws1, const wchar_t *ws2, locale_t locale)
44 {
45 	int len1, len2, pri1, pri2;
46 	wchar_t *tr1 = NULL, *tr2 = NULL;
47 	int direc, pass;
48 	int ret = wcscmp(ws1, ws2);
49 
50 	FIX_LOCALE(locale);
51 	struct xlocale_collate *table =
52 		(struct xlocale_collate*)locale->components[XLC_COLLATE];
53 
54 	if (table->__collate_load_error || ret == 0)
55 		return (ret);
56 
57 	if (*ws1 == 0 && *ws2 != 0)
58 		return (-1);
59 	if (*ws1 != 0 && *ws2 == 0)
60 		return (1);
61 
62 	/*
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.
70 	 *
71 	 * Note that we do one final extra pass at the end to pick
72 	 * up UNDEFINED elements.  There is special handling for them.
73 	 */
74 	for (pass = 0; pass <= table->info->directive_count; pass++) {
75 
76 		const int32_t *st1 = NULL;
77 		const int32_t *st2 = NULL;
78 		const wchar_t	*w1 = ws1;
79 		const wchar_t	*w2 = ws2;
80 
81 		/* special pass for UNDEFINED */
82 		if (pass == table->info->directive_count) {
83 			direc = DIRECTIVE_FORWARD;
84 		} else {
85 			direc = table->info->directive[pass];
86 		}
87 
88 		if (direc & DIRECTIVE_BACKWARD) {
89 			wchar_t *bp, *fp, c;
90 			free(tr1);
91 			if ((tr1 = wcsdup(w1)) == NULL)
92 				goto end;
93 			bp = tr1;
94 			fp = tr1 + wcslen(tr1) - 1;
95 			while (bp < fp) {
96 				c = *bp;
97 				*bp++ = *fp;
98 				*fp-- = c;
99 			}
100 			free(tr2);
101 			if ((tr2 = wcsdup(w2)) == NULL)
102 				goto end;
103 			bp = tr2;
104 			fp = tr2 + wcslen(tr2) - 1;
105 			while (bp < fp) {
106 				c = *bp;
107 				*bp++ = *fp;
108 				*fp-- = c;
109 			}
110 			w1 = tr1;
111 			w2 = tr2;
112 		}
113 
114 		if (direc & DIRECTIVE_POSITION) {
115 			int check1, check2;
116 			while (*w1 && *w2) {
117 				pri1 = pri2 = 0;
118 				check1 = check2 = 1;
119 				while ((pri1 == pri2) && (check1 || check2)) {
120 					if (check1) {
121 						_collate_lookup(table, w1, &len1,
122 						    &pri1, pass, &st1);
123 						if (pri1 < 0) {
124 							errno = EINVAL;
125 							goto end;
126 						}
127 						if (!pri1) {
128 							pri1 = COLLATE_MAX_PRIORITY;
129 							st1 = NULL;
130 						}
131 						check1 = (st1 != NULL);
132 					}
133 					if (check2) {
134 						_collate_lookup(table, w2, &len2,
135 						    &pri2, pass, &st2);
136 						if (pri2 < 0) {
137 							errno = EINVAL;
138 							goto end;
139 						}
140 						if (!pri2) {
141 							pri2 = COLLATE_MAX_PRIORITY;
142 							st2 = NULL;
143 						}
144 						check2 = (st2 != NULL);
145 					}
146 				}
147 				if (pri1 != pri2) {
148 					ret = pri1 - pri2;
149 					goto end;
150 				}
151 				w1 += len1;
152 				w2 += len2;
153 			}
154 			if (!*w1) {
155 				if (*w2) {
156 					ret = -(int)*w2;
157 					goto end;
158 				}
159 			} else {
160 				ret = *w1;
161 				goto end;
162 			}
163 		} else {
164 			int vpri1 = 0, vpri2 = 0;
165 			while (*w1 || *w2 || st1 || st2) {
166 				pri1 = 1;
167 				while (*w1 || st1) {
168 					_collate_lookup(table, w1, &len1, &pri1,
169 					    pass, &st1);
170 					w1 += len1;
171 					if (pri1 > 0) {
172 						vpri1++;
173 						break;
174 					}
175 
176 					if (pri1 < 0) {
177 						errno = EINVAL;
178 						goto end;
179 					}
180 					st1 = NULL;
181 				}
182 				pri2 = 1;
183 				while (*w2 || st2) {
184 					_collate_lookup(table, w2, &len2, &pri2,
185 					    pass, &st2);
186 					w2 += len2;
187 					if (pri2 > 0) {
188 						vpri2++;
189 						break;
190 					}
191 					if (pri2 < 0) {
192 						errno = EINVAL;
193 						goto end;
194 					}
195 					st2 = NULL;
196 				}
197 				if ((!pri1 || !pri2) && (vpri1 == vpri2))
198 					break;
199 				if (pri1 != pri2) {
200 					ret = pri1 - pri2;
201 					goto end;
202 				}
203 			}
204 			if (vpri1 && !vpri2) {
205 				ret = 1;
206 				goto end;
207 			}
208 			if (!vpri1 && vpri2) {
209 				ret = -1;
210 				goto end;
211 			}
212 		}
213 	}
214 	ret = 0;
215 
216 end:
217 	free(tr1);
218 	free(tr2);
219 
220 	return (ret);
221 }
222 
223 int
224 wcscoll(const wchar_t *ws1, const wchar_t *ws2)
225 {
226 	return wcscoll_l(ws1, ws2, __get_locale());
227 }
228