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 <ctype.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <wchar.h>
33 #include <wctype.h>
34 
35 // TODO: these only work for the ASCII range; rewrite to dlsym icu4c? http://b/14499654
36 
iswalnum(wint_t wc)37 int iswalnum(wint_t wc) { return isalnum(wc); }
iswalpha(wint_t wc)38 int iswalpha(wint_t wc) { return isalpha(wc); }
iswblank(wint_t wc)39 int iswblank(wint_t wc) { return isblank(wc); }
iswcntrl(wint_t wc)40 int iswcntrl(wint_t wc) { return iscntrl(wc); }
iswdigit(wint_t wc)41 int iswdigit(wint_t wc) { return isdigit(wc); }
iswgraph(wint_t wc)42 int iswgraph(wint_t wc) { return isgraph(wc); }
iswlower(wint_t wc)43 int iswlower(wint_t wc) { return islower(wc); }
iswprint(wint_t wc)44 int iswprint(wint_t wc) { return isprint(wc); }
iswpunct(wint_t wc)45 int iswpunct(wint_t wc) { return ispunct(wc); }
iswspace(wint_t wc)46 int iswspace(wint_t wc) { return isspace(wc); }
iswupper(wint_t wc)47 int iswupper(wint_t wc) { return isupper(wc); }
iswxdigit(wint_t wc)48 int iswxdigit(wint_t wc) { return isxdigit(wc); }
49 
iswalnum_l(wint_t c,locale_t)50 int iswalnum_l(wint_t c, locale_t) { return iswalnum(c); }
iswalpha_l(wint_t c,locale_t)51 int iswalpha_l(wint_t c, locale_t) { return iswalpha(c); }
iswblank_l(wint_t c,locale_t)52 int iswblank_l(wint_t c, locale_t) { return iswblank(c); }
iswcntrl_l(wint_t c,locale_t)53 int iswcntrl_l(wint_t c, locale_t) { return iswcntrl(c); }
iswdigit_l(wint_t c,locale_t)54 int iswdigit_l(wint_t c, locale_t) { return iswdigit(c); }
iswgraph_l(wint_t c,locale_t)55 int iswgraph_l(wint_t c, locale_t) { return iswgraph(c); }
iswlower_l(wint_t c,locale_t)56 int iswlower_l(wint_t c, locale_t) { return iswlower(c); }
iswprint_l(wint_t c,locale_t)57 int iswprint_l(wint_t c, locale_t) { return iswprint(c); }
iswpunct_l(wint_t c,locale_t)58 int iswpunct_l(wint_t c, locale_t) { return iswpunct(c); }
iswspace_l(wint_t c,locale_t)59 int iswspace_l(wint_t c, locale_t) { return iswspace(c); }
iswupper_l(wint_t c,locale_t)60 int iswupper_l(wint_t c, locale_t) { return iswupper(c); }
iswxdigit_l(wint_t c,locale_t)61 int iswxdigit_l(wint_t c, locale_t) { return iswxdigit(c); }
62 
iswctype(wint_t wc,wctype_t char_class)63 int iswctype(wint_t wc, wctype_t char_class) {
64   switch (char_class) {
65     case WC_TYPE_ALNUM: return iswalnum(wc);
66     case WC_TYPE_ALPHA: return iswalpha(wc);
67     case WC_TYPE_BLANK: return iswblank(wc);
68     case WC_TYPE_CNTRL: return iswcntrl(wc);
69     case WC_TYPE_DIGIT: return iswdigit(wc);
70     case WC_TYPE_GRAPH: return iswgraph(wc);
71     case WC_TYPE_LOWER: return iswlower(wc);
72     case WC_TYPE_PRINT: return iswprint(wc);
73     case WC_TYPE_PUNCT: return iswpunct(wc);
74     case WC_TYPE_SPACE: return iswspace(wc);
75     case WC_TYPE_UPPER: return iswupper(wc);
76     case WC_TYPE_XDIGIT: return iswxdigit(wc);
77     default: return 0;
78   }
79 }
80 
iswctype_l(wint_t wc,wctype_t char_class,locale_t)81 int iswctype_l(wint_t wc, wctype_t char_class, locale_t) {
82   return iswctype(wc, char_class);
83 }
84 
towlower(wint_t wc)85 wint_t towlower(wint_t wc) { return tolower(wc); }
towupper(wint_t wc)86 wint_t towupper(wint_t wc) { return toupper(wc); }
87 
towupper_l(int c,locale_t)88 int towupper_l(int c, locale_t) { return towupper(c); }
towlower_l(int c,locale_t)89 int towlower_l(int c, locale_t) { return towlower(c); }
90 
wctype(const char * property)91 wctype_t wctype(const char* property) {
92   static const char* const  properties[WC_TYPE_MAX] = {
93     "<invalid>",
94     "alnum", "alpha", "blank", "cntrl", "digit", "graph",
95     "lower", "print", "punct", "space", "upper", "xdigit"
96   };
97   for (size_t i = 0; i < WC_TYPE_MAX; ++i) {
98     if (!strcmp(properties[i], property)) {
99       return static_cast<wctype_t>(i);
100     }
101   }
102   return static_cast<wctype_t>(0);
103 }
104 
wctype_l(const char * property,locale_t)105 wctype_t wctype_l(const char* property, locale_t) {
106   return wctype(property);
107 }
108 
wcwidth(wchar_t wc)109 int wcwidth(wchar_t wc) {
110   return (wc > 0);
111 }
112