1 /* $OpenBSD: iswctype.c,v 1.6 2015/09/13 11:38:08 guenther Exp $ */ 2 /* $NetBSD: iswctype.c,v 1.15 2005/02/09 21:35:46 kleink Exp $ */ 3 4 /* 5 * Copyright (c) 1989 The Regents of the University of California. 6 * All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #include <wchar.h> 39 #include <wctype.h> 40 #include <ctype.h> 41 #include <errno.h> 42 #include <string.h> 43 #include "rune.h" 44 #include "runetype.h" 45 #include "rune_local.h" 46 #include "_wctrans_local.h" 47 48 static inline _RuneType __runetype_w(wint_t); 49 static inline int __isctype_w(wint_t, _RuneType); 50 static inline wint_t __toupper_w(wint_t); 51 static inline wint_t __tolower_w(wint_t); 52 53 static inline _RuneType 54 __runetype_w(wint_t c) 55 { 56 _RuneLocale *rl = _CurrentRuneLocale; 57 58 return (_RUNE_ISCACHED(c) ? 59 rl->rl_runetype[c] : ___runetype_mb(c)); 60 } 61 62 static inline int 63 __isctype_w(wint_t c, _RuneType f) 64 { 65 return (!!(__runetype_w(c) & f)); 66 } 67 68 static inline wint_t 69 __toupper_w(wint_t c) 70 { 71 return (_towctrans(c, _wctrans_upper(_CurrentRuneLocale))); 72 } 73 74 static inline wint_t 75 __tolower_w(wint_t c) 76 { 77 return (_towctrans(c, _wctrans_lower(_CurrentRuneLocale))); 78 } 79 80 int 81 iswalnum(wint_t c) 82 { 83 return (__isctype_w((c), _CTYPE_A|_CTYPE_D)); 84 } 85 86 int 87 iswalpha(wint_t c) 88 { 89 return (__isctype_w((c), _CTYPE_A)); 90 } 91 92 int 93 iswblank(wint_t c) 94 { 95 return (__isctype_w((c), _CTYPE_B)); 96 } 97 98 int 99 iswcntrl(wint_t c) 100 { 101 return (__isctype_w((c), _CTYPE_C)); 102 } 103 104 int 105 iswdigit(wint_t c) 106 { 107 return (__isctype_w((c), _CTYPE_D)); 108 } 109 110 int 111 iswgraph(wint_t c) 112 { 113 return (__isctype_w((c), _CTYPE_G)); 114 } 115 116 int 117 iswlower(wint_t c) 118 { 119 return (__isctype_w((c), _CTYPE_L)); 120 } 121 122 int 123 iswprint(wint_t c) 124 { 125 return (__isctype_w((c), _CTYPE_R)); 126 } 127 128 int 129 iswpunct(wint_t c) 130 { 131 return (__isctype_w((c), _CTYPE_P)); 132 } 133 134 int 135 iswspace(wint_t c) 136 { 137 return (__isctype_w((c), _CTYPE_S)); 138 } 139 DEF_STRONG(iswspace); 140 141 int 142 iswupper(wint_t c) 143 { 144 return (__isctype_w((c), _CTYPE_U)); 145 } 146 DEF_STRONG(iswupper); 147 148 int 149 iswxdigit(wint_t c) 150 { 151 return (__isctype_w((c), _CTYPE_X)); 152 } 153 154 wint_t 155 towupper(wint_t c) 156 { 157 return (__toupper_w(c)); 158 } 159 160 wint_t 161 towlower(wint_t c) 162 { 163 return (__tolower_w(c)); 164 } 165 DEF_STRONG(towlower); 166 167 int 168 wcwidth(wchar_t c) 169 { 170 if (__isctype_w((c), _CTYPE_R)) 171 return (((unsigned)__runetype_w(c) & _CTYPE_SWM) >> _CTYPE_SWS); 172 return -1; 173 } 174 DEF_WEAK(wcwidth); 175 176 wctrans_t 177 wctrans(const char *charclass) 178 { 179 int i; 180 _RuneLocale *rl = _CurrentRuneLocale; 181 182 if (rl->rl_wctrans[_WCTRANS_INDEX_LOWER].te_name==NULL) 183 _wctrans_init(rl); 184 185 for (i=0; i<_WCTRANS_NINDEXES; i++) 186 if (!strcmp(rl->rl_wctrans[i].te_name, charclass)) 187 return ((wctrans_t)&rl->rl_wctrans[i]); 188 189 return ((wctrans_t)NULL); 190 } 191 192 wint_t 193 towctrans(wint_t c, wctrans_t desc) 194 { 195 if (desc==NULL) { 196 errno = EINVAL; 197 return (c); 198 } 199 return (_towctrans(c, (_WCTransEntry *)desc)); 200 } 201 202 wctype_t 203 wctype(const char *property) 204 { 205 int i; 206 _RuneLocale *rl = _CurrentRuneLocale; 207 208 for (i=0; i<_WCTYPE_NINDEXES; i++) 209 if (!strcmp(rl->rl_wctype[i].te_name, property)) 210 return ((wctype_t)&rl->rl_wctype[i]); 211 return ((wctype_t)NULL); 212 } 213 214 int 215 iswctype(wint_t c, wctype_t charclass) 216 { 217 218 /* 219 * SUSv3: If charclass is 0, iswctype() shall return 0. 220 */ 221 if (charclass == (wctype_t)0) { 222 return 0; 223 } 224 225 return (__isctype_w(c, ((_WCTypeEntry *)charclass)->te_mask)); 226 } 227