summaryrefslogtreecommitdiff
path: root/src/cmark_ctype.c
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2014-12-29 23:21:41 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2014-12-29 23:21:41 -0800
commite00b7a672258d320789ee2efd9ed34307e68bf95 (patch)
tree3a162e2e7a9e8eea8a7709d4a012a23233ba7688 /src/cmark_ctype.c
parent1ada72c625b2005d9b03d86bc72ce1597740bf7a (diff)
Attempted optimization of cmark_ctype.
Use a single lookup table for all character types. I'm not sure this actually helps so much.
Diffstat (limited to 'src/cmark_ctype.c')
-rw-r--r--src/cmark_ctype.c41
1 files changed, 29 insertions, 12 deletions
diff --git a/src/cmark_ctype.c b/src/cmark_ctype.c
index 7ced3a2..8805b9a 100644
--- a/src/cmark_ctype.c
+++ b/src/cmark_ctype.c
@@ -1,12 +1,32 @@
+#include <stdint.h>
+
+/** 1 = space, 2 = punct, 3 = digit, 4 = alpha, 0 = other
+ */
+static const int8_t cmark_ctype_class[256] = {
+/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
+/* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
+/* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+/* 2 */ 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+/* 3 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2,
+/* 4 */ 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+/* 5 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2,
+/* 6 */ 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+/* 7 */ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 0,
+/* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+/* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+/* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+/* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+/* c */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+/* d */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+/* e */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+/* f */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+
/**
* Returns 1 if c is a "whitespace" character as defined by the spec.
*/
int cmark_isspace(char c)
{
- return (c == 0x09 ||
- c == 0x20 ||
- c == 0x0a ||
- c == 0x0d);
+ return cmark_ctype_class[(int8_t)c] == 1;
}
/**
@@ -14,20 +34,17 @@ int cmark_isspace(char c)
*/
int cmark_ispunct(char c)
{
- return ((c >= 33 && c <= 47) ||
- (c >= 58 && c <= 64) ||
- (c >= 91 && c <= 96) ||
- (c >= 123 && c <= 126));
+ return cmark_ctype_class[(int8_t)c] == 2;
}
int cmark_isalnum(char c)
{
- return ((c >= 48 && c <= 57) ||
- (c >= 65 && c <= 90) ||
- (c >= 97 && c <= 122));
+ int8_t result;
+ result = cmark_ctype_class[(int8_t)c];
+ return (result == 3 || result == 4);
}
int cmark_isdigit(char c)
{
- return (c >= 48 && c <= 57);
+ return cmark_ctype_class[(int8_t)c] == 3;
}