]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
In JSON - minor code cleanup and refactoring with a small size reduction
authordrh <>
Tue, 19 Dec 2023 21:39:58 +0000 (21:39 +0000)
committerdrh <>
Tue, 19 Dec 2023 21:39:58 +0000 (21:39 +0000)
and performance increase.

FossilOrigin-Name: 215fabda38daecdbd38b1eca5a6aafbc61b6a36a8303f1d7164d5a1138e63134

manifest
manifest.uuid
src/json.c

index 5fcfb15ae351806253fe1cb395d28363ed9ac9ed..c659725846a97c610c7811e0845c6d9d4dd40fe7 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Remove\sredundant\sconditional\sfrom\ssqlite3ExprCanBeNull().
-D 2023-12-19T15:51:14.357
+C In\sJSON\s-\sminor\scode\scleanup\sand\srefactoring\swith\sa\ssmall\ssize\sreduction\nand\sperformance\sincrease.
+D 2023-12-19T21:39:58.157
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -696,7 +696,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51
 F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6
 F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
 F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276
-F src/json.c 661b6da631185152c635161d52c274255c8de3f5769570bbbde0311cd9ad7460
+F src/json.c c6fae579d681d5ab43387c8667cc6ef0c0f8769fd881fde3af6260e837cae762
 F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
 F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36
 F src/main.c ce714ee501122c76eb2e69b292bebe443aba611fc3b88e6786eb910285515fe4
@@ -2155,8 +2155,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 36fe6a61ef8fb393281a5e15119d716521219c7b971fbfd63bdea07d27a78ac9
-R 8d141472bcc17284a46ea446e12c41f3
+P 257f96a2d22c605885fa66220c28cf7dc5941c330bccee3f132b9e7b70d89d30
+R 1e43c523c137344cae8cdc90cc8b1706
 U drh
-Z 7720e1377fde94e934586312bc65b10d
+Z 35aaca72ca93a246000bef5de54a54bb
 # Remove this line to create a well-formed Fossil manifest.
index 5c345ddc6fe7dacf12242d0e7bc0f833e699ba8f..fdf94f914d0e306e817b092e08a48385caa2dfa5 100644 (file)
@@ -1 +1 @@
-257f96a2d22c605885fa66220c28cf7dc5941c330bccee3f132b9e7b70d89d30
\ No newline at end of file
+215fabda38daecdbd38b1eca5a6aafbc61b6a36a8303f1d7164d5a1138e63134
\ No newline at end of file
index d0e7b3fc6b8a8179f1b294cf76841f100c097825..8fae3d1140ce8c94d7722bf0f5ef912716d5eb6d 100644 (file)
@@ -20,9 +20,9 @@
 ** All generated JSON text still conforms strictly to RFC-8259, but text
 ** with JSON-5 extensions is accepted as input.
 **
-** Beginning with version 3.45.0 (pending), these routines also accept
-** BLOB values that have JSON encoded using a binary representation we
-** call JSONB.  The name JSONB comes from PostgreSQL, however the on-disk
+** Beginning with version 3.45.0 (circa 2024-01-01), these routines also
+** accept BLOB values that have JSON encoded using a binary representation
+** called "JSONB".  The name JSONB comes from PostgreSQL, however the on-disk
 ** format SQLite JSONB is completely different and incompatible with
 ** PostgreSQL JSONB.
 **
@@ -220,6 +220,8 @@ typedef struct JsonParse JsonParse;
 **    *   The aBlob[] array must be owned by the JsonParse object.  In other
 **        words, nBlobAlloc must be non-zero.
 **
