1 /* $NetBSD: iswctype_mb.c,v 1.14 2024/06/07 13:53:23 riastradh Exp $ */
2
3 /*-
4 * Copyright (c)2008 Citrus Project,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: iswctype_mb.c,v 1.14 2024/06/07 13:53:23 riastradh Exp $");
32 #endif /* LIBC_SCCS and not lint */
33
34 #include "namespace.h"
35 #include <sys/types.h>
36 #include <assert.h>
37 #include <errno.h>
38 #define __SETLOCALE_SOURCE__
39 #include <locale.h>
40 #include <string.h>
41 #include <wchar.h>
42 #include <wctype.h>
43
44 #include "setlocale_local.h"
45
46 #include "runetype_local.h"
47 #include "_wctype_local.h"
48 #include "_wctrans_local.h"
49
50 #define _RUNE_LOCALE(loc) ((_RuneLocale const *) \
51 (loc)->part_impl[LC_CTYPE])
52
53 #define _ISWCTYPE_FUNC(name, index) \
54 int \
55 isw##name##_l(wint_t wc, locale_t loc) \
56 { \
57 _RuneLocale const *rl; \
58 _WCTypeEntry const *te; \
59 \
60 rl = _RUNE_LOCALE(loc); \
61 te = &rl->rl_wctype[index]; \
62 return _iswctype_priv(rl, wc, te); \
63 } \
64 int \
65 isw##name(wint_t wc) \
66 { \
67 return isw##name##_l(wc, _current_locale()); \
68 }
69
_ISWCTYPE_FUNC(alnum,_WCTYPE_INDEX_ALNUM)70 _ISWCTYPE_FUNC(alnum, _WCTYPE_INDEX_ALNUM)
71 _ISWCTYPE_FUNC(alpha, _WCTYPE_INDEX_ALPHA)
72 _ISWCTYPE_FUNC(blank, _WCTYPE_INDEX_BLANK)
73 _ISWCTYPE_FUNC(cntrl, _WCTYPE_INDEX_CNTRL)
74 _ISWCTYPE_FUNC(digit, _WCTYPE_INDEX_DIGIT)
75 _ISWCTYPE_FUNC(graph, _WCTYPE_INDEX_GRAPH)
76 _ISWCTYPE_FUNC(lower, _WCTYPE_INDEX_LOWER)
77 _ISWCTYPE_FUNC(print, _WCTYPE_INDEX_PRINT)
78 _ISWCTYPE_FUNC(punct, _WCTYPE_INDEX_PUNCT)
79 _ISWCTYPE_FUNC(space, _WCTYPE_INDEX_SPACE)
80 _ISWCTYPE_FUNC(upper, _WCTYPE_INDEX_UPPER)
81 _ISWCTYPE_FUNC(xdigit, _WCTYPE_INDEX_XDIGIT)
82
83 #define _TOWCTRANS_FUNC(name, index) \
84 wint_t \
85 tow##name##_l(wint_t wc, locale_t loc) \
86 { \
87 _RuneLocale const *rl; \
88 _WCTransEntry const *te; \
89 \
90 rl = _RUNE_LOCALE(loc); \
91 te = &rl->rl_wctrans[index]; \
92 return _towctrans_priv(wc, te); \
93 } \
94 wint_t \
95 tow##name(wint_t wc) \
96 { \
97 return tow##name##_l(wc, _current_locale()); \
98 }
99 _TOWCTRANS_FUNC(upper, _WCTRANS_INDEX_UPPER)
100 _TOWCTRANS_FUNC(lower, _WCTRANS_INDEX_LOWER)
101
102 wctype_t
103 wctype_l(const char *charclass, locale_t loc)
104 {
105 _RuneLocale const *rl;
106 size_t i;
107
108 rl = _RUNE_LOCALE(loc);
109 for (i = 0; i < _WCTYPE_NINDEXES; ++i) {
110 if (!strcmp(rl->rl_wctype[i].te_name, charclass))
111 return (wctype_t)__UNCONST(&rl->rl_wctype[i]);
112 }
113 return (wctype_t)NULL;
114 }
115
116 wctype_t
wctype(const char * charclass)117 wctype(const char *charclass)
118 {
119 return wctype_l(charclass, _current_locale());
120 }
121
122 wctrans_t
wctrans_l(const char * charmap,locale_t loc)123 wctrans_l(const char *charmap, locale_t loc)
124 {
125 _RuneLocale const *rl;
126 size_t i;
127
128 rl = _RUNE_LOCALE(loc);
129 for (i = 0; i < _WCTRANS_NINDEXES; ++i) {
130 _DIAGASSERT(rl->rl_wctrans[i].te_name != NULL);
131 if (!strcmp(rl->rl_wctrans[i].te_name, charmap))
132 return (wctrans_t)__UNCONST(&rl->rl_wctype[i]);
133 }
134 return (wctrans_t)NULL;
135 }
136
137 wctrans_t
wctrans(const char * charmap)138 wctrans(const char *charmap)
139 {
140 return wctrans_l(charmap, _current_locale());
141 }
142
143 int
iswctype_l(wint_t wc,wctype_t charclass,locale_t loc)144 iswctype_l(wint_t wc, wctype_t charclass, locale_t loc)
145 {
146 _RuneLocale const *rl;
147 _WCTypeEntry const *te;
148
149 if (charclass == NULL) {
150 errno = EINVAL;
151 return 0;
152 }
153
154 rl = _RUNE_LOCALE(loc);
155 te = (_WCTypeEntry const *)(void *)charclass;
156 return _iswctype_priv(rl, wc, te);
157 }
158
159 int
iswctype(wint_t wc,wctype_t charclass)160 iswctype(wint_t wc, wctype_t charclass)
161 {
162 return iswctype_l(wc, charclass, _current_locale());
163 }
164
165 wint_t
towctrans(wint_t wc,wctrans_t charmap)166 towctrans(wint_t wc, wctrans_t charmap)
167 {
168 _WCTransEntry const *te;
169
170 if (charmap == NULL) {
171 errno = EINVAL;
172 return wc;
173 }
174 te = (_WCTransEntry const *)(void *)charmap;
175 return _towctrans_priv(wc, te);
176 }
177
178 /* ARGSUSED */
179 wint_t
towctrans_l(wint_t wc,wctrans_t charmap,locale_t loc)180 towctrans_l(wint_t wc, wctrans_t charmap, locale_t loc)
181 {
182 return towctrans(wc, charmap);
183 }
184
__weak_alias(wcwidth,_wcwidth)185 __weak_alias(wcwidth,_wcwidth)
186 __weak_alias(wcwidth_l,_wcwidth_l)
187
188 int
189 wcwidth_l(wchar_t wc, locale_t loc)
190 {
191 _RuneLocale const *rl;
192 _RuneType x;
193
194 if (wc == L'\0')
195 return 0;
196
197 rl = _RUNE_LOCALE(loc);
198 x = _runetype_priv(rl, wc);
199 if (x & _RUNETYPE_R)
200 return ((unsigned)x & _RUNETYPE_SWM) >> _RUNETYPE_SWS;
201 return -1;
202 }
203
204 int
wcwidth(wchar_t wc)205 wcwidth(wchar_t wc)
206 {
207 return wcwidth_l(wc, _current_locale());
208 }
209
210 int
wcswidth_l(const wchar_t * __restrict ws,size_t wn,locale_t loc)211 wcswidth_l(const wchar_t * __restrict ws, size_t wn, locale_t loc)
212 {
213 _RuneLocale const *rl;
214 _RuneType x;
215 int width;
216
217 _DIAGASSERT(ws != NULL);
218
219 rl = _RUNE_LOCALE(loc);
220 width = 0;
221 while (wn > 0 && *ws != L'\0') {
222 x = _runetype_priv(rl, *ws);
223 if ((x & _RUNETYPE_R) == 0)
224 return -1;
225 width += ((unsigned)x & _RUNETYPE_SWM) >> _RUNETYPE_SWS;
226 ++ws, --wn;
227 }
228 return width;
229 }
230
231 int
wcswidth(const wchar_t * __restrict ws,size_t wn)232 wcswidth(const wchar_t * __restrict ws, size_t wn)
233 {
234 return wcswidth_l(ws, wn, _current_locale());
235 }
236