wctype.cpp revision 063e20c26943ec82ef1d53a544545e79054e93d3
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 34// TODO: these only work for the ASCII range; rewrite to dlsym icu4c? http://b/14499654 35 36int iswalnum(wint_t wc) { return isalnum(wc); } 37int iswalpha(wint_t wc) { return isalpha(wc); } 38int iswblank(wint_t wc) { return isblank(wc); } 39int iswcntrl(wint_t wc) { return iscntrl(wc); } 40int iswdigit(wint_t wc) { return isdigit(wc); } 41int iswgraph(wint_t wc) { return isgraph(wc); } 42int iswlower(wint_t wc) { return islower(wc); } 43int iswprint(wint_t wc) { return isprint(wc); } 44int iswpunct(wint_t wc) { return ispunct(wc); } 45int iswspace(wint_t wc) { return isspace(wc); } 46int iswupper(wint_t wc) { return isupper(wc); } 47int iswxdigit(wint_t wc) { return isxdigit(wc); } 48 49int iswctype(wint_t wc, wctype_t char_class) { 50 switch (char_class) { 51 case WC_TYPE_ALNUM: return iswalnum(wc); 52 case WC_TYPE_ALPHA: return iswalpha(wc); 53 case WC_TYPE_BLANK: return iswblank(wc); 54 case WC_TYPE_CNTRL: return iswcntrl(wc); 55 case WC_TYPE_DIGIT: return iswdigit(wc); 56 case WC_TYPE_GRAPH: return iswgraph(wc); 57 case WC_TYPE_LOWER: return iswlower(wc); 58 case WC_TYPE_PRINT: return iswprint(wc); 59 case WC_TYPE_PUNCT: return iswpunct(wc); 60 case WC_TYPE_SPACE: return iswspace(wc); 61 case WC_TYPE_UPPER: return iswupper(wc); 62 case WC_TYPE_XDIGIT: return iswxdigit(wc); 63 default: return 0; 64 } 65} 66 67wint_t towlower(wint_t wc) { return tolower(wc); } 68wint_t towupper(wint_t wc) { return toupper(wc); } 69 70wctype_t wctype(const char* property) { 71 static const char* const properties[WC_TYPE_MAX] = { 72 "<invalid>", 73 "alnum", "alpha", "blank", "cntrl", "digit", "graph", 74 "lower", "print", "punct", "space", "upper", "xdigit" 75 }; 76 for (size_t i = 0; i < WC_TYPE_MAX; ++i) { 77 if (!strcmp(properties[i], property)) { 78 return static_cast<wctype_t>(i); 79 } 80 } 81 return static_cast<wctype_t>(0); 82} 83 84int wcwidth(wchar_t wc) { 85 return (wc > 0); 86} 87