1
0
mirror of https://github.com/stefanocasazza/ULib.git synced 2025-09-28 19:05:55 +08:00

jfind fix

This commit is contained in:
stefanocasazza 2017-04-22 18:30:30 +02:00
parent 43bab3c84a
commit 23bbd1bc84
3 changed files with 38 additions and 4 deletions

View File

@ -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); 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_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 */ 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 */ /* find char not quoted */
U_EXPORT const char* u_find_char(const char* restrict s, const char* restrict end, char c) __pure; U_EXPORT const char* u_find_char(const char* restrict s, const char* restrict end, char c) __pure;

View File

@ -2133,7 +2133,19 @@ bool UValue::jfind(const UString& json, const char* query, uint32_t query_len, U
U_ASSERT(result.empty()) 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) 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; pos += query_len;
if (u__isquote(json.c_char(pos))) ++pos;
UTokenizer tok(json.substr(pos)); UTokenizer tok(json.substr(pos));
int sTok = jreadFindToken(tok); int sTok = jreadFindToken(tok);

View File

@ -774,6 +774,15 @@ U_EXPORT main (int argc, char* argv[], char* env[])
U_INTERNAL_ASSERT_EQUALS(workingString, "[ -73.9888983, 40.7212405 ]") 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( U_STRING_FROM_CONSTANT("{ \"_id\" : 3457, \"id\" : 3457, \"randomNumber\" : 8427 }"), "{'randomNumber'", U_STRING_FROM_CONSTANT("8427") );
testQuery( exampleJson, "", exampleJson ); testQuery( exampleJson, "", exampleJson );
testQuery( exampleJson, "[1", U_STRING_FROM_CONSTANT("") ); testQuery( exampleJson, "[1", U_STRING_FROM_CONSTANT("") );