xref: /freebsd/usr.bin/sort/bwstring.h (revision 4543ef516683042d46f3bd3bb8a4f3f746e00499)
1 
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause
4  *
5  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
6  * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #if !defined(__BWSTRING_H__)
32 #define	__BWSTRING_H__
33 
34 #include <stdbool.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <sysexits.h>
38 #include <wchar.h>
39 
40 #include "sort.h"
41 #include "mem.h"
42 
43 extern bool byte_sort;
44 
45 /* wchar_t is of 4 bytes: */
46 #define	SIZEOF_WCHAR_STRING(LEN) ((LEN)*sizeof(wchar_t))
47 
48 struct wstr {
49 	size_t len;
50 	wchar_t str[];
51 };
52 
53 struct cstr {
54 	size_t len;
55 	char str[];
56 };
57 
58 /*
59  * Binary "wide" string
60  */
61 struct bwstring
62 {
63 	union {
64 		struct wstr wdata;
65 		struct cstr cdata;
66 	};
67 };
68 
69 typedef void *bwstring_iterator;
70 
71 #define	BWSLEN(s) ((mb_cur_max == 1) ? (s)->cdata.len : (s)->wdata.len)
72 struct bwstring *bwsalloc(size_t sz);
73 
74 size_t bwsrawlen(const struct bwstring *bws);
75 const void* bwsrawdata(const struct bwstring *bws);
76 void bws_setlen(struct bwstring *bws, size_t newlen);
77 size_t bws_memsize(const struct bwstring *bws);
78 double bwstod(struct bwstring *s0, bool *empty);
79 int bws_month_score(const struct bwstring *s0);
80 
81 struct bwstring *ignore_leading_blanks(struct bwstring *str);
82 struct bwstring *ignore_nonprinting(struct bwstring *str);
83 struct bwstring *dictionary_order(struct bwstring *str);
84 struct bwstring *ignore_case(struct bwstring *str);
85 
86 void bwsprintf(FILE*, struct bwstring*, const char *prefix, const char *suffix);
87 void bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos);
88 
89 struct bwstring *bwsdup(const struct bwstring *s);
90 struct bwstring *bwssbdup(const wchar_t *str, size_t size);
91 struct bwstring *bwscsbdup(const unsigned char *str, size_t size);
92 void bwsfree(const struct bwstring *s);
93 struct bwstring *bwsnocpy(struct bwstring *dst, const struct bwstring *src, size_t offset, size_t size);
94 int bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset);
95 int bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset, size_t len);
96 int bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset);
97 size_t bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended);
98 
99 static inline bwstring_iterator
100 bws_begin(struct bwstring *bws)
101 {
102 
103 	return ((bwstring_iterator)bws->wdata.str);
104 }
105 
106 static inline bwstring_iterator
107 bws_end(struct bwstring *bws)
108 {
109 
110 	return ((mb_cur_max == 1) ?
111 	    (bwstring_iterator) (bws->cdata.str + bws->cdata.len) :
112 	    (bwstring_iterator) (bws->wdata.str + bws->wdata.len));
113 }
114 
115 static inline bwstring_iterator
116 bws_iterator_inc(bwstring_iterator iter, size_t pos)
117 {
118 
119 	if (mb_cur_max == 1)
120 		return ((unsigned char *) iter) + pos;
121 	else
122 		return ((wchar_t*) iter) + pos;
123 }
124 
125 static inline wchar_t
126 bws_get_iter_value(bwstring_iterator iter)
127 {
128 
129 	if (mb_cur_max == 1)
130 		return *((unsigned char *) iter);
131 	else
132 		return *((wchar_t*) iter);
133 }
134 
135 int
136 bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len);
137 
138 #define	BWS_GET(bws, pos) ((mb_cur_max == 1) ? (bws->cdata.str[(pos)]) : bws->wdata.str[(pos)])
139 
140 void initialise_months(void);
141 
142 #endif /* __BWSTRING_H__ */
143