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