+**    *   eEdit and delta must be zero.
+**
 **    *   zJson must be an RCStr.  In other words bJsonIsRCStr must be true.
 */
 struct JsonCache {
@@ -279,8 +281,8 @@ struct JsonString {
 **       json_replace() or json_patch() or similar).
 **
 **   4.  New JSON text is generated from the aBlob[] for output.  This step
-**       is skipped the function is one of the jsonb_* functions that returns
-**       JSONB instead of text JSON.
+**       is skipped if the function is one of the jsonb_* functions that
+**       returns JSONB instead of text JSON.
 */
 struct JsonParse {
   u8 *aBlob;         /* JSONB representation of JSON value */
@@ -288,14 +290,14 @@ struct JsonParse {
   u32 nBlobAlloc;    /* Bytes allocated to aBlob[].  0 if aBlob is external */
   char *zJson;       /* Json text used for parsing */
   int nJson;         /* Length of the zJson string in bytes */
+  u32 nJPRef;        /* Number of references to this object */
+  u32 iErr;          /* Error location in zJson[] */
   u16 iDepth;        /* Nesting depth */
   u8 nErr;           /* Number of errors seen */
   u8 oom;            /* Set to true if out of memory */
   u8 bJsonIsRCStr;   /* True if zJson is an RCStr */
   u8 hasNonstd;      /* True if input uses non-standard features like JSON5 */
   u8 bReadOnly;      /* Do not modify. */
-  u32 nJPRef;        /* Number of references to this object */
-  u32 iErr;          /* Error location in zJson[] */
   /* Search and edit information.  See jsonLookupStep() */
   u8 eEdit;          /* Edit operation to apply */
   int delta;         /* Size change due to the edit */
@@ -334,7 +336,7 @@ struct JsonParse {
 **************************************************************************/
 static void jsonReturnStringAsBlob(JsonString*);
 static int jsonFuncArgMightBeBinary(sqlite3_value *pJson);
-static u32 jsonXlateBlobToText(const JsonParse*,u32,JsonString*);
+static u32 jsonTranslateBlobToText(const JsonParse*,u32,JsonString*);
 static void jsonReturnParse(sqlite3_context*,JsonParse*);
 static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32);
 static void jsonParseFree(JsonParse*);
@@ -709,7 +711,7 @@ static void jsonAppendSqlValue(
         memset(&px, 0, sizeof(px));
         px.aBlob = (u8*)sqlite3_value_blob(pValue);
         px.nBlob = sqlite3_value_bytes(pValue);
-        jsonXlateBlobToText(&px, 0, p);
+        jsonTranslateBlobToText(&px, 0, p);
       }else if( p->eErr==0 ){
         sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
         p->eErr = JSTRING_ERR;
@@ -1226,15 +1228,11 @@ static int jsonBlobChangePayloadSize(
 */
 static int jsonIs4HexB(const char *z, int *pOp){
   if( z[0]!='u' ) return 0;
-  if( !sqlite3Isxdigit(z[1]) ) return 0;
-  if( !sqlite3Isxdigit(z[2]) ) return 0;
-  if( !sqlite3Isxdigit(z[3]) ) return 0;
-  if( !sqlite3Isxdigit(z[4]) ) return 0;
+  if( !jsonIs4Hex(&z[1]) ) return 0;
   *pOp = JSONB_TEXTJ;
   return 1;
 }
 
-
 /*
 ** Check a single element of the JSONB in pParse for validity.
 **
@@ -1451,7 +1449,7 @@ static u32 jsonbValidityCheck(
 **     -4    ',' seen    /     the index in zJson[] of the seen character
 **     -5    ':' seen   /
 */
-static int jsonXlateTextToBlob(JsonParse *pParse, u32 i){
+static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){
   char c;
   u32 j;
   u32 iThis, iStart;
@@ -1471,7 +1469,7 @@ json_parse_restart:
     iStart = pParse->nBlob;
     for(j=i+1;;j++){
       u32 iBlob = pParse->nBlob;
-      x = jsonXlateTextToBlob(pParse, j);
+      x = jsonTranslateTextToBlob(pParse, j);
       if( x<=0 ){
         int op;
         if( x==(-2) ){
@@ -1517,7 +1515,7 @@ json_parse_restart:
             goto parse_object_value;
           }
         }
-        x = jsonXlateTextToBlob(pParse, j);
+        x = jsonTranslateTextToBlob(pParse, j);
         if( x!=(-5) ){
           if( x!=(-1) ) pParse->iErr = j;
           return -1;
@@ -1525,7 +1523,7 @@ json_parse_restart:
         j = pParse->iErr+1;
       }
     parse_object_value:
-      x = jsonXlateTextToBlob(pParse, j);
+      x = jsonTranslateTextToBlob(pParse, j);
       if( x<=0 ){
         if( x!=(-1) ) pParse->iErr = j;
         return -1;
@@ -1544,7 +1542,7 @@ json_parse_restart:
             break;
           }
         }
-        x = jsonXlateTextToBlob(pParse, j);
+        x = jsonTranslateTextToBlob(pParse, j);
         if( x==(-4) ){
           j = pParse->iErr;
           continue;
@@ -1572,7 +1570,7 @@ json_parse_restart:
       return -1;
     }
     for(j=i+1;;j++){
-      x = jsonXlateTextToBlob(pParse, j);
+      x = jsonTranslateTextToBlob(pParse, j);
       if( x<=0 ){
         if( x==(-3) ){
           j = pParse->iErr;
@@ -1596,7 +1594,7 @@ json_parse_restart:
             break;
           }
         }
-        x = jsonXlateTextToBlob(pParse, j);
+        x = jsonTranslateTextToBlob(pParse, j);
         if( x==(-4) ){
           j = pParse->iErr;
           continue;
@@ -1919,7 +1917,7 @@ static int jsonConvertTextToBlob(
 ){
   int i;
   const char *zJson = pParse->zJson;
-  i = jsonXlateTextToBlob(pParse, 0);
+  i = jsonTranslateTextToBlob(pParse, 0);
   if( pParse->oom ) i = -1;
   if( i>0 ){
 #ifdef SQLITE_DEBUG
@@ -1964,7 +1962,7 @@ static void jsonReturnStringAsBlob(JsonString *pStr){
   jsonStringTerminate(pStr);
   px.zJson = pStr->zBuf;
   px.nJson = pStr->nUsed;
-  (void)jsonXlateTextToBlob(&px, 0);
+  (void)jsonTranslateTextToBlob(&px, 0);
   if( px.oom ){
     sqlite3_free(px.aBlob);
     sqlite3_result_error_nomem(pStr->pCtx);
@@ -2052,7 +2050,7 @@ static u32 jsonbPayloadSize(const JsonParse *pParse, u32 i, u32 *pSz){
 **
 ** The pOut->eErr JSTRING_OOM flag is set on a OOM.
 */
-static u32 jsonXlateBlobToText(
+static u32 jsonTranslateBlobToText(
   const JsonParse *pParse,       /* the complete parse of the JSON */
   u32 i,                         /* Start rendering at this index */
   JsonString *pOut               /* Write JSON here */
@@ -2223,7 +2221,7 @@ static u32 jsonXlateBlobToText(
       j = i+n;
       iEnd = j+sz;
       while( j<iEnd ){
-        j = jsonXlateBlobToText(pParse, j, pOut);
+        j = jsonTranslateBlobToText(pParse, j, pOut);
         jsonAppendChar(pOut, ',');
       }
       if( sz>0 ) pOut->nUsed--;
@@ -2236,7 +2234,7 @@ static u32 jsonXlateBlobToText(
       j = i+n;
       iEnd = j+sz;
       while( j<iEnd ){
-        j = jsonXlateBlobToText(pParse, j, pOut);
+        j = jsonTranslateBlobToText(pParse, j, pOut);
         jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
       }
       if( x & 1 ) pOut->eErr |= JSTRING_MALFORMED;
@@ -2818,7 +2816,7 @@ static void jsonReturnTextJsonFromBlob(
   x.aBlob = (u8*)aBlob;
   x.nBlob = nBlob;
   jsonStringInit(&s, ctx);
-  jsonXlateBlobToText(&x, 0, &s);
+  jsonTranslateBlobToText(&x, 0, &s);
   jsonReturnString(&s, 0, 0);
 }
 
@@ -3320,7 +3318,7 @@ static void jsonReturnParse(
     JsonString s;
     jsonStringInit(&s, ctx);
     p->delta = 0;
-    jsonXlateBlobToText(p, 0, &s);
+    jsonTranslateBlobToText(p, 0, &s);
     jsonReturnString(&s, p, ctx);
     sqlite3_result_subtype(ctx, JSON_SUBTYPE);
   }
@@ -3648,7 +3646,7 @@ static void jsonExtractFunc(
       if( argc==2 ){
         if( flags & JSON_JSON ){
           jsonStringInit(&jx, ctx);
-          jsonXlateBlobToText(p, j, &jx);
+          jsonTranslateBlobToText(p, j, &jx);
           jsonReturnString(&jx, 0, 0);
           jsonStringReset(&jx);
           assert( (flags & JSON_BLOB)==0 );
@@ -3663,7 +3661,7 @@ static void jsonExtractFunc(
         }
       }else{
         jsonAppendSeparator(&jx);
-        jsonXlateBlobToText(p, j, &jx);
+        jsonTranslateBlobToText(p, j, &jx);
       }
     }else if( j==JSON_LOOKUP_NOTFOUND ){
       if( argc==2 ){