handle \n etc

master
leitner 8 years ago
parent c29096b2e0
commit c493b262ac

@ -7,16 +7,24 @@
size_t fmt_jsonescape(char* dest,const char* src,size_t len) { size_t fmt_jsonescape(char* dest,const char* src,size_t len) {
register const unsigned char* s=(const unsigned char*) src; register const unsigned char* s=(const unsigned char*) src;
size_t written=0,i; size_t written=0,i;
char c;
for (i=0; i<len; ++i) { for (i=0; i<len; ++i) {
switch (s[i]) { switch (s[i]) {
case '\\': case '\\':
case '"': case '"':
c=s[i];
escape:
if (dest) { if (dest) {
dest[written]='\\'; dest[written]='\\';
dest[written+1]=s[i]; dest[written+1]=c;
} }
written+=2; written+=2;
break; break;
case '\n': c='n'; goto escape;
case '\r': c='r'; goto escape;
case '\b': c='b'; goto escape;
case '\t': c='t'; goto escape;
case '\f': c='f'; goto escape;
default: default:
if (s[i]<' ') { if (s[i]<' ') {
if (dest) { if (dest) {

@ -15,6 +15,11 @@ size_t scan_jsonescape(const char *src,char *dest,size_t *destlen) {
if (prev!=(unsigned int)-1) return 0; // lead surrogate not followed by tail surrogate if (prev!=(unsigned int)-1) return 0; // lead surrogate not followed by tail surrogate
// c='\\'; // c already is backslash // c='\\'; // c already is backslash
break; break;
case 'n': c='\n'; break;
case 'r': c='\r'; break;
case 'b': c='\b'; break;
case 'f': c='\f'; break;
case 't': c='\t'; break;
case 'u': case 'u':
{ {
size_t j; size_t j;

Loading…
Cancel
Save