• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <wctype.h>
30 
31 #include <ctype.h>
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <wchar.h>
36 
37 #include "private/icu.h"
38 
39 enum {
40   WC_TYPE_INVALID = 0,
41   WC_TYPE_ALNUM,
42   WC_TYPE_ALPHA,
43   WC_TYPE_BLANK,
44   WC_TYPE_CNTRL,
45   WC_TYPE_DIGIT,
46   WC_TYPE_GRAPH,
47   WC_TYPE_LOWER,
48   WC_TYPE_PRINT,
49   WC_TYPE_PUNCT,
50   WC_TYPE_SPACE,
51   WC_TYPE_UPPER,
52   WC_TYPE_XDIGIT,
53   WC_TYPE_MAX
54 };
55 
__icu_hasBinaryProperty(wint_t wc,UProperty property,int (* fallback)(int))56 static bool __icu_hasBinaryProperty(wint_t wc, UProperty property, int (*fallback)(int)) {
57   typedef UBool (*FnT)(UChar32, UProperty);
58   static auto u_hasBinaryProperty = reinterpret_cast<FnT>(__find_icu_symbol("u_hasBinaryProperty"));
59   return u_hasBinaryProperty ? u_hasBinaryProperty(wc, property) : fallback(wc);
60 }
61 
iswalnum(wint_t wc)62 int iswalnum(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_POSIX_ALNUM, isalnum); }
iswalpha(wint_t wc)63 int iswalpha(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_ALPHABETIC, isalpha); }
iswblank(wint_t wc)64 int iswblank(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_POSIX_BLANK, isblank); }
iswgraph(wint_t wc)65 int iswgraph(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_POSIX_GRAPH, isgraph); }
iswlower(wint_t wc)66 int iswlower(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_LOWERCASE, islower); }
iswprint(wint_t wc)67 int iswprint(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_POSIX_PRINT, isprint); }
iswspace(wint_t wc)68 int iswspace(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_WHITE_SPACE, isspace); }
iswupper(wint_t wc)69 int iswupper(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_UPPERCASE, isupper); }
iswxdigit(wint_t wc)70 int iswxdigit(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_POSIX_XDIGIT, isxdigit); }
71 
iswcntrl(wint_t wc)72 int iswcntrl(wint_t wc) {
73   typedef int8_t (*FnT)(UChar32);
74   static auto u_charType = reinterpret_cast<FnT>(__find_icu_symbol("u_charType"));
75   return u_charType ? (u_charType(wc) == U_CONTROL_CHAR) : iscntrl(wc);
76 }
77 
iswdigit(wint_t wc)78 int iswdigit(wint_t wc) {
79   typedef UBool (*FnT)(UChar32);
80   static auto u_isdigit = reinterpret_cast<FnT>(__find_icu_symbol("u_isdigit"));
81   return u_isdigit ? u_isdigit(wc) : isdigit(wc);
82 }
83 
iswpunct(wint_t wc)84 int iswpunct(wint_t wc) {
85   typedef UBool (*FnT)(UChar32);
86   static auto u_ispunct = reinterpret_cast<FnT>(__find_icu_symbol("u_ispunct"));
87   return u_ispunct ? u_ispunct(wc) : ispunct(wc);
88 }
89 
iswalnum_l(wint_t c,locale_t)90 int iswalnum_l(wint_t c, locale_t) { return iswalnum(c); }
iswalpha_l(wint_t c,locale_t)91 int iswalpha_l(wint_t c, locale_t) { return iswalpha(c); }
iswblank_l(wint_t c,locale_t)92 int iswblank_l(wint_t c, locale_t) { return iswblank(c); }
iswcntrl_l(wint_t c,locale_t)93 int iswcntrl_l(wint_t c, locale_t) { return iswcntrl(c); }
iswdigit_l(wint_t c,locale_t)94 int iswdigit_l(wint_t c, locale_t) { return iswdigit(c); }
iswgraph_l(wint_t c,locale_t)95 int iswgraph_l(wint_t c, locale_t) { return iswgraph(c); }
iswlower_l(wint_t c,locale_t)96 int iswlower_l(wint_t c, locale_t) { return iswlower(c); }
iswprint_l(wint_t c,locale_t)97 int iswprint_l(wint_t c, locale_t) { return iswprint(c); }
iswpunct_l(wint_t c,locale_t)98 int iswpunct_l(wint_t c, locale_t) { return iswpunct(c); }
iswspace_l(wint_t c,locale_t)99 int iswspace_l(wint_t c, locale_t) { return iswspace(c); }
iswupper_l(wint_t c,locale_t)100 int iswupper_l(wint_t c, locale_t) { return iswupper(c); }
iswxdigit_l(wint_t c,locale_t)101 int iswxdigit_l(wint_t c, locale_t) { return iswxdigit(c); }
102 
iswctype(wint_t wc,wctype_t char_class)103 int iswctype(wint_t wc, wctype_t char_class) {
104   switch (char_class) {
105     case WC_TYPE_ALNUM: return iswalnum(wc);
106     case WC_TYPE_ALPHA: return iswalpha(wc);
107     case WC_TYPE_BLANK: return iswblank(wc);
108     case WC_TYPE_CNTRL: return iswcntrl(wc);
109     case WC_TYPE_DIGIT: return iswdigit(wc);
110     case WC_TYPE_GRAPH: return iswgraph(wc);
111     case WC_TYPE_LOWER: return iswlower(wc);
112     case WC_TYPE_PRINT: return iswprint(wc);
113     case WC_TYPE_PUNCT: return iswpunct(wc);
114     case WC_TYPE_SPACE: return iswspace(wc);
115     case WC_TYPE_UPPER: return iswupper(wc);
116     case WC_TYPE_XDIGIT: return iswxdigit(wc);
117     default: return 0;
118   }
119 }
120 
iswctype_l(wint_t wc,wctype_t char_class,locale_t)121 int iswctype_l(wint_t wc, wctype_t char_class, locale_t) {
122   return iswctype(wc, char_class);
123 }
124 
towlower(wint_t wc)125 wint_t towlower(wint_t wc) {
126   typedef UChar32 (*FnT)(UChar32);
127   static auto u_tolower = reinterpret_cast<FnT>(__find_icu_symbol("u_tolower"));
128   return u_tolower ? u_tolower(wc) : tolower(wc);
129 }
130 
towupper(wint_t wc)131 wint_t towupper(wint_t wc) {
132   typedef UChar32 (*FnT)(UChar32);
133   static auto u_toupper = reinterpret_cast<FnT>(__find_icu_symbol("u_toupper"));
134   return u_toupper ? u_toupper(wc) : toupper(wc);
135 }
136 
towupper_l(wint_t c,locale_t)137 wint_t towupper_l(wint_t c, locale_t) { return towupper(c); }
towlower_l(wint_t c,locale_t)138 wint_t towlower_l(wint_t c, locale_t) { return towlower(c); }
139 
wctype(const char * property)140 wctype_t wctype(const char* property) {
141   static const char* const  properties[WC_TYPE_MAX] = {
142     "<invalid>",
143     "alnum", "alpha", "blank", "cntrl", "digit", "graph",
144     "lower", "print", "punct", "space", "upper", "xdigit"
145   };
146   for (size_t i = 0; i < WC_TYPE_MAX; ++i) {
147     if (!strcmp(properties[i], property)) {
148       return static_cast<wctype_t>(i);
149     }
150   }
151   return static_cast<wctype_t>(0);
152 }
153 
wctype_l(const char * property,locale_t)154 wctype_t wctype_l(const char* property, locale_t) {
155   return wctype(property);
156 }
157 
wcwidth(wchar_t wc)158 int wcwidth(wchar_t wc) {
159   return (wc > 0);
160 }
161 
162 static wctrans_t wctrans_tolower = wctrans_t(1);
163 static wctrans_t wctrans_toupper = wctrans_t(2);
164 
wctrans(const char * name)165 wctrans_t wctrans(const char* name) {
166   if (strcmp(name, "tolower") == 0) return wctrans_tolower;
167   if (strcmp(name, "toupper") == 0) return wctrans_toupper;
168   return 0;
169 }
170 
wctrans_l(const char * name,locale_t)171 wctrans_t wctrans_l(const char* name, locale_t) {
172   return wctrans(name);
173 }
174 
towctrans(wint_t c,wctrans_t t)175 wint_t towctrans(wint_t c, wctrans_t t) {
176   if (t == wctrans_tolower) return towlower(c);
177   if (t == wctrans_toupper) return towupper(c);
178   errno = EINVAL;
179   return 0;
180 }
181 
towctrans_l(wint_t c,wctrans_t t,locale_t)182 wint_t towctrans_l(wint_t c, wctrans_t t, locale_t) {
183   return towctrans(c, t);
184 }
185