Smaller and faster entity comparison
This reduces the size by a bit over 100 bytes and improves performance a
tiny bit.
diff --git a/yxml.c b/yxml.c
index a7a613f..4f9f1b4 100644
--- a/yxml.c
+++ b/yxml.c
@@ -109,6 +109,8 @@
* this parser doesn't understand entities with '.', ':', etc, anwyay. */
#define yxml_isRef(c) (yxml_isNum(c) || yxml_isAlpha(c) || c == '#')
+#define INTFROM5CHARS(a, b, c, d, e) ((((uint64_t)(a))<<32) | (((uint64_t)(b))<<24) | (((uint64_t)(c))<<16) | (((uint64_t)(d))<<8) | (uint64_t)(e))
+
/* Set the given char value to ch (0<=ch<=255).
* This can't be done with simple assignment because char may be signed, and
@@ -298,16 +300,13 @@
if(*r)
ch = 0;
} else {
- if(r[0] == 'l' && r[1] == 't' && !r[2])
- ch = '<';
- else if(r[0] == 'g' && r[1] == 't' && !r[2])
- ch = '>';
- else if(r[0] == 'a' && r[1] == 'm' && r[2] == 'p' && !r[3])
- ch = '&';
- else if(r[0] == 'a' && r[1] == 'p' && r[2] == 'o' && r[3] == 's' && !r[4])
- ch = '\'';
- else if(r[0] == 'q' && r[1] == 'u' && r[2] == 'o' && r[3] == 't' && !r[4])
- ch = '"';
+ uint64_t i = INTFROM5CHARS(r[0], r[1], r[2], r[3], r[4]);
+ ch =
+ i == INTFROM5CHARS('l','t', 0, 0, 0) ? '<' :
+ i == INTFROM5CHARS('g','t', 0, 0, 0) ? '>' :
+ i == INTFROM5CHARS('a','m','p', 0, 0) ? '&' :
+ i == INTFROM5CHARS('a','p','o','s',0) ? '\'':
+ i == INTFROM5CHARS('q','u','o','t',0) ? '"' : 0;
}
/* Codepoints not allowed in the XML 1.1 definition of a Char */
diff --git a/yxml.c.in b/yxml.c.in
index 22e0e45..4cc40a8 100644
--- a/yxml.c.in
+++ b/yxml.c.in
@@ -45,6 +45,8 @@
* this parser doesn't understand entities with '.', ':', etc, anwyay. */
#define yxml_isRef(c) (yxml_isNum(c) || yxml_isAlpha(c) || c == '#')
+#define INTFROM5CHARS(a, b, c, d, e) ((((uint64_t)(a))<<32) | (((uint64_t)(b))<<24) | (((uint64_t)(c))<<16) | (((uint64_t)(d))<<8) | (uint64_t)(e))
+
/* Set the given char value to ch (0<=ch<=255).
* This can't be done with simple assignment because char may be signed, and
@@ -234,16 +236,13 @@
if(*r)
ch = 0;
} else {
- if(r[0] == 'l' && r[1] == 't' && !r[2])
- ch = '<';
- else if(r[0] == 'g' && r[1] == 't' && !r[2])
- ch = '>';
- else if(r[0] == 'a' && r[1] == 'm' && r[2] == 'p' && !r[3])
- ch = '&';
- else if(r[0] == 'a' && r[1] == 'p' && r[2] == 'o' && r[3] == 's' && !r[4])
- ch = '\'';
- else if(r[0] == 'q' && r[1] == 'u' && r[2] == 'o' && r[3] == 't' && !r[4])
- ch = '"';
+ uint64_t i = INTFROM5CHARS(r[0], r[1], r[2], r[3], r[4]);
+ ch =
+ i == INTFROM5CHARS('l','t', 0, 0, 0) ? '<' :
+ i == INTFROM5CHARS('g','t', 0, 0, 0) ? '>' :
+ i == INTFROM5CHARS('a','m','p', 0, 0) ? '&' :
+ i == INTFROM5CHARS('a','p','o','s',0) ? '\'':
+ i == INTFROM5CHARS('q','u','o','t',0) ? '"' : 0;
}
/* Codepoints not allowed in the XML 1.1 definition of a Char */