1 /* $NetBSD: iswctype_sb.c,v 1.11 2010/06/01 18:00:28 tnozaki Exp $ */
4 * Copyright (c)2008 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: iswctype_sb.c,v 1.11 2010/06/01 18:00:28 tnozaki Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include "namespace.h"
36 #define _CTYPE_NOINLINE
44 #define _ISWCTYPE_FUNC(name) \
46 isw##name(wint_t wc) \
50 c = (wc == WEOF) ? EOF : (unsigned char)wc; \
64 _ISWCTYPE_FUNC(xdigit
)
66 #define _TOWCTRANS_FUNC(name) \
68 tow##name(wint_t wc) \
71 c = (wc == WEOF) ? EOF : (unsigned char)wc; \
74 _TOWCTRANS_FUNC(upper
)
75 _TOWCTRANS_FUNC(lower
)
77 struct _wctype_priv_t
{
79 int (*iswctype
)(wint_t);
82 static const struct _wctype_priv_t _wctype_decl
[] = {
83 { "alnum", &iswalnum
},
84 { "alpha", &iswalpha
},
85 { "blank", &iswblank
},
86 { "cntrl", &iswcntrl
},
87 { "digit", &iswdigit
},
88 { "graph", &iswgraph
},
89 { "lower", &iswlower
},
90 { "print", &iswprint
},
91 { "punct", &iswpunct
},
92 { "space", &iswspace
},
93 { "upper", &iswupper
},
94 { "xdigit", &iswxdigit
},
96 static const size_t _wctype_decl_size
=
97 sizeof(_wctype_decl
) / sizeof(struct _wctype_priv_t
);
100 wctype(const char *charclass
)
104 for (i
= 0; i
< _wctype_decl_size
; ++i
) {
105 if (!strcmp(charclass
, _wctype_decl
[i
].name
))
106 return (wctype_t)__UNCONST(&_wctype_decl
[i
]);
108 return (wctype_t)NULL
;
111 struct _wctrans_priv_t
{
113 wint_t (*towctrans
)(wint_t);
116 static const struct _wctrans_priv_t _wctrans_decl
[] = {
117 { "upper", &towupper
},
118 { "lower", &towlower
},
120 static const size_t _wctrans_decl_size
=
121 sizeof(_wctrans_decl
) / sizeof(struct _wctrans_priv_t
);
125 wctrans(const char *charmap
)
129 for (i
= 0; i
< _wctrans_decl_size
; ++i
) {
130 if (!strcmp(charmap
, _wctrans_decl
[i
].name
))
131 return (wctrans_t)__UNCONST(&_wctrans_decl
[i
]);
133 return (wctrans_t)NULL
;
138 iswctype(wint_t wc
, wctype_t charclass
)
140 const struct _wctype_priv_t
*p
;
142 p
= (const struct _wctype_priv_t
*)(void *)charclass
;
143 if (p
< &_wctype_decl
[0] || p
> &_wctype_decl
[_wctype_decl_size
- 1]) {
147 return (*p
->iswctype
)(wc
);
152 towctrans(wint_t wc
, wctrans_t charmap
)
154 const struct _wctrans_priv_t
*p
;
156 p
= (const struct _wctrans_priv_t
*)(void *)charmap
;
157 if (p
< &_wctrans_decl
[0] || p
> &_wctrans_decl
[_wctrans_decl_size
- 1]) {
161 return (*p
->towctrans
)(wc
);
164 __weak_alias(wcwidth
,_wcwidth
)
178 c
= (unsigned char)wc
;
186 wcswidth(const wchar_t * __restrict ws
, size_t wn
)
192 while (wn
> 0 && *ws
!= L
'\0') {
193 c
= (*ws
== WEOF
) ? EOF
: (unsigned char)*ws
;
198 return (int)(ws
- pws
);