Skip to content

Instantly share code, notes, and snippets.

@mattn
Last active June 29, 2016 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattn/77d3c09e1b193b8d1ddaef86961483f2 to your computer and use it in GitHub Desktop.
Save mattn/77d3c09e1b193b8d1ddaef86961483f2 to your computer and use it in GitHub Desktop.
diff --git a/bench.c b/bench.c
index 5f107c8..a18e099 100644
--- a/bench.c
+++ b/bench.c
@@ -55,6 +55,7 @@ int main(void) {
}
static void simple_escape_html(char *dst, const char *input, size_t input_size) {
+#if 0
for (int i = 0; i < input_size; i++) {
const char c = *(input++);
switch (c) {
@@ -102,5 +103,27 @@ static void simple_escape_html(char *dst, const char *input, size_t input_size)
}
}
*dst++ = *"\0";
+#endif
+ const char *ptr = input, *end = input + input_size;
+#define STRCPY(d,s) memcpy(d,s,sizeof(s)-1); d += sizeof(s)-1;
+ while (ptr < end) {
+ if (*ptr < 48) {
+ switch (*ptr) {
+ case '&': STRCPY(dst,"&amp;"); break;
+ case '>': STRCPY(dst,"&gt;"); break;
+ case '<': STRCPY(dst,"&lt;"); break;
+ case '"': STRCPY(dst,"&quot;"); break;
+ case '\'': STRCPY(dst,"&#39;"); break;
+ case '`': STRCPY(dst,"&#96;"); break;
+ case '{': STRCPY(dst,"&#123;"); break;
+ case '}': STRCPY(dst,"&#125;"); break;
+ default: *dst++ = *ptr; break;
+ }
+ ptr++;
+ } else
+ *dst++ = *ptr++;
+ }
+#undef STRCPY
+ *dst++ = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment