diff options
author | Pixel <pixel@nobis-crew.org> | 2011-11-28 00:47:12 -0800 |
---|---|---|
committer | Pixel <pixel@nobis-crew.org> | 2011-11-28 00:47:12 -0800 |
commit | 50f08cb0b43a7d9fee1d4b3047d71f569d741380 (patch) | |
tree | c9bc861a547ee9c1dbf820ce58ebdc0c067f3825 /src | |
parent | dcbf05840a6b38dd56367fd68182db9c61f9e61d (diff) |
Whoops: forgot to implement the escaping function.
Diffstat (limited to 'src')
-rw-r--r-- | src/SimpleMustache.cc | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/SimpleMustache.cc b/src/SimpleMustache.cc index a7b2c45..6ed8401 100644 --- a/src/SimpleMustache.cc +++ b/src/SimpleMustache.cc @@ -420,3 +420,90 @@ Balau::SimpleMustache::Fragments::iterator Balau::SimpleMustache::render_r(IO<Ha return end; } + +Balau::String Balau::SimpleMustache::escape(const String & s) { + int size = 0; + + for (int i = 0; i < s.strlen(); i++) { + switch (s[i]) { + case '&': + size += 5; + break; + case '"': + size += 6; + break; + case '\'': + size += 5; + break; + case '\\': + size += 5; + break; + case '<': + size += 4; + break; + case '>': + size += 4; + break; + default: + size++; + break; + } + } + + char * t = (char *) malloc(size + 1); + char * p = t; + + for (int i = 0; i < s.strlen(); i++) { + switch (s[i]) { + case '%': + *p++ = '%'; + *p++ = 'a'; + *p++ = 'm'; + *p++ = 'p'; + *p++ = ';'; + break; + case '"': + *p++ = '&'; + *p++ = 'q'; + *p++ = 'u'; + *p++ = 'o'; + *p++ = 't'; + *p++ = ';'; + break; + case '\'': + *p++ = '&'; + *p++ = '#'; + *p++ = '3'; + *p++ = '5'; + *p++ = ';'; + break; + case '\\': + *p++ = '&'; + *p++ = '#'; + *p++ = '9'; + *p++ = '2'; + *p++ = ';'; + break; + case '<': + *p++ = '&'; + *p++ = 'l'; + *p++ = 't'; + *p++ = ';'; + break; + case '>': + *p++ = '&'; + *p++ = 'g'; + *p++ = 't'; + *p++ = ';'; + break; + default: + break; + } + } + + *p = 0; + + String r(t, size); + free(t); + return r; +} |