1 /* $NetBSD: iswctype_mb.c,v 1.4 2009/01/18 19:53:11 christos 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_mb.c,v 1.4 2009/01/18 19:53:11 christos Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include "namespace.h"
35 #include <sys/types.h>
40 #define __SETLOCALE_SOURCE__
46 #include "setlocale_local.h"
49 #include "_wctype_local.h"
50 #include "_wctrans_local.h"
52 #define _RUNE_LOCALE() ((_RuneLocale const *) \
53 (*_current_locale())->part_impl[(size_t)LC_CTYPE])
55 #define _ISWCTYPE_FUNC(name, index) \
57 isw##name(wint_t wc) \
59 _RuneLocale const *rl; \
60 _WCTypeEntry const *te; \
62 rl = _RUNE_LOCALE(); \
63 te = &rl->rl_wctype[index]; \
64 return _iswctype_priv(rl, wc, te); \
66 _ISWCTYPE_FUNC(alnum
, _WCTYPE_INDEX_ALNUM
)
67 _ISWCTYPE_FUNC(alpha
, _WCTYPE_INDEX_ALPHA
)
68 _ISWCTYPE_FUNC(blank
, _WCTYPE_INDEX_BLANK
)
69 _ISWCTYPE_FUNC(cntrl
, _WCTYPE_INDEX_CNTRL
)
70 _ISWCTYPE_FUNC(digit
, _WCTYPE_INDEX_DIGIT
)
71 _ISWCTYPE_FUNC(graph
, _WCTYPE_INDEX_GRAPH
)
72 _ISWCTYPE_FUNC(lower
, _WCTYPE_INDEX_LOWER
)
73 _ISWCTYPE_FUNC(print
, _WCTYPE_INDEX_PRINT
)
74 _ISWCTYPE_FUNC(punct
, _WCTYPE_INDEX_PUNCT
)
75 _ISWCTYPE_FUNC(space
, _WCTYPE_INDEX_SPACE
)
76 _ISWCTYPE_FUNC(upper
, _WCTYPE_INDEX_UPPER
)
77 _ISWCTYPE_FUNC(xdigit
, _WCTYPE_INDEX_XDIGIT
)
79 #define _TOWCTRANS_FUNC(name, index) \
81 tow##name(wint_t wc) \
83 _RuneLocale const *rl; \
84 _WCTransEntry const *te; \
86 rl = _RUNE_LOCALE(); \
87 te = &rl->rl_wctrans[index]; \
88 return _towctrans_priv(rl, wc, te); \
90 _TOWCTRANS_FUNC(upper
, _WCTRANS_INDEX_UPPER
)
91 _TOWCTRANS_FUNC(lower
, _WCTRANS_INDEX_LOWER
)
94 wctype(const char *charclass
)
96 _RuneLocale
const *rl
;
100 for (i
= 0; i
< _WCTYPE_NINDEXES
; ++i
) {
101 if (!strcmp(rl
->rl_wctype
[i
].te_name
, charclass
))
102 return (wctype_t)__UNCONST(&rl
->rl_wctype
[i
]);
104 return (wctype_t)NULL
;
108 wctrans(const char *charmap
)
110 _RuneLocale
const *rl
;
114 for (i
= 0; i
< _WCTRANS_NINDEXES
; ++i
) {
115 _DIAGASSERT(rl
->rl_wctrans
[i
].te_name
!= NULL
);
116 if (!strcmp(rl
->rl_wctrans
[i
].te_name
, charmap
))
117 return (wctrans_t)__UNCONST(&rl
->rl_wctype
[i
]);
119 return (wctrans_t)NULL
;
123 iswctype(wint_t wc
, wctype_t charclass
)
125 _RuneLocale
const *rl
;
126 _WCTypeEntry
const *te
;
128 if (charclass
== NULL
) {
133 te
= (_WCTypeEntry
const *)charclass
;
134 return _iswctype_priv(rl
, wc
, te
);
138 towctrans(wint_t wc
, wctrans_t charmap
)
140 _RuneLocale
const *rl
;
141 _WCTransEntry
const *te
;
143 if (charmap
== NULL
) {
148 te
= (_WCTransEntry
const *)charmap
;
149 return _towctrans_priv(rl
, wc
, te
);
152 __weak_alias(wcwidth
,_wcwidth
)
157 _RuneLocale
const *rl
;
163 x
= _runetype_priv(rl
, wc
);
165 return ((unsigned)x
& _CTYPE_SWM
) >> _CTYPE_SWS
;
170 wcswidth(const wchar_t * __restrict ws
, size_t wn
)
172 _RuneLocale
const *rl
;
176 _DIAGASSERT(ws
!= NULL
);
180 while (wn
> 0 && *ws
!= L
'\0') {
181 x
= _runetype_priv(rl
, *ws
);
182 if ((x
& _CTYPE_R
) == 0)
184 width
+= ((unsigned)x
& _CTYPE_SWM
) >> _CTYPE_SWS
;