summaryrefslogtreecommitdiff
path: root/include/libHX/ctype_helper.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/libHX/ctype_helper.h')
-rw-r--r--include/libHX/ctype_helper.h86
1 files changed, 86 insertions, 0 deletions
diff --git a/include/libHX/ctype_helper.h b/include/libHX/ctype_helper.h
new file mode 100644
index 0000000..2ba0a7a
--- /dev/null
+++ b/include/libHX/ctype_helper.h
@@ -0,0 +1,86 @@
+#ifndef _LIBHX_CTYPE_H
+#define _LIBHX_CTYPE_H 1
+
+#ifdef __cplusplus
+# include <cctype>
+#else
+# include <ctype.h>
+# include <stdbool.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * ctype.h workarounds. The is*() functions takes an int, but people
+ * commonly pass in a char. Because char can technically be signed, the
+ * value would be sign-extended during promotion to the int type which the
+ * ctype family functions take.
+ *
+ * The HX_ ctype related functions therefore explicitly take an unsigned char,
+ * and thus knowingly make it impossible to pass in stdio's "EOF" (-1).
+ * [When was the last time you did a isalpha(EOF) anyway?]
+ *
+ * Because, again, this all works due to implicit type conversion, these
+ * wrappers look rather plain. Oh, also note we are returning the much
+ * more modern "bool".
+ *
+ * And not all ctype functions are provided - no need so far, and I do not
+ * want to clutter it before needing it.
+ */
+static __inline__ bool HX_isalnum(unsigned char c)
+{
+ return isalnum(c);
+}
+
+static __inline__ bool HX_isalpha(unsigned char c)
+{
+ return isalpha(c);
+}
+
+static __inline__ bool HX_isdigit(unsigned char c)
+{
+ return isdigit(c);
+}
+
+static __inline__ bool HX_islower(unsigned char c)
+{
+ return islower(c);
+}
+
+static __inline__ bool HX_isprint(unsigned char c)
+{
+ return isprint(c);
+}
+
+static __inline__ bool HX_isspace(unsigned char c)
+{
+ return isspace(c);
+}
+
+static __inline__ bool HX_isupper(unsigned char c)
+{
+ return isupper(c);
+}
+
+static __inline__ bool HX_isxdigit(unsigned char c)
+{
+ return isxdigit(c);
+}
+
+static __inline__ unsigned char HX_tolower(unsigned char c)
+{
+ return tolower(c);
+}
+
+static __inline__ unsigned char HX_toupper(unsigned char c)
+{
+ return toupper(c);
+}
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* _LIBHX_CTYPE_H */