From d321724ae95825d58825b42b553633f2e1e73510 Mon Sep 17 00:00:00 2001 From: lxsang Date: Thu, 18 Jun 2020 09:51:41 +0200 Subject: [PATCH] native unescape json string --- lib/asl/json.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/lib/asl/json.c b/lib/asl/json.c index 509ce5a..8682fd2 100644 --- a/lib/asl/json.c +++ b/lib/asl/json.c @@ -108,10 +108,63 @@ static void stackDump (lua_State *L) { */ static int process_token_string(lua_State* L, jsmntok_t* t, const char* s, int cid) { - // unescape a string - //const char* search_token[8] = {"\\\\","\\\"","\\n","\\t","\\b","\\f","\\r","\\/"}; - //const char* replace_token[8] = {"\\","\"","\n","\t","\b","\f","\r","/"}; - char * str = strndup(s+t[cid].start, t[cid].end-t[cid].start); + // un escape the string + char * str = (char*) malloc(t[cid].end-t[cid].start + 1); + int index = 0; + char c; + uint8_t escape = 0; + + for (int i = t[cid].start; i < t[cid].end; i++) + { + c = *(s+i); + if(c == '\\') + { + if(escape) + { + str[index] = c; + escape = 0; + index++; + } + else + { + escape = 1; + } + } + else + { + if(escape) + { + switch (c) + { + case 'b': + str[index] = '\b'; + break; + case 'f': + str[index] = '\f'; + break; + case 'n': + str[index] = '\n'; + break; + case 'r': + str[index] = '\r'; + break; + case 't': + str[index] = '\t'; + break; + default: + str[index] = c; + } + } + else + { + str[index] = c; + } + index++; + escape = 0; + } + } + str[index] = '\0'; + //strndup(s+t[cid].start, t[cid].end-t[cid].start); // un escape the string /*lua_getglobal(L, "utils"); lua_getfield(L, -1, "unescape"); @@ -125,6 +178,7 @@ static int process_token_string(lua_State* L, jsmntok_t* t, const char* s, int c //stackDump(L); //lua_pushstring(L, str); //printf("%s\n",strndup(s+t[cid].start, t[cid].end-t[cid].start) ); + if(str) free(str); return cid+1; } static int process_token_primitive(lua_State* L, jsmntok_t* t, const char* s, int cid)