From 2282735a4c0d6e85e01c4caab637af457a3c0015 Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Sat, 22 Dec 2012 00:56:39 +0000 Subject: [PATCH] Add ctype-style APIs that are compatible with wctype.h. --- Makefile.am | 3 +- src/ctype.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++ src/include/ucd/ucd.h | 80 +++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 src/ctype.cpp diff --git a/Makefile.am b/Makefile.am index 805b2770..8c885b24 100644 --- a/Makefile.am +++ b/Makefile.am @@ -68,4 +68,5 @@ lib_LTLIBRARIES += src/libucd.la src_libucd_la_LDFLAGS = -version-info $(LIBUCD_VERSION) src_libucd_la_CXXFLAGS = ${AM_CXXFLAGS} src_libucd_la_SOURCES = \ - src/categories.cpp + src/categories.cpp \ + src/ctype.cpp diff --git a/src/ctype.cpp b/src/ctype.cpp new file mode 100644 index 00000000..4488378e --- /dev/null +++ b/src/ctype.cpp @@ -0,0 +1,116 @@ +/* ctype-style APIs. + * + * Copyright (C) 2012 Reece H. Dunn + * + * This file is part of ucd-tools. + * + * ucd-tools is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ucd-tools is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ucd-tools. If not, see . + */ + +// NOTE: This file is automatically generated from the UnicodeData.txt file in +// the Unicode Character database by the ucd-tools/tools/categories.py script. + +#include "ucd/ucd.h" + +int ucd::isalnum(codepoint_t c) +{ + switch (lookup_category(c)) + { + case Ll: case Lm: case Lo: case Lt: case Lu: + case Nd: case Nl: case No: + return 1; + } + return 0; +} + +int ucd::isalpha(codepoint_t c) +{ + switch (lookup_category(c)) + { + case Ll: case Lm: case Lo: case Lt: case Lu: + return 1; + } + return 0; +} + +int ucd::iscntrl(codepoint_t c) +{ + switch (lookup_category(c)) + { + case Cc: case Zc: + return 1; + } + return 0; +} + +int ucd::isdigit(codepoint_t c) +{ + switch (lookup_category(c)) + { + case Nd: case Nl: case No: + return 1; + } + return 0; +} + +int ucd::isgraph(codepoint_t c) +{ + switch (lookup_category(c)) + { + case Cc: case Cf: case Ci: case Cn: case Co: case Cs: + case Zc: case Zl: case Zp: case Zs: + return 0; + } + return 1; +} + +int ucd::islower(codepoint_t c) +{ + return lookup_category(c) == Ll; +} + +int ucd::isprint(codepoint_t c) +{ + switch (lookup_category(c)) + { + case Cc: case Cf: case Ci: case Cn: case Co: case Cs: + return 0; + } + return 1; +} + +int ucd::ispunct(codepoint_t c) +{ + switch (lookup_category(c)) + { + case Pc: case Pd: case Pe: case Pf: case Pi: case Po: case Ps: + return 1; + } + return 0; +} + +int ucd::isspace(codepoint_t c) +{ + switch (lookup_category(c)) + { + case Zc: case Zl: case Zp: case Zs: + return 1; + } + return 0; +} + +int ucd::isupper(codepoint_t c) +{ + return lookup_category(c) == Lu; +} diff --git a/src/include/ucd/ucd.h b/src/include/ucd/ucd.h index 8242b484..4ac482ee 100644 --- a/src/include/ucd/ucd.h +++ b/src/include/ucd/ucd.h @@ -96,6 +96,86 @@ namespace ucd * @return The General Category of the Unicode codepoint. */ category lookup_category(codepoint_t c); + + + /** @name ctype-style APIs + * @brief These functions provide wctype compatible functions using the UCD data. + */ + //@{ + + + /** @brief Is the codepoint an alpha-numeric character? + * + * @param c The Unicode codepoint to check. + * @return Non-zero if the codepoint is a letter or number, zero otherwise. + */ + int isalnum(codepoint_t c); + + /** @brief Is the codepoint a letter? + * + * @param c The Unicode codepoint to check. + * @return Non-zero if the codepoint is a letter, zero otherwise. + */ + int isalpha(codepoint_t c); + + /** @brief Is the codepoint a control character? + * + * @param c The Unicode codepoint to check. + * @return Non-zero if the codepoint is a control character, zero otherwise. + */ + int iscntrl(codepoint_t c); + + /** @brief Is the codepoint a numeric character? + * + * @param c The Unicode codepoint to check. + * @return Non-zero if the codepoint is a number, zero otherwise. + */ + int isdigit(codepoint_t c); + + /** @brief Does the codepoint have a displayable glyph? + * + * @param c The Unicode codepoint to check. + * @return Non-zero if the codepoint has a displayable glyph, zero otherwise. + */ + int isgraph(codepoint_t c); + + /** @brief Is the codepoint a lower-case letter? + * + * @param c The Unicode codepoint to check. + * @return Non-zero if the codepoint is a lower-case letter, zero otherwise. + */ + int islower(codepoint_t c); + + /** @brief Is the codepoint a printable character? + * + * @param c The Unicode codepoint to check. + * @return Non-zero if the codepoint is a printable character, zero otherwise. + */ + int isprint(codepoint_t c); + + /** @brief Is the codepoint a punctuation character? + * + * @param c The Unicode codepoint to check. + * @return Non-zero if the codepoint is a punctuation character, zero otherwise. + */ + int ispunct(codepoint_t c); + + /** @brief Is the codepoint a whitespace character? + * + * @param c The Unicode codepoint to check. + * @return Non-zero if the codepoint is a whitespace character, zero otherwise. + */ + int isspace(codepoint_t c); + + /** @brief Is the codepoint an upper-case letter? + * + * @param c The Unicode codepoint to check. + * @return Non-zero if the codepoint is an upper-case letter, zero otherwise. + */ + int isupper(codepoint_t c); + + + //@} } #endif