From 23bbd1bc84cb93c7b0a9b54764114b421e7bff3c Mon Sep 17 00:00:00 2001 From: stefanocasazza Date: Sat, 22 Apr 2017 18:30:30 +0200 Subject: [PATCH] jfind fix --- include/ulib/base/utility.h | 17 ++++++++++++++++- src/ulib/json/value.cpp | 16 +++++++++++++--- tests/ulib/test_json.cpp | 9 +++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/include/ulib/base/utility.h b/include/ulib/base/utility.h index 1f36b29e..b34e30ee 100644 --- a/include/ulib/base/utility.h +++ b/include/ulib/base/utility.h @@ -211,11 +211,26 @@ static inline uint32_t u_findEndHeader1(const char* restrict s, uint32_t n) /* f return (p ? (const char*)p - s + U_CONSTANT_SIZE(U_CRLF2) : U_NOT_FOUND); } -U_EXPORT uint32_t u_findEndHeader( const char* restrict s, uint32_t n) __pure; /* find sequence of U_CRLF2 or U_LF2 */ +U_EXPORT uint32_t u_findEndHeader(const char* restrict s, uint32_t n) __pure; /* find sequence of U_CRLF2 or U_LF2 */ U_EXPORT bool u_endsWith(const char* restrict a, uint32_t n1, const char* restrict b, uint32_t n2) __pure; /* check if string a terminate with string b */ U_EXPORT bool u_startsWith(const char* restrict a, uint32_t n1, const char* restrict b, uint32_t n2) __pure; /* check if string a start with string b */ +/* check if the string is quoted... */ + +static inline bool u_is_quoted(const char* restrict s, uint32_t n) +{ + U_INTERNAL_TRACE("u_is_quoted(%.*s,%u)", U_min(n,128), s, n) + + if (s[0] == '"' && + s[n-1] == '"') + { + return true; + } + + return false; +} + /* find char not quoted */ U_EXPORT const char* u_find_char(const char* restrict s, const char* restrict end, char c) __pure; diff --git a/src/ulib/json/value.cpp b/src/ulib/json/value.cpp index c22a891d..515f7f21 100644 --- a/src/ulib/json/value.cpp +++ b/src/ulib/json/value.cpp @@ -2133,7 +2133,19 @@ bool UValue::jfind(const UString& json, const char* query, uint32_t query_len, U U_ASSERT(result.empty()) - uint32_t pos = json.find(query, 0, query_len); + uint32_t pos; + + if (u_is_quoted(query, query_len)) pos = json.find(query, 0, query_len); + else + { + uint32_t len = query_len; + + UString quoted((query_len += 2)); + + quoted.snprintf(U_CONSTANT_TO_PARAM("\"%.*s\""), len, query); + + pos = json.find(quoted); + } U_INTERNAL_DUMP("pos = %d", pos) @@ -2141,8 +2153,6 @@ bool UValue::jfind(const UString& json, const char* query, uint32_t query_len, U { pos += query_len; - if (u__isquote(json.c_char(pos))) ++pos; - UTokenizer tok(json.substr(pos)); int sTok = jreadFindToken(tok); diff --git a/tests/ulib/test_json.cpp b/tests/ulib/test_json.cpp index 73a9ffdf..72973308 100644 --- a/tests/ulib/test_json.cpp +++ b/tests/ulib/test_json.cpp @@ -774,6 +774,15 @@ U_EXPORT main (int argc, char* argv[], char* env[]) U_INTERNAL_ASSERT_EQUALS(workingString, "[ -73.9888983, 40.7212405 ]") + workingString.clear(); + result1.clear(); + + (void) U_JFIND(U_STRING_FROM_CONSTANT("{\"saltedHash\":\"f66113b5ed33f961219c\",\"osVersion\":\"10.3.1\",\"socials\":[{\"name\":\"victor]},\"t\":\"createAccount\"}"), "t", result1); + + U_INTERNAL_ASSERT_EQUALS(result1, "createAccount") + + result1.clear(); + testQuery( U_STRING_FROM_CONSTANT("{ \"_id\" : 3457, \"id\" : 3457, \"randomNumber\" : 8427 }"), "{'randomNumber'", U_STRING_FROM_CONSTANT("8427") ); testQuery( exampleJson, "", exampleJson ); testQuery( exampleJson, "[1", U_STRING_FROM_CONSTANT("") );