diff --git a/entities.c b/entities.c index 30888bb..9410e1e 100644 --- a/entities.c +++ b/entities.c @@ -9,8 +9,10 @@ #include #include +#include #include #include +#include #define UNICODE_MAX 0x10FFFFul @@ -391,3 +393,33 @@ size_t decode_html_entities_utf8(char *dest, const char *src) return (size_t)(to - dest); } +size_t encode_html_entities(char *dest, const char *src) { + char *to = dest; + for( const char *from = src ; *from ; from++ ) { + if ( *from>=0 ) { + if ( (*from>='A' && *from<='Z') || (*from>='a' && *from<='z') ) { + *to = *from; + to++; + continue; + } + } + unsigned i; + for( i=0 ; iHello!!

"; + static char GOAL[] = "" + "Miguel Leitão\n" + "test@example.org\n" + "<p>Hello!!</p>"; + char OUTPUT[sizeof GOAL]; + char REVERT[sizeof GOAL]; + assert(encode_html_entities(OUTPUT, INPUT) == sizeof GOAL - 1); + assert(strcmp(OUTPUT, GOAL) == 0); + + decode_html_entities_utf8(REVERT, OUTPUT); + assert(strcmp(INPUT, REVERT) == 0); + } + + fprintf(stdout, "All tests passed :-)\n"); return EXIT_SUCCESS; }