# HG changeset patch # User jbe # Date 1406816679 -7200 # Node ID 3b8c1e2aef9cb257db28262f9cd5e21d6ae3fcad # Parent d476b3c8960df4ff0b72b4d0acc99e8f0e4c373a Some code cleanup in json.import(...) function diff -r d476b3c8960d -r 3b8c1e2aef9c libraries/json/json.c --- a/libraries/json/json.c Thu Jul 31 13:22:35 2014 +0200 +++ b/libraries/json/json.c Thu Jul 31 16:24:39 2014 +0200 @@ -123,7 +123,7 @@ char c; // variable to store a single character to be processed luaL_Buffer luabuf; // Lua buffer to decode JSON string values char *cbuf; // C buffer to decode JSON string values - size_t writepos; // write position of decoded strings in C buffer + size_t outlen; // maximum length or write position of C buffer size_t arraylen; // variable to temporarily store the array length // stack shall contain one function argument: lua_settop(L, 1); @@ -297,28 +297,27 @@ case '"': // consume quote character: pos++; - // determine buffer length: - writepos = pos; - while ((c = str[writepos]) != '"') { + // find last character in input string: + outlen = pos; + while ((c = str[outlen]) != '"') { // consume one character: - writepos++; + outlen++; // handle unexpected end of JSON document: if (c == 0) goto json_import_unexpected_eof; // consume one extra character when encountering an escaped quote: - else if (c == '\\' && str[writepos] == '"') writepos++; + else if (c == '\\' && str[outlen] == '"') outlen++; } - writepos -= pos; + // determine buffer length: + outlen -= pos; // check if string is non empty: - if (writepos) { + if (outlen) { // prepare buffer to decode string (with maximum possible length) and set write position to zero: - cbuf = luaL_buffinitsize(L, &luabuf, writepos); - writepos = 0; + cbuf = luaL_buffinitsize(L, &luabuf, outlen); + outlen = 0; // loop through the characters until encountering end quote: while ((c = str[pos++]) != '"') { - if (c == 0) { - // handle unexpected end of JSON document: - goto json_import_unexpected_eof; - } else if (c < 32 || c == 127) { + // NOTE: unexpected end cannot happen anymore + if (c < 32 || c == 127) { // do not allow ASCII control characters: // NOTE: illegal UTF-8 sequences and extended control characters are not sanitized // by this parser to allow different encodings than Unicode @@ -336,18 +335,18 @@ case '"': case '/': case '\\': - cbuf[writepos++] = c; + cbuf[outlen++] = c; break; // unescaping of backspace: - case 'b': cbuf[writepos++] = '\b'; break; + case 'b': cbuf[outlen++] = '\b'; break; // unescaping of form-feed: - case 'f': cbuf[writepos++] = '\f'; break; + case 'f': cbuf[outlen++] = '\f'; break; // unescaping of new-line: - case 'n': cbuf[writepos++] = '\n'; break; + case 'n': cbuf[outlen++] = '\n'; break; // unescaping of carriage-return: - case 'r': cbuf[writepos++] = '\r'; break; + case 'r': cbuf[outlen++] = '\r'; break; // unescaping of tabulator: - case 't': cbuf[writepos++] = '\t'; break; + case 't': cbuf[outlen++] = '\t'; break; // unescaping of UTF-16 characters case 'u': lua_pushnil(L); @@ -361,11 +360,11 @@ } } else { // normal character: - cbuf[writepos++] = c; + cbuf[outlen++] = c; } } // process buffer to Lua string: - luaL_pushresultsize(&luabuf, writepos); + luaL_pushresultsize(&luabuf, outlen); } else { // if JSON string is empty, // push empty Lua string: