cmark

My personal build of CMark ✏️

Commit
e00b7a672258d320789ee2efd9ed34307e68bf95
Parent
1ada72c625b2005d9b03d86bc72ce1597740bf7a
Author
John MacFarlane <jgm@berkeley.edu>
Date

Attempted optimization of cmark_ctype.

Use a single lookup table for all character types. I'm not sure this actually helps so much.

Diffstat

1 file changed, 29 insertions, 12 deletions

Status File Name N° Changes Insertions Deletions
Modified src/cmark_ctype.c 41 29 12
diff --git 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;
 }