Sync usage with man page.
[netbsd-mini2440.git] / lib / libc / locale / iswctype_mb.c
blobc765dbb8748d33b73889cd4fce5560ffb41e1302
1 /* $NetBSD: iswctype_mb.c,v 1.4 2009/01/18 19:53:11 christos Exp $ */
3 /*-
4 * Copyright (c)2008 Citrus Project,
5 * All rights reserved.
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.
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.
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>
36 #include <assert.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <langinfo.h>
40 #define __SETLOCALE_SOURCE__
41 #include <locale.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <wctype.h>
46 #include "setlocale_local.h"
48 #include "rune.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) \
56 int \
57 isw##name(wint_t wc) \
58 { \
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) \
80 wint_t \
81 tow##name(wint_t wc) \
82 { \
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)
93 wctype_t
94 wctype(const char *charclass)
96 _RuneLocale const *rl;
97 size_t i;
99 rl = _RUNE_LOCALE();
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;
107 wctrans_t
108 wctrans(const char *charmap)
110 _RuneLocale const *rl;
111 size_t i;
113 rl = _RUNE_LOCALE();
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) {
129 errno = EINVAL;
130 return 0;
132 rl = _RUNE_LOCALE();
133 te = (_WCTypeEntry const *)charclass;
134 return _iswctype_priv(rl, wc, te);
137 wint_t
138 towctrans(wint_t wc, wctrans_t charmap)
140 _RuneLocale const *rl;
141 _WCTransEntry const *te;
143 if (charmap == NULL) {
144 errno = EINVAL;
145 return wc;
147 rl = _RUNE_LOCALE();
148 te = (_WCTransEntry const *)charmap;
149 return _towctrans_priv(rl, wc, te);
152 __weak_alias(wcwidth,_wcwidth)
155 wcwidth(wchar_t wc)
157 _RuneLocale const *rl;
158 _RuneType x;
160 if (wc == L'\0')
161 return 0;
162 rl = _RUNE_LOCALE();
163 x = _runetype_priv(rl, wc);
164 if (x & _CTYPE_R)
165 return ((unsigned)x & _CTYPE_SWM) >> _CTYPE_SWS;
166 return -1;
170 wcswidth(const wchar_t * __restrict ws, size_t wn)
172 _RuneLocale const *rl;
173 _RuneType x;
174 int width;
176 _DIAGASSERT(ws != NULL);
178 rl = _RUNE_LOCALE();
179 width = 0;
180 while (wn > 0 && *ws != L'\0') {
181 x = _runetype_priv(rl, *ws);
182 if ((x & _CTYPE_R) == 0)
183 return -1;
184 width += ((unsigned)x & _CTYPE_SWM) >> _CTYPE_SWS;
185 ++ws, --wn;
187 return width;