]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.bin/sort/bwstring.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / usr.bin / sort / bwstring.c
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 (tmp == NULL)
69                                         continue;
70                                 if (debug_sort)
71                                         printf("month[%d]=%s\n", i, tmp);
72                                 len = strlen((char*)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((char*)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, (char*)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 size_t
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                 size_t printed = 0;
446
447                 eols = zero_ended ? btowc('\0') : btowc('\n');
448
449                 while (printed < 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         wint_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] == (wchar_t)eols)
498                                 --(*len);
499                 }
500                 return (bwssbdup(ret, *len));
501
502         } else if (!zero_ended && (MB_CUR_MAX == 1)) {
503                 char *ret;
504
505                 ret = fgetln(f, len);
506
507                 if (ret == NULL) {
508                         if (!feof(f))
509                                 err(2, NULL);
510                         return (NULL);
511                 }
512                 if (*len > 0) {
513                         if (ret[*len - 1] == '\n')
514                                 --(*len);
515                 }
516                 return (bwscsbdup((unsigned char*)ret, *len));
517
518         } else {
519                 *len = 0;
520
521                 if (feof(f))
522                         return (NULL);
523
524                 if (2 >= rb->fgetwln_z_buffer_size) {
525                         rb->fgetwln_z_buffer_size += 256;
526                         rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
527                             sizeof(wchar_t) * rb->fgetwln_z_buffer_size);
528                 }
529                 rb->fgetwln_z_buffer[*len] = 0;
530
531                 if (MB_CUR_MAX == 1)
532                         while (!feof(f)) {
533                                 int c;
534
535                                 c = fgetc(f);
536
537                                 if (c == EOF) {
538                                         if (*len == 0)
539                                                 return (NULL);
540                                         goto line_read_done;
541                                 }
542                                 if (c == eols)
543                                         goto line_read_done;
544
545                                 if (*len + 1 >= rb->fgetwln_z_buffer_size) {
546                                         rb->fgetwln_z_buffer_size += 256;
547                                         rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
548                                             SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size));
549                                 }
550
551                                 rb->fgetwln_z_buffer[*len] = c;
552                                 rb->fgetwln_z_buffer[++(*len)] = 0;
553                         }
554                 else
555                         while (!feof(f)) {
556                                 wint_t c = 0;
557
558                                 c = fgetwc(f);
559
560                                 if (c == WEOF) {
561                                         if (*len == 0)
562                                                 return (NULL);
563                                         goto line_read_done;
564                                 }
565                                 if (c == eols)
566                                         goto line_read_done;
567
568                                 if (*len + 1 >= rb->fgetwln_z_buffer_size) {
569                                         rb->fgetwln_z_buffer_size += 256;
570                                         rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer,
571                                             SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size));
572                                 }
573
574                                 rb->fgetwln_z_buffer[*len] = c;
575                                 rb->fgetwln_z_buffer[++(*len)] = 0;
576                         }
577
578 line_read_done:
579                 /* we do not count the last 0 */
580                 return (bwssbdup(rb->fgetwln_z_buffer, *len));
581         }
582 }
583
584 int
585 bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2,
586     size_t offset, size_t len)
587 {
588         size_t cmp_len, len1, len2;
589         int res = 0;
590
591         cmp_len = 0;
592         len1 = bws1->len;
593         len2 = bws2->len;
594
595         if (len1 <= offset) {
596                 return ((len2 <= offset) ? 0 : -1);
597         } else {
598                 if (len2 <= offset)
599                         return (+1);
600                 else {
601                         len1 -= offset;
602                         len2 -= offset;
603
604                         cmp_len = len1;
605
606                         if (len2 < cmp_len)
607                                 cmp_len = len2;
608
609                         if (len < cmp_len)
610                                 cmp_len = len;
611
612                         if (MB_CUR_MAX == 1) {
613                                 const unsigned char *s1, *s2;
614
615                                 s1 = bws1->data.cstr + offset;
616                                 s2 = bws2->data.cstr + offset;
617
618                                 res = memcmp(s1, s2, cmp_len);
619
620                         } else {
621                                 const wchar_t *s1, *s2;
622
623                                 s1 = bws1->data.wstr + offset;
624                                 s2 = bws2->data.wstr + offset;
625
626                                 res = memcmp(s1, s2, SIZEOF_WCHAR_STRING(cmp_len));
627                         }
628                 }
629         }
630
631         if (res == 0) {
632                 if (len1 < cmp_len && len1 < len2)
633                         res = -1;
634                 else if (len2 < cmp_len && len2 < len1)
635                         res = +1;
636         }
637
638         return (res);
639 }
640
641 int
642 bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset)
643 {
644         size_t len1, len2, cmp_len;
645         int res;
646
647         len1 = bws1->len;
648         len2 = bws2->len;
649
650         len1 -= offset;
651         len2 -= offset;
652
653         cmp_len = len1;
654
655         if (len2 < cmp_len)
656                 cmp_len = len2;
657
658         res = bwsncmp(bws1, bws2, offset, cmp_len);
659
660         if (res == 0) {
661                 if( len1 < len2)
662                         res = -1;
663                 else if (len2 < len1)
664                         res = +1;
665         }
666
667         return (res);
668 }
669
670 int
671 bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len)
672 {
673         wchar_t c1, c2;
674         size_t i = 0;
675
676         for (i = 0; i < len; ++i) {
677                 c1 = bws_get_iter_value(iter1);
678                 c2 = bws_get_iter_value(iter2);
679                 if (c1 != c2)
680                         return (c1 - c2);
681                 iter1 = bws_iterator_inc(iter1, 1);
682                 iter2 = bws_iterator_inc(iter2, 1);
683         }
684
685         return (0);
686 }
687
688 int
689 bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset)
690 {
691         size_t len1, len2;
692
693         len1 = bws1->len;
694         len2 = bws2->len;
695
696         if (len1 <= offset)
697                 return ((len2 <= offset) ? 0 : -1);
698         else {
699                 if (len2 <= offset)
700                         return (+1);
701                 else {
702                         len1 -= offset;
703                         len2 -= offset;
704
705                         if (MB_CUR_MAX == 1) {
706                                 const unsigned char *s1, *s2;
707
708                                 s1 = bws1->data.cstr + offset;
709                                 s2 = bws2->data.cstr + offset;
710
711                                 if (byte_sort) {
712                                         int res = 0;
713
714                                         if (len1 > len2) {
715                                                 res = memcmp(s1, s2, len2);
716                                                 if (!res)
717                                                         res = +1;
718                                         } else if (len1 < len2) {
719                                                 res = memcmp(s1, s2, len1);
720                                                 if (!res)
721                                                         res = -1;
722                                         } else
723                                                 res = memcmp(s1, s2, len1);
724
725                                         return (res);
726
727                                 } else {
728                                         int res = 0;
729                                         size_t i, maxlen;
730
731                                         i = 0;
732                                         maxlen = len1;
733
734                                         if (maxlen > len2)
735                                                 maxlen = len2;
736
737                                         while (i < maxlen) {
738                                                 /* goto next non-zero part: */
739                                                 while ((i < maxlen) &&
740                                                     !s1[i] && !s2[i])
741                                                         ++i;
742
743                                                 if (i >= maxlen)
744                                                         break;
745
746                                                 if (s1[i] == 0) {
747                                                         if (s2[i] == 0)
748                                                                 /* NOTREACHED */
749                                                                 err(2, "bwscoll error 01");
750                                                         else
751                                                                 return (-1);
752                                                 } else if (s2[i] == 0)
753                                                         return (+1);
754
755                                                 res = strcoll((const char*)(s1 + i), (const char*)(s2 + i));
756                                                 if (res)
757                                                         return (res);
758
759                                                 while ((i < maxlen) &&
760                                                     s1[i] && s2[i])
761                                                         ++i;
762
763                                                 if (i >= maxlen)
764                                                         break;
765
766                                                 if (s1[i] == 0) {
767                                                         if (s2[i] == 0) {
768                                                                 ++i;
769                                                                 continue;
770                                                         } else
771                                                                 return (-1);
772                                                 } else if (s2[i] == 0)
773                                                         return (+1);
774                                                 else
775                                                         /* NOTREACHED */
776                                                         err(2, "bwscoll error 02");
777                                         }
778
779                                         if (len1 < len2)
780                                                 return (-1);
781                                         else if (len1 > len2)
782                                                 return (+1);
783
784                                         return (0);
785                                 }
786                         } else {
787                                 const wchar_t *s1, *s2;
788                                 size_t i, maxlen;
789                                 int res = 0;
790
791                                 s1 = bws1->data.wstr + offset;
792                                 s2 = bws2->data.wstr + offset;
793
794                                 i = 0;
795                                 maxlen = len1;
796
797                                 if (maxlen > len2)
798                                         maxlen = len2;
799
800                                 while (i < maxlen) {
801
802                                         /* goto next non-zero part: */
803                                         while ((i < maxlen) &&
804                                             !s1[i] && !s2[i])
805                                                 ++i;
806
807                                         if (i >= maxlen)
808                                                 break;
809
810                                         if (s1[i] == 0) {
811                                                 if (s2[i] == 0)
812                                                         /* NOTREACHED */
813                                                         err(2, "bwscoll error 1");
814                                                 else
815                                                         return (-1);
816                                         } else if (s2[i] == 0)
817                                                 return (+1);
818
819                                         res = wide_str_coll(s1 + i, s2 + i);
820                                         if (res)
821                                                 return (res);
822
823                                         while ((i < maxlen) && s1[i] && s2[i])
824                                                 ++i;
825
826                                         if (i >= maxlen)
827                                                 break;
828
829                                         if (s1[i] == 0) {
830                                                 if (s2[i] == 0) {
831                                                         ++i;
832                                                         continue;
833                                                 } else
834                                                         return (-1);
835                                         } else if (s2[i] == 0)
836                                                 return (+1);
837                                         else
838                                                 /* NOTREACHED */
839                                                 err(2, "bwscoll error 2");
840                                 }
841
842                                 if (len1 < len2)
843                                         return (-1);
844                                 else if (len1 > len2)
845                                         return (+1);
846
847                                 return (0);
848                         }
849                 }
850         }
851 }
852
853 /*
854  * Correction of the system API
855  */
856 double
857 bwstod(struct bwstring *s0, bool *empty)
858 {
859         double ret = 0;
860
861         if (MB_CUR_MAX == 1) {
862                 unsigned char *end, *s;
863                 char *ep;
864
865                 s = s0->data.cstr;
866                 end = s + s0->len;
867                 ep = NULL;
868
869                 while (isblank(*s) && s < end)
870                         ++s;
871
872                 if (!isprint(*s)) {
873                         *empty = true;
874                         return (0);
875                 }
876
877                 ret = strtod((char*)s, &ep);
878                 if ((unsigned char*) ep == s) {
879                         *empty = true;
880                         return (0);
881                 }
882         } else {
883                 wchar_t *end, *ep, *s;
884
885                 s = s0->data.wstr;
886                 end = s + s0->len;
887                 ep = NULL;
888
889                 while (iswblank(*s) && s < end)
890                         ++s;
891
892                 if (!iswprint(*s)) {
893                         *empty = true;
894                         return (0);
895                 }
896
897                 ret = wcstod(s, &ep);
898                 if (ep == s) {
899                         *empty = true;
900                         return (0);
901                 }
902         }
903
904         *empty = false;
905         return (ret);
906 }
907
908 /*
909  * A helper function for monthcoll.  If a line matches
910  * a month name, it returns (number of the month - 1),
911  * while if there is no match, it just return -1.
912  */
913
914 int
915 bws_month_score(const struct bwstring *s0)
916 {
917
918         if (MB_CUR_MAX == 1) {
919                 const unsigned char *end, *s;
920                 size_t len;
921
922                 s = s0->data.cstr;
923                 end = s + s0->len;
924
925                 while (isblank(*s) && s < end)
926                         ++s;
927
928                 len = strlen((const char*)s);
929
930                 for (int i = 11; i >= 0; --i) {
931                         if (cmonths[i] &&
932                             (s == (unsigned char*)strstr((const char*)s, (char*)(cmonths[i]))))
933                                 return (i);
934                 }
935
936         } else {
937                 const wchar_t *end, *s;
938                 size_t len;
939
940                 s = s0->data.wstr;
941                 end = s + s0->len;
942
943                 while (iswblank(*s) && s < end)
944                         ++s;
945
946                 len = wcslen(s);
947
948                 for (int i = 11; i >= 0; --i) {
949                         if (wmonths[i] && (s == wcsstr(s, wmonths[i])))
950                                 return (i);
951                 }
952         }
953
954         return (-1);
955 }
956
957 /*
958  * Rips out leading blanks (-b).
959  */
960 struct bwstring *
961 ignore_leading_blanks(struct bwstring *str)
962 {
963
964         if (MB_CUR_MAX == 1) {
965                 unsigned char *dst, *end, *src;
966
967                 src = str->data.cstr;
968                 dst = src;
969                 end = src + str->len;
970
971                 while (src < end && isblank(*src))
972                         ++src;
973
974                 if (src != dst) {
975                         size_t newlen;
976
977                         newlen = BWSLEN(str) - (src - dst);
978
979                         while (src < end) {
980                                 *dst = *src;
981                                 ++dst;
982                                 ++src;
983                         }
984                         bws_setlen(str, newlen);
985                 }
986         } else {
987                 wchar_t *dst, *end, *src;
988
989                 src = str->data.wstr;
990                 dst = src;
991                 end = src + str->len;
992
993                 while (src < end && iswblank(*src))
994                         ++src;
995
996                 if (src != dst) {
997
998                         size_t newlen = BWSLEN(str) - (src - dst);
999
1000                         while (src < end) {
1001                                 *dst = *src;
1002                                 ++dst;
1003                                 ++src;
1004                         }
1005                         bws_setlen(str, newlen);
1006
1007                 }
1008         }
1009         return (str);
1010 }
1011
1012 /*
1013  * Rips out nonprinting characters (-i).
1014  */
1015 struct bwstring *
1016 ignore_nonprinting(struct bwstring *str)
1017 {
1018         size_t newlen = str->len;
1019
1020         if (MB_CUR_MAX == 1) {
1021                 unsigned char *dst, *end, *src;
1022                 unsigned char c;
1023
1024                 src = str->data.cstr;
1025                 dst = src;
1026                 end = src + str->len;
1027
1028                 while (src < end) {
1029                         c = *src;
1030                         if (isprint(c)) {
1031                                 *dst = c;
1032                                 ++dst;
1033                                 ++src;
1034                         } else {
1035                                 ++src;
1036                                 --newlen;
1037                         }
1038                 }
1039         } else {
1040                 wchar_t *dst, *end, *src;
1041                 wchar_t c;
1042
1043                 src = str->data.wstr;
1044                 dst = src;
1045                 end = src + str->len;
1046
1047                 while (src < end) {
1048                         c = *src;
1049                         if (iswprint(c)) {
1050                                 *dst = c;
1051                                 ++dst;
1052                                 ++src;
1053                         } else {
1054                                 ++src;
1055                                 --newlen;
1056                         }
1057                 }
1058         }
1059         bws_setlen(str, newlen);
1060
1061         return (str);
1062 }
1063
1064 /*
1065  * Rips out any characters that are not alphanumeric characters
1066  * nor blanks (-d).
1067  */
1068 struct bwstring *
1069 dictionary_order(struct bwstring *str)
1070 {
1071         size_t newlen = str->len;
1072
1073         if (MB_CUR_MAX == 1) {
1074                 unsigned char *dst, *end, *src;
1075                 unsigned char c;
1076
1077                 src = str->data.cstr;
1078                 dst = src;
1079                 end = src + str->len;
1080
1081                 while (src < end) {
1082                         c = *src;
1083                         if (isalnum(c) || isblank(c)) {
1084                                 *dst = c;
1085                                 ++dst;
1086                                 ++src;
1087                         } else {
1088                                 ++src;
1089                                 --newlen;
1090                         }
1091                 }
1092         } else {
1093                 wchar_t *dst, *end, *src;
1094                 wchar_t c;
1095
1096                 src = str->data.wstr;
1097                 dst = src;
1098                 end = src + str->len;
1099
1100                 while (src < end) {
1101                         c = *src;
1102                         if (iswalnum(c) || iswblank(c)) {
1103                                 *dst = c;
1104                                 ++dst;
1105                                 ++src;
1106                         } else {
1107                                 ++src;
1108                                 --newlen;
1109                         }
1110                 }
1111         }
1112         bws_setlen(str, newlen);
1113
1114         return (str);
1115 }
1116
1117 /*
1118  * Converts string to lower case(-f).
1119  */
1120 struct bwstring *
1121 ignore_case(struct bwstring *str)
1122 {
1123
1124         if (MB_CUR_MAX == 1) {
1125                 unsigned char *end, *s;
1126
1127                 s = str->data.cstr;
1128                 end = s + str->len;
1129
1130                 while (s < end) {
1131                         *s = toupper(*s);
1132                         ++s;
1133                 }
1134         } else {
1135                 wchar_t *end, *s;
1136
1137                 s = str->data.wstr;
1138                 end = s + str->len;
1139
1140                 while (s < end) {
1141                         *s = towupper(*s);
1142                         ++s;
1143                 }
1144         }
1145         return (str);
1146 }
1147
1148 void
1149 bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos)
1150 {
1151
1152         if (MB_CUR_MAX == 1)
1153                 warnx("%s:%zu: disorder: %s", fn, pos + 1, s->data.cstr);
1154         else
1155                 warnx("%s:%zu: disorder: %ls", fn, pos + 1, s->data.wstr);
1156 }