xref: /openbsd/lib/libc/locale/iswctype.c (revision 286f9c70)
1*286f9c70Sjca /*	$OpenBSD: iswctype.c,v 1.9 2024/02/04 12:46:01 jca Exp $ */
2037bef94Sespie /*	$NetBSD: iswctype.c,v 1.15 2005/02/09 21:35:46 kleink Exp $	*/
3037bef94Sespie 
4037bef94Sespie /*
5037bef94Sespie  * Copyright (c) 1989 The Regents of the University of California.
6037bef94Sespie  * All rights reserved.
7037bef94Sespie  * (c) UNIX System Laboratories, Inc.
8037bef94Sespie  * All or some portions of this file are derived from material licensed
9037bef94Sespie  * to the University of California by American Telephone and Telegraph
10037bef94Sespie  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11037bef94Sespie  * the permission of UNIX System Laboratories, Inc.
12037bef94Sespie  *
13037bef94Sespie  * Redistribution and use in source and binary forms, with or without
14037bef94Sespie  * modification, are permitted provided that the following conditions
15037bef94Sespie  * are met:
16037bef94Sespie  * 1. Redistributions of source code must retain the above copyright
17037bef94Sespie  *    notice, this list of conditions and the following disclaimer.
18037bef94Sespie  * 2. Redistributions in binary form must reproduce the above copyright
19037bef94Sespie  *    notice, this list of conditions and the following disclaimer in the
20037bef94Sespie  *    documentation and/or other materials provided with the distribution.
21037bef94Sespie  * 3. Neither the name of the University nor the names of its contributors
22037bef94Sespie  *    may be used to endorse or promote products derived from this software
23037bef94Sespie  *    without specific prior written permission.
24037bef94Sespie  *
25037bef94Sespie  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26037bef94Sespie  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27037bef94Sespie  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28037bef94Sespie  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29037bef94Sespie  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30037bef94Sespie  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31037bef94Sespie  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32037bef94Sespie  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33037bef94Sespie  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34037bef94Sespie  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35037bef94Sespie  * SUCH DAMAGE.
36037bef94Sespie  */
37037bef94Sespie 
38037bef94Sespie #include <wchar.h>
39037bef94Sespie #include <wctype.h>
40037bef94Sespie #include <ctype.h>
41037bef94Sespie #include <errno.h>
42037bef94Sespie #include <string.h>
43037bef94Sespie #include "rune.h"
44037bef94Sespie #include "runetype.h"
45037bef94Sespie #include "rune_local.h"
46037bef94Sespie #include "_wctrans_local.h"
47037bef94Sespie 
48eb78bd26Sguenther static inline _RuneType __runetype_w(wint_t);
49eb78bd26Sguenther static inline int __isctype_w(wint_t, _RuneType);
50eb78bd26Sguenther static inline wint_t __toupper_w(wint_t);
51eb78bd26Sguenther static inline wint_t __tolower_w(wint_t);
52037bef94Sespie 
53eb78bd26Sguenther static inline _RuneType
__runetype_w(wint_t c)54037bef94Sespie __runetype_w(wint_t c)
55037bef94Sespie {
563a628b46Sschwarze 	_RuneLocale *rl = _CurrentRuneLocale();
57037bef94Sespie 
58037bef94Sespie 	return (_RUNE_ISCACHED(c) ?
593a628b46Sschwarze 		rl->rl_runetype[c] : ___runetype_mb(c, rl));
60037bef94Sespie }
61037bef94Sespie 
62eb78bd26Sguenther static inline int
__isctype_w(wint_t c,_RuneType f)63037bef94Sespie __isctype_w(wint_t c, _RuneType f)
64037bef94Sespie {
65037bef94Sespie 	return (!!(__runetype_w(c) & f));
66037bef94Sespie }
67037bef94Sespie 
68eb78bd26Sguenther static inline wint_t
__toupper_w(wint_t c)69037bef94Sespie __toupper_w(wint_t c)
70037bef94Sespie {
713a628b46Sschwarze 	return (_towctrans(c, _wctrans_upper(_CurrentRuneLocale())));
72037bef94Sespie }
73037bef94Sespie 
74eb78bd26Sguenther static inline wint_t
__tolower_w(wint_t c)75037bef94Sespie __tolower_w(wint_t c)
76037bef94Sespie {
773a628b46Sschwarze 	return (_towctrans(c, _wctrans_lower(_CurrentRuneLocale())));
78037bef94Sespie }
79037bef94Sespie 
80037bef94Sespie int
iswalnum(wint_t c)81037bef94Sespie iswalnum(wint_t c)
82037bef94Sespie {
83*286f9c70Sjca 	return (__isctype_w((c), _RUNETYPE_A|_RUNETYPE_D));
84037bef94Sespie }
85037bef94Sespie 
86037bef94Sespie int
iswalpha(wint_t c)87037bef94Sespie iswalpha(wint_t c)
88037bef94Sespie {
89*286f9c70Sjca 	return (__isctype_w((c), _RUNETYPE_A));
90037bef94Sespie }
91037bef94Sespie 
92037bef94Sespie int
iswblank(wint_t c)93037bef94Sespie iswblank(wint_t c)
94037bef94Sespie {
95*286f9c70Sjca 	return (__isctype_w((c), _RUNETYPE_B));
96037bef94Sespie }
97037bef94Sespie 
98037bef94Sespie int
iswcntrl(wint_t c)99037bef94Sespie iswcntrl(wint_t c)
100037bef94Sespie {
101*286f9c70Sjca 	return (__isctype_w((c), _RUNETYPE_C));
102037bef94Sespie }
103037bef94Sespie 
104037bef94Sespie int
iswdigit(wint_t c)105037bef94Sespie iswdigit(wint_t c)
106037bef94Sespie {
107*286f9c70Sjca 	return (__isctype_w((c), _RUNETYPE_D));
108037bef94Sespie }
109037bef94Sespie 
110037bef94Sespie int
iswgraph(wint_t c)111037bef94Sespie iswgraph(wint_t c)
112037bef94Sespie {
113*286f9c70Sjca 	return (__isctype_w((c), _RUNETYPE_G));
114037bef94Sespie }
115037bef94Sespie 
116037bef94Sespie int
iswlower(wint_t c)117037bef94Sespie iswlower(wint_t c)
118037bef94Sespie {
119*286f9c70Sjca 	return (__isctype_w((c), _RUNETYPE_L));
120037bef94Sespie }
121037bef94Sespie 
122037bef94Sespie int
iswprint(wint_t c)123037bef94Sespie iswprint(wint_t c)
124037bef94Sespie {
125*286f9c70Sjca 	return (__isctype_w((c), _RUNETYPE_R));
126037bef94Sespie }
127037bef94Sespie 
128037bef94Sespie int
iswpunct(wint_t c)129037bef94Sespie iswpunct(wint_t c)
130037bef94Sespie {
131*286f9c70Sjca 	return (__isctype_w((c), _RUNETYPE_P));
132037bef94Sespie }
133037bef94Sespie 
134037bef94Sespie int
iswspace(wint_t c)135037bef94Sespie iswspace(wint_t c)
136037bef94Sespie {
137*286f9c70Sjca 	return (__isctype_w((c), _RUNETYPE_S));
138037bef94Sespie }
139eb78bd26Sguenther DEF_STRONG(iswspace);
140037bef94Sespie 
141037bef94Sespie int
iswupper(wint_t c)142037bef94Sespie iswupper(wint_t c)
143037bef94Sespie {
144*286f9c70Sjca 	return (__isctype_w((c), _RUNETYPE_U));
145037bef94Sespie }
146eb78bd26Sguenther DEF_STRONG(iswupper);
147037bef94Sespie 
148037bef94Sespie int
iswxdigit(wint_t c)149037bef94Sespie iswxdigit(wint_t c)
150037bef94Sespie {
151*286f9c70Sjca 	return (__isctype_w((c), _RUNETYPE_X));
152037bef94Sespie }
153037bef94Sespie 
154037bef94Sespie wint_t
towupper(wint_t c)155037bef94Sespie towupper(wint_t c)
156037bef94Sespie {
157037bef94Sespie 	return (__toupper_w(c));
158037bef94Sespie }
159037bef94Sespie 
160037bef94Sespie wint_t
towlower(wint_t c)161037bef94Sespie towlower(wint_t c)
162037bef94Sespie {
163037bef94Sespie 	return (__tolower_w(c));
164037bef94Sespie }
165eb78bd26Sguenther DEF_STRONG(towlower);
166037bef94Sespie 
167037bef94Sespie int
wcwidth(wchar_t c)168037bef94Sespie wcwidth(wchar_t c)
169037bef94Sespie {
170*286f9c70Sjca 	if (__isctype_w((c), _RUNETYPE_R))
171*286f9c70Sjca 		return (((unsigned)__runetype_w(c) & _RUNETYPE_SWM) >>
172*286f9c70Sjca 		    _RUNETYPE_SWS);
17329374459Sstsp 	return -1;
174037bef94Sespie }
17538a75b98Sguenther DEF_WEAK(wcwidth);
176037bef94Sespie 
177037bef94Sespie wctrans_t
wctrans(const char * charclass)178037bef94Sespie wctrans(const char *charclass)
179037bef94Sespie {
180037bef94Sespie 	int i;
1813a628b46Sschwarze 	_RuneLocale *rl = _CurrentRuneLocale();
182037bef94Sespie 
183037bef94Sespie 	if (rl->rl_wctrans[_WCTRANS_INDEX_LOWER].te_name==NULL)
184037bef94Sespie 		_wctrans_init(rl);
185037bef94Sespie 
186037bef94Sespie 	for (i=0; i<_WCTRANS_NINDEXES; i++)
187037bef94Sespie 		if (!strcmp(rl->rl_wctrans[i].te_name, charclass))
188037bef94Sespie 			return ((wctrans_t)&rl->rl_wctrans[i]);
189037bef94Sespie 
190037bef94Sespie 	return ((wctrans_t)NULL);
191037bef94Sespie }
192037bef94Sespie 
193037bef94Sespie wint_t
towctrans(wint_t c,wctrans_t desc)194037bef94Sespie towctrans(wint_t c, wctrans_t desc)
195037bef94Sespie {
196037bef94Sespie 	if (desc==NULL) {
197037bef94Sespie 		errno = EINVAL;
198037bef94Sespie 		return (c);
199037bef94Sespie 	}
200037bef94Sespie 	return (_towctrans(c, (_WCTransEntry *)desc));
201037bef94Sespie }
2023a628b46Sschwarze DEF_STRONG(towctrans);
203037bef94Sespie 
204037bef94Sespie int
iswctype(wint_t c,wctype_t charclass)205037bef94Sespie iswctype(wint_t c, wctype_t charclass)
206037bef94Sespie {
207037bef94Sespie 
208037bef94Sespie 	/*
209037bef94Sespie 	 * SUSv3: If charclass is 0, iswctype() shall return 0.
210037bef94Sespie 	 */
211037bef94Sespie 	if (charclass == (wctype_t)0) {
212037bef94Sespie 		return 0;
213037bef94Sespie 	}
214037bef94Sespie 
215037bef94Sespie 	return (__isctype_w(c, ((_WCTypeEntry *)charclass)->te_mask));
216037bef94Sespie }
217