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