Repository: lucy-clownfish Updated Branches: refs/heads/master bd503cb73 -> e4a94bca5
Add safe ctype.h wrappers >From the C99 spec: The header <ctype.h> declares several functions useful for classifying and mapping characters. In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined. The behavior of these functions is affected by the current locale. Implementations using a superset of ASCII are assumed. Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/fb28c3c8 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/fb28c3c8 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/fb28c3c8 Branch: refs/heads/master Commit: fb28c3c8c707a6c289553a7480922134bfb0e110 Parents: 658d118 Author: Nick Wellnhofer <wellnho...@aevum.de> Authored: Wed Apr 6 13:03:44 2016 +0200 Committer: Nick Wellnhofer <wellnho...@aevum.de> Committed: Wed Apr 6 13:03:44 2016 +0200 ---------------------------------------------------------------------- compiler/src/CFCUtil.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ compiler/src/CFCUtil.h | 27 +++++++++++++++++++++++++ 2 files changed, 76 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/fb28c3c8/compiler/src/CFCUtil.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCUtil.c b/compiler/src/CFCUtil.c index f505cac..83c30e2 100644 --- a/compiler/src/CFCUtil.c +++ b/compiler/src/CFCUtil.c @@ -308,6 +308,55 @@ CFCUtil_wrapped_free(void *ptr) { free(ptr); } +// Avoid -Wtype-limits warning. +#if CHAR_MAX <= 127 + #define IS_ASCII(c) ((c) >= 0) +#else + #define IS_ASCII(c) ((c) >= 0 && (c) <= 127) +#endif + +int +CFCUtil_isalnum(char c) { + return IS_ASCII(c) && isalnum(c); +} + +int +CFCUtil_isalpha(char c) { + return IS_ASCII(c) && isalpha(c); +} + +int +CFCUtil_isdigit(char c) { + return IS_ASCII(c) && isdigit(c); +} + +int +CFCUtil_islower(char c) { + return IS_ASCII(c) && islower(c); +} + +int +CFCUtil_isspace(char c) { + return IS_ASCII(c) && isspace(c); +} + +int +CFCUtil_isupper(char c) { + return IS_ASCII(c) && isupper(c); +} + +char +CFCUtil_tolower(char c) { + if (!IS_ASCII(c)) { return c; } + return (char)tolower(c); +} + +char +CFCUtil_toupper(char c) { + if (!IS_ASCII(c)) { return c; } + return (char)toupper(c); +} + int CFCUtil_current(const char *orig, const char *dest) { // If the destination file doesn't exist, we're not current. http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/fb28c3c8/compiler/src/CFCUtil.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCUtil.h b/compiler/src/CFCUtil.h index 73286ef..ea66400 100644 --- a/compiler/src/CFCUtil.h +++ b/compiler/src/CFCUtil.h @@ -153,6 +153,33 @@ CFCUtil_wrapped_free(void *ptr); #define FREEMEM(_ptr) \ CFCUtil_wrapped_free(_ptr) +/** Safe wrappers for ctype.h functions. + */ + +int +CFCUtil_isalnum(char c); + +int +CFCUtil_isalpha(char c); + +int +CFCUtil_isdigit(char c); + +int +CFCUtil_islower(char c); + +int +CFCUtil_isspace(char c); + +int +CFCUtil_isupper(char c); + +char +CFCUtil_tolower(char c); + +char +CFCUtil_toupper(char c); + /** Given two filepaths, return true if the second exists and has a * modification time which more recent than that of the first. */