xref: /freebsd/usr.bin/sort/bwstring.c (revision e39e854e27f53a784c3982cbeb68f4ad1cfd9162)
1 /*-
2  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
3  * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko@citrix.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <ctype.h>
32 #include <errno.h>
33 #include <err.h>
34 #include <langinfo.h>
35 #include <math.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <wchar.h>
39 #include <wctype.h>
40 
41 #include "bwstring.h"
42 #include "sort.h"
43 
44 bool byte_sort;
45 
46 static wchar_t **wmonths;
47 static unsigned char **cmonths;
48 
49 /* initialise months */
50 
51 void
52 initialise_months(void)
53 {
54 	const nl_item item[12] = { ABMON_1, ABMON_2, ABMON_3, ABMON_4,
55 	    ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10,
56 	    ABMON_11, ABMON_12 };
57 	unsigned char *tmp;
58 	size_t len;
59 
60 	if (MB_CUR_MAX == 1) {
61 		if (cmonths == NULL) {
62 			unsigned char *m;
63 
64 			cmonths = sort_malloc(sizeof(unsigned char*) * 12);
65 			for (int i = 0; i < 12; i++) {
66 				cmonths[i] = NULL;
67 				tmp = (unsigned char *) nl_langinfo(item[i]);
68 				if (tmp == NULL)
69 					continue;
70 				if (debug_sort)
71 					printf("month[%d]=%s\n", i, tmp);
72 				len = strlen(tmp);
73 				if (len < 1)
74 					continue;
75 				while (isblank(*tmp))
76 					++tmp;
77 				m = sort_malloc(len + 1);
78 				memcpy(m, tmp, len + 1);
79 				m[len] = '\0';
80 				for (unsigned int j = 0; j < len; j++)
81 					m[j] = toupper(m[j]);
82 				cmonths[i] = m;
83 			}
84 		}
85 
86 	} else {
87 		if (wmonths == NULL) {
88 			wchar_t *m;
89 
90 			wmonths = sort_malloc(sizeof(wchar_t *) * 12);
91 			for (int i = 0; i < 12; i++) {
92 				wmonths[i] = NULL;
93 				tmp = (unsigned char *) nl_langinfo(item[i]);
94 				if (tmp == NULL)
95 					continue;
96 				if (debug_sort)
97 					printf("month[%d]=%s\n", i, tmp);
98 				len = strlen(tmp);
99 				if (len < 1)
100 					continue;
101 				while (isblank(*tmp))
102 					++tmp;
103 				m = sort_malloc(SIZEOF_WCHAR_STRING(len + 1));
104 				if (mbstowcs(m, tmp, len) == ((size_t) -1))
105 					continue;
106 				m[len] = L'\0';
107 				for (unsigned int j = 0; j < len; j++)
108 					m[j] = towupper(m[j]);
109 				wmonths[i] = m;
110 			}
111 		}
112 	}
113 }
114 
115 /*
116  * Compare two wide-character strings
117  */
118 static int
119 wide_str_coll(const wchar_t *s1, const wchar_t *s2)
120 {
121 	int ret = 0;
122 
123 	errno = 0;
124 	ret = wcscoll(s1, s2);
125 	if (errno == EILSEQ) {
126 		errno = 0;
127 		ret = wcscmp(s1, s2);
128 		if (errno != 0) {
129 			for (size_t i = 0; ; ++i) {
130 				wchar_t c1 = s1[i];
131 				wchar_t c2 = s2[i];
132 				if (c1 == L'\0')
133 					return ((c2 == L'\0') ? 0 : -1);
134 				if (c2 == L'\0')
135 					return (+1);
136 				if (c1 == c2)
137 					continue;
138 				return ((int)(c1 - c2));
139 			}
140 		}
141 	}
142 	return (ret);
143 }
144 
145 /* counterparts of wcs functions */
146 
147 void
148 bwsprintf(FILE *f, struct bwstring *bws, const char *prefix, const char *suffix)
149 {
150 
151 	if (MB_CUR_MAX == 1)
152 		fprintf(f, "%s%s%s", prefix, bws->data.cstr, suffix);
153 	else
154 		fprintf(f, "%s%S%s", prefix, bws->data.wstr, suffix);
155 }
156 
157 const void* bwsrawdata(const struct bwstring *bws)
158 {
159 
160 	return (&(bws->data));
161 }
162 
163 size_t bwsrawlen(const struct bwstring *bws)
164 {
165 
166 	return ((MB_CUR_MAX == 1) ? bws->len : SIZEOF_WCHAR_STRING(bws->len));
167 }
168 
169 size_t
170 bws_memsize(const struct bwstring *bws)
171 {
172 
173 	return ((MB_CUR_MAX == 1) ? (bws->len + 2 + sizeof(struct bwstring)) :
174 	    (SIZEOF_WCHAR_STRING(bws->len + 1) + sizeof(struct bwstring)));
175 }
176 
177 void
178 bws_setlen(struct bwstring *bws, size_t newlen)
179 {
180 
181 	if (bws && newlen != bws->len && newlen <= bws->len) {
182 		bws->len = newlen;
183 		if (MB_CUR_MAX == 1)
184 			bws->data.cstr[newlen] = '\0';
185 		else
186 			bws->data.wstr[newlen] = L'\0';
187 	}
188 }
189 
190 /*
191  * Allocate a new binary string of specified size
192  */
193 struct bwstring *
194 bwsalloc(size_t sz)
195 {
196 	struct bwstring *ret;
197 
198 	if (MB_CUR_MAX == 1)
199 		ret = sort_malloc(sizeof(struct bwstring) + 1 + sz);
200 	else
201 		ret = sort_malloc(sizeof(struct bwstring) +
202 		    SIZEOF_WCHAR_STRING(sz + 1));
203 	ret->len = sz;
204 
205 	if (MB_CUR_MAX == 1)
206 		ret->data.cstr[ret->len] = '\0';
207 	else
208 		ret->data.wstr[ret->len] = L'\0';
209 
210 	return (ret);
211 }
212 
213 /*
214  * Create a copy of binary string.
215  * New string size equals the length of the old string.
216  */
217 struct bwstring *
218 bwsdup(const struct bwstring *s)
219 {
220 
221 	if (s == NULL)
222 		return (NULL);
223 	else {
224 		struct bwstring *ret = bwsalloc(s->len);
225 
226 		if (MB_CUR_MAX == 1)
227 			memcpy(ret->data.cstr, s->data.cstr, (s->len));
228 		else
229 			memcpy(ret->data.wstr, s->data.wstr,
230 			    SIZEOF_WCHAR_STRING(s->len));
231 
232 		return (ret);
233 	}
234 }
235 
236 /*
237  * Create a new binary string from a raw binary buffer.
238  */
239 struct bwstring *
240 bwssbdup(const wchar_t *str, size_t len)
241 {
242 
243 	if (str == NULL)
244 		return ((len == 0) ? bwsalloc(0) : NULL);
245 	else {
246 		struct bwstring *ret;
247 
248 		ret = bwsalloc(len);
249 
250 		if (MB_CUR_MAX == 1)
251 			for (size_t i = 0; i < len; ++i)
252 				ret->data.cstr[i] = (unsigned char) str[i];
253 		else
254 			memcpy(ret->data.wstr, str, SIZEOF_WCHAR_STRING(len));
255 
256 		return (ret);
257 	}
258 }
259 
260 /*
261  * Create a new binary string from a raw binary buffer.
262  */
263 struct bwstring *
264 bwscsbdup(const unsigned char *str, size_t len)
265 {
266 	struct bwstring *ret;
267 
268 	ret = bwsalloc(len);
269 
270 	if (str) {
271 		if (MB_CUR_MAX == 1)
272 			memcpy(ret->data.cstr, str, len);
273 		else {
274 			mbstate_t mbs;
275 			const char *s;
276 			size_t charlen, chars, cptr;
277 
278 			charlen = chars = 0;
279 			cptr = 0;
280 			s = (const char *) str;
281 
282 			memset(&mbs, 0, sizeof(mbs));
283 
284 			while (cptr < len) {
285 				size_t n = MB_CUR_MAX;
286 
287 				if (n > len - cptr)
288 					n = len - cptr;
289 				charlen = mbrlen(s + cptr, n, &mbs);
290 				switch (charlen) {
291 				case 0:
292 					/* FALLTHROUGH */
293 				case (size_t) -1:
294 					/* FALLTHROUGH */
295 				case (size_t) -2:
296 					ret->data.wstr[chars++] =
297 					    (unsigned char) s[cptr];
298 					++cptr;
299 					break;
300 				default:
301 					n = mbrtowc(ret->data.wstr + (chars++),
302 					    s + cptr, charlen, &mbs);
303 					if ((n == (size_t)-1) || (n == (size_t)-2))
304 						/* NOTREACHED */
305 						err(2, "mbrtowc error");
306 					cptr += charlen;
307 				};
308 			}
309 
310 			ret->len = chars;
311 			ret->data.wstr[ret->len] = L'\0';
312 		}
313 	}
314 	return (ret);
315 }
316 
317 /*
318  * De-allocate object memory
319  */
320 void
321 bwsfree(const struct bwstring *s)
322 {
323 
324 	if (s)
325 		sort_free(s);
326 }
327 
328 /*
329  * Copy content of src binary string to dst.
330  * If the capacity of the dst string is not sufficient,
331  * then the data is truncated.
332  */
333 size_t
334 bwscpy(struct bwstring *dst, const struct bwstring *src)
335 {
336 	size_t nums = src->len;
337 
338 	if (nums > dst->len)
339 		nums = dst->len;
340 	dst->len = nums;
341 
342 	if (MB_CUR_MAX == 1) {
343 		memcpy(dst->data.cstr, src->data.cstr, nums);
344 		dst->data.cstr[dst->len] = '\0';
345 	} else {
346 		memcpy(dst->data.wstr, src->data.wstr,
347 		    SIZEOF_WCHAR_STRING(nums + 1));
348 		dst->data.wstr[dst->len] = L'\0';
349 	}
350 
351 	return (nums);
352 }
353 
354 /*
355  * Copy content of src binary string to dst,
356  * with specified number of symbols to be copied.
357  * If the capacity of the dst string is not sufficient,
358  * then the data is truncated.
359  */
360 struct bwstring *
361 bwsncpy(struct bwstring *dst, const struct bwstring *src, size_t size)
362 {
363 	size_t nums = src->len;
364 
365 	if (nums > dst->len)
366 		nums = dst->len;
367 	if (nums > size)
368 		nums = size;
369 	dst->len = nums;
370 
371 	if (MB_CUR_MAX == 1) {
372 		memcpy(dst->data.cstr, src->data.cstr, nums);
373 		dst->data.cstr[dst->len] = '\0';
374 	} else {
375 		memcpy(dst->data.wstr, src->data.wstr,
376 		    SIZEOF_WCHAR_STRING(nums + 1));
377 		dst->data.wstr[dst->len] = L'\0';
378 	}
379 
380 	return (dst);
381 }
382 
383 /*
384  * Copy content of src binary string to dst,
385  * with specified number of symbols to be copied.
386  * An offset value can be specified, from the start of src string.
387  * If the capacity of the dst string is not sufficient,
388  * then the data is truncated.
389  */
390 struct bwstring *
391 bwsnocpy(struct bwstring *dst, const struct bwstring *src, size_t offset,
392     size_t size)
393 {
394 
395 	if (offset >= src->len) {
396 		dst->data.wstr[0] = 0;
397 		dst->len = 0;
398 	} else {
399 		size_t nums = src->len - offset;
400 
401 		if (nums > dst->len)
402 			nums = dst->len;
403 		if (nums > size)
404 			nums = size;
405 		dst->len = nums;
406 		if (MB_CUR_MAX == 1) {
407 			memcpy(dst->data.cstr, src->data.cstr + offset,
408 			    (nums));
409 			dst->data.cstr[dst->len] = '\0';
410 		} else {
411 			memcpy(dst->data.wstr, src->data.wstr + offset,
412 			    SIZEOF_WCHAR_STRING(nums));
413 			dst->data.wstr[dst->len] = L'\0';
414 		}
415 	}
416 	return (dst);
417 }
418 
419 /*
420  * Write binary string to the file.
421  * The output is ended either with '\n' (nl == true)
422  * or '\0' (nl == false).
423  */
424 int
425 bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended)
426 {
427 
428 	if (MB_CUR_MAX == 1) {
429 		size_t len = bws->len;
430 
431 		if (!zero_ended) {
432 			bws->data.cstr[len] = '\n';
433 
434 			if (fwrite(bws->data.cstr, len + 1, 1, f) < 1)
435 				err(2, NULL);
436 
437 			bws->data.cstr[len] = '\0';
438 		} else if (fwrite(bws->data.cstr, len + 1, 1, f) < 1)
439 			err(2, NULL);
440 
441 		return (len + 1);
442 
443 	} else {
444 		wchar_t eols;
445 		int printed = 0;
446 
447 		eols = zero_ended ? btowc('\0') : btowc('\n');
448 
449 		while (printed < (int) BWSLEN(bws)) {
450 			const wchar_t *s = bws->data.wstr + printed;
451 
452 			if (*s == L'\0') {
453 				int nums;
454 
455 				nums = fwprintf(f, L"%lc", *s);
456 
457 				if (nums != 1)
458 					err(2, NULL);
459 				++printed;
460 			} else {
461 				int nums;
462 
463 				nums = fwprintf(f, L"%ls", s);
464 
465 				if (nums < 1)
466 					err(2, NULL);
467 				printed += nums;
468 			}
469 		}
470 		fwprintf(f, L"%lc", eols);
471 		return (printed + 1);
472 	}
473 }
474 
475 /*
476  * Allocate and read a binary string from file.
477  * The strings are nl-ended or zero-ended, depending on the sort setting.
478  */
479 struct bwstring *
480 bwsfgetln(FILE *f, size_t *len, bool zero_ended, struct reader_buffer *rb)
481 {
482 	wchar_t eols;
483 
484 	eols = zero_ended ? btowc('\0') : btowc('\n');
485 
486 	if (!zero_ended && (MB_CUR_MAX > 1)) {
487 		wchar_t *ret;
488 
489 		ret = fgetwln(f, len);
490 
491 		if (ret == NULL) {
492 			if (!feof(f))
493 				err(2, NULL);
494 			return (NULL);
495 		}
496 		if (*len > 0) {
497 			if (ret[*len - 1] == eols)
498 				--(*len);
499 		}
500 		return (bwssbdup(ret, *len));
501 
502 	} else {
503 		wchar_t c = 0;
504 
505 		*len = 0;
506 
507 		if (feof(f))
508 			return (NULL);
509 
510 		if (2 >= rb->fgetwln_z_buffer_size) {
511 			rb->fgetwln_z_buffer_size += 256;
512 			rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
513 			    sizeof(wchar_t) * rb->fgetwln_z_buffer_size);
514 		}
515 		rb->fgetwln_z_buffer[*len] = 0;
516 
517 		if (MB_CUR_MAX == 1)
518 			while (!feof(f)) {
519 				c = fgetc(f);
520 
521 				if (c == EOF) {
522 					if (*len == 0)
523 						return (NULL);
524 					goto line_read_done;
525 				}
526 				if (c == eols)
527 					goto line_read_done;
528 
529 				if (*len + 1 >= rb->fgetwln_z_buffer_size) {
530 					rb->fgetwln_z_buffer_size += 256;
531 					rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
532 					    SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size));
533 				}
534 
535 				rb->fgetwln_z_buffer[*len] = c;
536 				rb->fgetwln_z_buffer[++(*len)] = 0;
537 			}
538 		else
539 			while (!feof(f)) {
540 				c = fgetwc(f);
541 
542 				if (c == WEOF) {
543 					if (*len == 0)
544 						return (NULL);
545 					goto line_read_done;
546 				}
547 				if (c == eols)
548 					goto line_read_done;
549 
550 				if (*len + 1 >= rb->fgetwln_z_buffer_size) {
551 					rb->fgetwln_z_buffer_size += 256;
552 					rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
553 					    SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size));
554 				}
555 
556 				rb->fgetwln_z_buffer[*len] = c;
557 				rb->fgetwln_z_buffer[++(*len)] = 0;
558 			}
559 
560 line_read_done:
561 		/* we do not count the last 0 */
562 		return (bwssbdup(rb->fgetwln_z_buffer, *len));
563 	}
564 }
565 
566 int
567 bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2,
568     size_t offset, size_t len)
569 {
570 	size_t cmp_len, len1, len2;
571 	int res = 0;
572 
573 	cmp_len = 0;
574 	len1 = bws1->len;
575 	len2 = bws2->len;
576 
577 	if (len1 <= offset) {
578 		return ((len2 <= offset) ? 0 : -1);
579 	} else {
580 		if (len2 <= offset)
581 			return (+1);
582 		else {
583 			len1 -= offset;
584 			len2 -= offset;
585 
586 			cmp_len = len1;
587 
588 			if (len2 < cmp_len)
589 				cmp_len = len2;
590 
591 			if (len < cmp_len)
592 				cmp_len = len;
593 
594 			if (MB_CUR_MAX == 1) {
595 				const unsigned char *s1, *s2;
596 
597 				s1 = bws1->data.cstr + offset;
598 				s2 = bws2->data.cstr + offset;
599 
600 				res = memcmp(s1, s2, cmp_len);
601 
602 			} else {
603 				const wchar_t *s1, *s2;
604 
605 				s1 = bws1->data.wstr + offset;
606 				s2 = bws2->data.wstr + offset;
607 
608 				res = memcmp(s1, s2, SIZEOF_WCHAR_STRING(cmp_len));
609 			}
610 		}
611 	}
612 
613 	if (res == 0) {
614 		if (len1 < cmp_len && len1 < len2)
615 			res = -1;
616 		else if (len2 < cmp_len && len2 < len1)
617 			res = +1;
618 	}
619 
620 	return (res);
621 }
622 
623 int
624 bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset)
625 {
626 	size_t len1, len2, cmp_len;
627 	int res;
628 
629 	len1 = bws1->len;
630 	len2 = bws2->len;
631 
632 	len1 -= offset;
633 	len2 -= offset;
634 
635 	cmp_len = len1;
636 
637 	if (len2 < cmp_len)
638 		cmp_len = len2;
639 
640 	res = bwsncmp(bws1, bws2, offset, cmp_len);
641 
642 	if (res == 0) {
643 		if( len1 < len2)
644 			res = -1;
645 		else if (len2 < len1)
646 			res = +1;
647 	}
648 
649 	return (res);
650 }
651 
652 int
653 bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len)
654 {
655 	wchar_t c1, c2;
656 	size_t i = 0;
657 
658 	for (i = 0; i < len; ++i) {
659 		c1 = bws_get_iter_value(iter1);
660 		c2 = bws_get_iter_value(iter2);
661 		if (c1 != c2)
662 			return (c1 - c2);
663 		iter1 = bws_iterator_inc(iter1, 1);
664 		iter2 = bws_iterator_inc(iter2, 1);
665 	}
666 
667 	return (0);
668 }
669 
670 int
671 bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset)
672 {
673 	size_t len1, len2;
674 
675 	len1 = bws1->len;
676 	len2 = bws2->len;
677 
678 	if (len1 <= offset)
679 		return ((len2 <= offset) ? 0 : -1);
680 	else {
681 		if (len2 <= offset)
682 			return (+1);
683 		else {
684 			len1 -= offset;
685 			len2 -= offset;
686 
687 			if (MB_CUR_MAX == 1) {
688 				const unsigned char *s1, *s2;
689 
690 				s1 = bws1->data.cstr + offset;
691 				s2 = bws2->data.cstr + offset;
692 
693 				if (byte_sort) {
694 					int res = 0;
695 
696 					if (len1 > len2) {
697 						res = memcmp(s1, s2, len2);
698 						if (!res)
699 							res = +1;
700 					} else if (len1 < len2) {
701 						res = memcmp(s1, s2, len1);
702 						if (!res)
703 							res = -1;
704 					} else
705 						res = memcmp(s1, s2, len1);
706 
707 					return (res);
708 
709 				} else {
710 					int res = 0;
711 					size_t i, maxlen;
712 
713 					i = 0;
714 					maxlen = len1;
715 
716 					if (maxlen > len2)
717 						maxlen = len2;
718 
719 					while (i < maxlen) {
720 						/* goto next non-zero part: */
721 						while ((i < maxlen) &&
722 						    !s1[i] && !s2[i])
723 							++i;
724 
725 						if (i >= maxlen)
726 							break;
727 
728 						if (s1[i] == 0) {
729 							if (s2[i] == 0)
730 								/* NOTREACHED */
731 								err(2, "bwscoll error 01");
732 							else
733 								return (-1);
734 						} else if (s2[i] == 0)
735 							return (+1);
736 
737 						res = strcoll(s1 + i, s2 + i);
738 						if (res)
739 							return (res);
740 
741 						while ((i < maxlen) &&
742 						    s1[i] && s2[i])
743 							++i;
744 
745 						if (i >= maxlen)
746 							break;
747 
748 						if (s1[i] == 0) {
749 							if (s2[i] == 0) {
750 								++i;
751 								continue;
752 							} else
753 								return (-1);
754 						} else if (s2[i] == 0)
755 							return (+1);
756 						else
757 							/* NOTREACHED */
758 							err(2, "bwscoll error 02");
759 					}
760 
761 					if (len1 < len2)
762 						return (-1);
763 					else if (len1 > len2)
764 						return (+1);
765 
766 					return (0);
767 				}
768 			} else {
769 				const wchar_t *s1, *s2;
770 				size_t i, maxlen;
771 				int res = 0;
772 
773 				s1 = bws1->data.wstr + offset;
774 				s2 = bws2->data.wstr + offset;
775 
776 				i = 0;
777 				maxlen = len1;
778 
779 				if (maxlen > len2)
780 					maxlen = len2;
781 
782 				while (i < maxlen) {
783 
784 					/* goto next non-zero part: */
785 					while ((i < maxlen) &&
786 					    !s1[i] && !s2[i])
787 						++i;
788 
789 					if (i >= maxlen)
790 						break;
791 
792 					if (s1[i] == 0) {
793 						if (s2[i] == 0)
794 							/* NOTREACHED */
795 							err(2, "bwscoll error 1");
796 						else
797 							return (-1);
798 					} else if (s2[i] == 0)
799 						return (+1);
800 
801 					res = wide_str_coll(s1 + i, s2 + i);
802 					if (res)
803 						return (res);
804 
805 					while ((i < maxlen) && s1[i] && s2[i])
806 						++i;
807 
808 					if (i >= maxlen)
809 						break;
810 
811 					if (s1[i] == 0) {
812 						if (s2[i] == 0) {
813 							++i;
814 							continue;
815 						} else
816 							return (-1);
817 					} else if (s2[i] == 0)
818 						return (+1);
819 					else
820 						/* NOTREACHED */
821 						err(2, "bwscoll error 2");
822 				}
823 
824 				if (len1 < len2)
825 					return (-1);
826 				else if (len1 > len2)
827 					return (+1);
828 
829 				return (0);
830 			}
831 		}
832 	}
833 }
834 
835 /*
836  * Correction of the system API
837  */
838 double
839 bwstod(struct bwstring *s0, bool *empty)
840 {
841 	double ret = 0;
842 
843 	if (MB_CUR_MAX == 1) {
844 		unsigned char *end, *s;
845 		char *ep;
846 
847 		s = s0->data.cstr;
848 		end = s + s0->len;
849 		ep = NULL;
850 
851 		while (isblank(*s) && s < end)
852 			++s;
853 
854 		if (!isprint(*s)) {
855 			*empty = true;
856 			return (0);
857 		}
858 
859 		ret = strtod(s, &ep);
860 		if ((unsigned char*) ep == s) {
861 			*empty = true;
862 			return (0);
863 		}
864 	} else {
865 		wchar_t *end, *ep, *s;
866 
867 		s = s0->data.wstr;
868 		end = s + s0->len;
869 		ep = NULL;
870 
871 		while (iswblank(*s) && s < end)
872 			++s;
873 
874 		if (!iswprint(*s)) {
875 			*empty = true;
876 			return (0);
877 		}
878 
879 		ret = wcstod(s, &ep);
880 		if (ep == s) {
881 			*empty = true;
882 			return (0);
883 		}
884 	}
885 
886 	*empty = false;
887 	return (ret);
888 }
889 
890 /*
891  * A helper function for monthcoll.  If a line matches
892  * a month name, it returns (number of the month - 1),
893  * while if there is no match, it just return -1.
894  */
895 
896 int
897 bws_month_score(const struct bwstring *s0)
898 {
899 
900 	if (MB_CUR_MAX == 1) {
901 		const unsigned char *end, *s;
902 		size_t len;
903 
904 		s = s0->data.cstr;
905 		end = s + s0->len;
906 
907 		while (isblank(*s) && s < end)
908 			++s;
909 
910 		len = strlen(s);
911 
912 		for (int i = 11; i >= 0; --i) {
913 			if (cmonths[i] &&
914 			    (s == (unsigned char*)strstr(s, cmonths[i])))
915 				return (i);
916 		}
917 
918 	} else {
919 		const wchar_t *end, *s;
920 		size_t len;
921 
922 		s = s0->data.wstr;
923 		end = s + s0->len;
924 
925 		while (iswblank(*s) && s < end)
926 			++s;
927 
928 		len = wcslen(s);
929 
930 		for (int i = 11; i >= 0; --i) {
931 			if (wmonths[i] && (s == wcsstr(s, wmonths[i])))
932 				return (i);
933 		}
934 	}
935 
936 	return (-1);
937 }
938 
939 /*
940  * Rips out leading blanks (-b).
941  */
942 struct bwstring *
943 ignore_leading_blanks(struct bwstring *str)
944 {
945 
946 	if (MB_CUR_MAX == 1) {
947 		unsigned char *dst, *end, *src;
948 
949 		src = str->data.cstr;
950 		dst = src;
951 		end = src + str->len;
952 
953 		while (src < end && isblank(*src))
954 			++src;
955 
956 		if (src != dst) {
957 			size_t newlen;
958 
959 			newlen = BWSLEN(str) - (src - dst);
960 
961 			while (src < end) {
962 				*dst = *src;
963 				++dst;
964 				++src;
965 			}
966 			bws_setlen(str, newlen);
967 		}
968 	} else {
969 		wchar_t *dst, *end, *src;
970 
971 		src = str->data.wstr;
972 		dst = src;
973 		end = src + str->len;
974 
975 		while (src < end && iswblank(*src))
976 			++src;
977 
978 		if (src != dst) {
979 
980 			size_t newlen = BWSLEN(str) - (src - dst);
981 
982 			while (src < end) {
983 				*dst = *src;
984 				++dst;
985 				++src;
986 			}
987 			bws_setlen(str, newlen);
988 
989 		}
990 	}
991 	return (str);
992 }
993 
994 /*
995  * Rips out nonprinting characters (-i).
996  */
997 struct bwstring *
998 ignore_nonprinting(struct bwstring *str)
999 {
1000 	size_t newlen = str->len;
1001 
1002 	if (MB_CUR_MAX == 1) {
1003 		unsigned char *dst, *end, *src;
1004 		unsigned char c;
1005 
1006 		src = str->data.cstr;
1007 		dst = src;
1008 		end = src + str->len;
1009 
1010 		while (src < end) {
1011 			c = *src;
1012 			if (isprint(c)) {
1013 				*dst = c;
1014 				++dst;
1015 				++src;
1016 			} else {
1017 				++src;
1018 				--newlen;
1019 			}
1020 		}
1021 	} else {
1022 		wchar_t *dst, *end, *src;
1023 		wchar_t c;
1024 
1025 		src = str->data.wstr;
1026 		dst = src;
1027 		end = src + str->len;
1028 
1029 		while (src < end) {
1030 			c = *src;
1031 			if (iswprint(c)) {
1032 				*dst = c;
1033 				++dst;
1034 				++src;
1035 			} else {
1036 				++src;
1037 				--newlen;
1038 			}
1039 		}
1040 	}
1041 	bws_setlen(str, newlen);
1042 
1043 	return (str);
1044 }
1045 
1046 /*
1047  * Rips out any characters that are not alphanumeric characters
1048  * nor blanks (-d).
1049  */
1050 struct bwstring *
1051 dictionary_order(struct bwstring *str)
1052 {
1053 	size_t newlen = str->len;
1054 
1055 	if (MB_CUR_MAX == 1) {
1056 		unsigned char *dst, *end, *src;
1057 		unsigned char c;
1058 
1059 		src = str->data.cstr;
1060 		dst = src;
1061 		end = src + str->len;
1062 
1063 		while (src < end) {
1064 			c = *src;
1065 			if (isalnum(c) || isblank(c)) {
1066 				*dst = c;
1067 				++dst;
1068 				++src;
1069 			} else {
1070 				++src;
1071 				--newlen;
1072 			}
1073 		}
1074 	} else {
1075 		wchar_t *dst, *end, *src;
1076 		wchar_t c;
1077 
1078 		src = str->data.wstr;
1079 		dst = src;
1080 		end = src + str->len;
1081 
1082 		while (src < end) {
1083 			c = *src;
1084 			if (iswalnum(c) || iswblank(c)) {
1085 				*dst = c;
1086 				++dst;
1087 				++src;
1088 			} else {
1089 				++src;
1090 				--newlen;
1091 			}
1092 		}
1093 	}
1094 	bws_setlen(str, newlen);
1095 
1096 	return (str);
1097 }
1098 
1099 /*
1100  * Converts string to lower case(-f).
1101  */
1102 struct bwstring *
1103 ignore_case(struct bwstring *str)
1104 {
1105 
1106 	if (MB_CUR_MAX == 1) {
1107 		unsigned char *end, *s;
1108 
1109 		s = str->data.cstr;
1110 		end = s + str->len;
1111 
1112 		while (s < end) {
1113 			*s = toupper(*s);
1114 			++s;
1115 		}
1116 	} else {
1117 		wchar_t *end, *s;
1118 
1119 		s = str->data.wstr;
1120 		end = s + str->len;
1121 
1122 		while (s < end) {
1123 			*s = towupper(*s);
1124 			++s;
1125 		}
1126 	}
1127 	return (str);
1128 }
1129 
1130 void
1131 bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos)
1132 {
1133 
1134 	if (MB_CUR_MAX == 1)
1135 		warnx("%s:%zu: disorder: %s", fn, pos + 1, s->data.cstr);
1136 	else
1137 		warnx("%s:%zu: disorder: %ls", fn, pos + 1, s->data.wstr);
1138 }
1139