wctype.cpp revision c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1
15a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes/*
25a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * Copyright (C) 2008 The Android Open Source Project
35a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * All rights reserved.
45a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes *
55a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * Redistribution and use in source and binary forms, with or without
65a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * modification, are permitted provided that the following conditions
75a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * are met:
85a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes *  * Redistributions of source code must retain the above copyright
95a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes *    notice, this list of conditions and the following disclaimer.
105a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes *  * Redistributions in binary form must reproduce the above copyright
115a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes *    notice, this list of conditions and the following disclaimer in
125a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes *    the documentation and/or other materials provided with the
135a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes *    distribution.
145a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes *
155a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
165a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
175a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
185a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
195a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
205a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
215a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
225a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
235a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
245a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
255a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
265a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes * SUCH DAMAGE.
275a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes */
285a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes
295a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes#include <ctype.h>
305a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes#include <stdlib.h>
315a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes#include <string.h>
325a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes#include <wchar.h>
335a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes
34c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes// TODO: these only work for the ASCII range; rewrite to dlsym icu4c? http://b/14499654
355a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes
365a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswalnum(wint_t wc) { return isalnum(wc); }
375a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswalpha(wint_t wc) { return isalpha(wc); }
385a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswblank(wint_t wc) { return isblank(wc); }
395a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswcntrl(wint_t wc) { return iscntrl(wc); }
405a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswdigit(wint_t wc) { return isdigit(wc); }
415a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswgraph(wint_t wc) { return isgraph(wc); }
425a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswlower(wint_t wc) { return islower(wc); }
435a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswprint(wint_t wc) { return isprint(wc); }
445a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswpunct(wint_t wc) { return ispunct(wc); }
455a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswspace(wint_t wc) { return isspace(wc); }
465a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswupper(wint_t wc) { return isupper(wc); }
475a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswxdigit(wint_t wc) { return isxdigit(wc); }
485a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes
495a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint iswctype(wint_t wc, wctype_t char_class) {
505a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes  switch (char_class) {
51c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes    case WC_TYPE_ALNUM: return iswalnum(wc);
52c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes    case WC_TYPE_ALPHA: return iswalpha(wc);
53c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes    case WC_TYPE_BLANK: return iswblank(wc);
54c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes    case WC_TYPE_CNTRL: return iswcntrl(wc);
55c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes    case WC_TYPE_DIGIT: return iswdigit(wc);
56c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes    case WC_TYPE_GRAPH: return iswgraph(wc);
57c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes    case WC_TYPE_LOWER: return iswlower(wc);
58c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes    case WC_TYPE_PRINT: return iswprint(wc);
59c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes    case WC_TYPE_PUNCT: return iswpunct(wc);
60c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes    case WC_TYPE_SPACE: return iswspace(wc);
61c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes    case WC_TYPE_UPPER: return iswupper(wc);
62c42f5c6fe6f52c9a7082d2a43d0af42326a9c6d1Elliott Hughes    case WC_TYPE_XDIGIT: return iswxdigit(wc);
635a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes    default: return 0;
645a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes  }
655a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes}
665a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes
675a0aa3dee247a313f04252cf45608097695d5953Elliott Hugheswint_t towlower(wint_t wc) { return tolower(wc); }
685a0aa3dee247a313f04252cf45608097695d5953Elliott Hugheswint_t towupper(wint_t wc) { return toupper(wc); }
695a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes
705a0aa3dee247a313f04252cf45608097695d5953Elliott Hugheswctype_t wctype(const char* property) {
715a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes  static const char* const  properties[WC_TYPE_MAX] = {
725a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes    "<invalid>",
735a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes    "alnum", "alpha", "blank", "cntrl", "digit", "graph",
745a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes    "lower", "print", "punct", "space", "upper", "xdigit"
755a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes  };
765a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes  for (size_t i = 0; i < WC_TYPE_MAX; ++i) {
775a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes    if (!strcmp(properties[i], property)) {
785a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes      return static_cast<wctype_t>(i);
795a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes    }
805a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes  }
815a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes  return static_cast<wctype_t>(0);
825a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes}
835a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes
845a0aa3dee247a313f04252cf45608097695d5953Elliott Hughesint wcwidth(wchar_t wc) {
855a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes  return (wc > 0);
865a0aa3dee247a313f04252cf45608097695d5953Elliott Hughes}
87