Browse Source

handle \n etc

master
Felix von Leitner 3 years ago
parent
commit
3324ee127c
2 changed files with 14 additions and 1 deletions
  1. +9
    -1
      textcode/fmt_jsonescape.c
  2. +5
    -0
      textcode/scan_jsonescape.c

+ 9
- 1
textcode/fmt_jsonescape.c View File

@@ -7,16 +7,24 @@
size_t fmt_jsonescape(char* dest,const char* src,size_t len) {
register const unsigned char* s=(const unsigned char*) src;
size_t written=0,i;
char c;
for (i=0; i<len; ++i) {
switch (s[i]) {
case '\\':
case '"':
c=s[i];
escape:
if (dest) {
dest[written]='\\';
dest[written+1]=s[i];
dest[written+1]=c;
}
written+=2;
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:
if (s[i]<' ') {
if (dest) {


+ 5
- 0
textcode/scan_jsonescape.c View File

@@ -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
// c='\\'; // c already is backslash
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':
{
size_t j;


Loading…
Cancel
Save