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 static bool __icu_hasBinaryProperty(wint_t wc, UProperty property, int (*fallback)(int)) { 40 typedef UBool (*FnT)(UChar32, UProperty); 41 static auto u_hasBinaryProperty = reinterpret_cast<FnT>(__find_icu_symbol("u_hasBinaryProperty")); 42 return u_hasBinaryProperty ? u_hasBinaryProperty(wc, property) : fallback(wc); 43 } 44 45 int iswalnum(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_POSIX_ALNUM, isalnum); } 46 int iswalpha(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_ALPHABETIC, isalpha); } 47 int iswblank(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_POSIX_BLANK, isblank); } 48 int iswgraph(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_POSIX_GRAPH, isgraph); } 49 int iswlower(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_LOWERCASE, islower); } 50 int iswprint(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_POSIX_PRINT, isprint); } 51 int iswspace(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_WHITE_SPACE, isspace); } 52 int iswupper(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_UPPERCASE, isupper); } 53 int iswxdigit(wint_t wc) { return __icu_hasBinaryProperty(wc, UCHAR_POSIX_XDIGIT, isxdigit); } 54 55 int iswcntrl(wint_t wc) { 56 typedef int8_t (*FnT)(UChar32); 57 static auto u_charType = reinterpret_cast<FnT>(__find_icu_symbol("u_charType")); 58 return u_charType ? (u_charType(wc) == U_CONTROL_CHAR) : iscntrl(wc); 59 } 60 61 int iswdigit(wint_t wc) { 62 typedef UBool (*FnT)(UChar32); 63 static auto u_isdigit = reinterpret_cast<FnT>(__find_icu_symbol("u_isdigit")); 64 return u_isdigit ? u_isdigit(wc) : isdigit(wc); 65 } 66 67 int iswpunct(wint_t wc) { 68 typedef UBool (*FnT)(UChar32); 69 static auto u_ispunct = reinterpret_cast<FnT>(__find_icu_symbol("u_ispunct")); 70 return u_ispunct ? u_ispunct(wc) : ispunct(wc); 71 } 72 73 int iswalnum_l(wint_t c, locale_t) { return iswalnum(c); } 74 int iswalpha_l(wint_t c, locale_t) { return iswalpha(c); } 75 int iswblank_l(wint_t c, locale_t) { return iswblank(c); } 76 int iswcntrl_l(wint_t c, locale_t) { return iswcntrl(c); } 77 int iswdigit_l(wint_t c, locale_t) { return iswdigit(c); } 78 int iswgraph_l(wint_t c, locale_t) { return iswgraph(c); } 79 int iswlower_l(wint_t c, locale_t) { return iswlower(c); } 80 int iswprint_l(wint_t c, locale_t) { return iswprint(c); } 81 int iswpunct_l(wint_t c, locale_t) { return iswpunct(c); } 82 int iswspace_l(wint_t c, locale_t) { return iswspace(c); } 83 int iswupper_l(wint_t c, locale_t) { return iswupper(c); } 84 int iswxdigit_l(wint_t c, locale_t) { return iswxdigit(c); } 85 86 int iswctype(wint_t wc, wctype_t char_class) { 87 switch (char_class) { 88 case WC_TYPE_ALNUM: return iswalnum(wc); 89 case WC_TYPE_ALPHA: return iswalpha(wc); 90 case WC_TYPE_BLANK: return iswblank(wc); 91 case WC_TYPE_CNTRL: return iswcntrl(wc); 92 case WC_TYPE_DIGIT: return iswdigit(wc); 93 case WC_TYPE_GRAPH: return iswgraph(wc); 94 case WC_TYPE_LOWER: return iswlower(wc); 95 case WC_TYPE_PRINT: return iswprint(wc); 96 case WC_TYPE_PUNCT: return iswpunct(wc); 97 case WC_TYPE_SPACE: return iswspace(wc); 98 case WC_TYPE_UPPER: return iswupper(wc); 99 case WC_TYPE_XDIGIT: return iswxdigit(wc); 100 default: return 0; 101 } 102 } 103 104 int iswctype_l(wint_t wc, wctype_t char_class, locale_t) { 105 return iswctype(wc, char_class); 106 } 107 108 wint_t towlower(wint_t wc) { 109 typedef UChar32 (*FnT)(UChar32); 110 static auto u_tolower = reinterpret_cast<FnT>(__find_icu_symbol("u_tolower")); 111 return u_tolower ? u_tolower(wc) : tolower(wc); 112 } 113 114 wint_t towupper(wint_t wc) { 115 typedef UChar32 (*FnT)(UChar32); 116 static auto u_toupper = reinterpret_cast<FnT>(__find_icu_symbol("u_toupper")); 117 return u_toupper ? u_toupper(wc) : toupper(wc); 118 } 119 120 wint_t towupper_l(wint_t c, locale_t) { return towupper(c); } 121 wint_t towlower_l(wint_t c, locale_t) { return towlower(c); } 122 123 wctype_t wctype(const char* property) { 124 static const char* const properties[WC_TYPE_MAX] = { 125 "<invalid>", 126 "alnum", "alpha", "blank", "cntrl", "digit", "graph", 127 "lower", "print", "punct", "space", "upper", "xdigit" 128 }; 129 for (size_t i = 0; i < WC_TYPE_MAX; ++i) { 130 if (!strcmp(properties[i], property)) { 131 return static_cast<wctype_t>(i); 132 } 133 } 134 return static_cast<wctype_t>(0); 135 } 136 137 wctype_t wctype_l(const char* property, locale_t) { 138 return wctype(property); 139 } 140 141 int wcwidth(wchar_t wc) { 142 return (wc > 0); 143 } 144 145 static wctrans_t wctrans_tolower = wctrans_t(1); 146 static wctrans_t wctrans_toupper = wctrans_t(2); 147 148 wctrans_t wctrans(const char* name) { 149 if (strcmp(name, "tolower") == 0) return wctrans_tolower; 150 if (strcmp(name, "toupper") == 0) return wctrans_toupper; 151 return 0; 152 } 153 154 wctrans_t wctrans_l(const char* name, locale_t) { 155 return wctrans(name); 156 } 157 158 wint_t towctrans(wint_t c, wctrans_t t) { 159 if (t == wctrans_tolower) return towlower(c); 160 if (t == wctrans_toupper) return towupper(c); 161 errno = EINVAL; 162 return 0; 163 } 164 165 wint_t towctrans_l(wint_t c, wctrans_t t, locale_t) { 166 return towctrans(c, t); 167 } 168