From e00b7a672258d320789ee2efd9ed34307e68bf95 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Mon, 29 Dec 2014 23:21:41 -0800 Subject: Attempted optimization of cmark_ctype. Use a single lookup table for all character types. I'm not sure this actually helps so much. --- src/cmark_ctype.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'src/cmark_ctype.c') 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 + +/** 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; } -- cgit v1.2.3