Fossil SCM
Added a parameter to encode_json_string_literal() to allow it to return its output string length, saving a strlen() call in vxprintf() %j.
Commit
57edd1814461ef2dea885b89d211ee3ad90c6f6177ffea281dba0d8f3d56baf4
Parent
28b2261b75d5372…
2 files changed
+8
-1
+1
-2
+8
-1
| --- src/encode.c | ||
| +++ src/encode.c | ||
| @@ -380,12 +380,16 @@ | ||
| 380 | 380 | |
| 381 | 381 | /* |
| 382 | 382 | ** Encode a UTF8 string as a JSON string literal (without the surrounding |
| 383 | 383 | ** "...") and return a pointer to the encoding. Space to hold the encoding |
| 384 | 384 | ** is obtained from fossil_malloc() and must be freed by the caller. |
| 385 | +** | |
| 386 | +** If nOut is not NULL then it is assigned to the length, in bytes, of | |
| 387 | +** the returned string (its strlen(), not counting the terminating | |
| 388 | +** NUL). | |
| 385 | 389 | */ |
| 386 | -char *encode_json_string_literal(const char *zStr){ | |
| 390 | +char *encode_json_string_literal(const char *zStr, int * nOut){ | |
| 387 | 391 | const unsigned char *z; |
| 388 | 392 | char *zOut; |
| 389 | 393 | u32 c; |
| 390 | 394 | int n, i, j; |
| 391 | 395 | z = (const unsigned char*)zStr; |
| @@ -428,10 +432,13 @@ | ||
| 428 | 432 | }else{ |
| 429 | 433 | zOut[i++] = c; |
| 430 | 434 | } |
| 431 | 435 | } |
| 432 | 436 | zOut[i] = 0; |
| 437 | + if(nOut!=0){ | |
| 438 | + *nOut = i; | |
| 439 | + } | |
| 433 | 440 | return zOut; |
| 434 | 441 | } |
| 435 | 442 | |
| 436 | 443 | /* |
| 437 | 444 | ** The characters used for HTTP base64 encoding. |
| 438 | 445 |
| --- src/encode.c | |
| +++ src/encode.c | |
| @@ -380,12 +380,16 @@ | |
| 380 | |
| 381 | /* |
| 382 | ** Encode a UTF8 string as a JSON string literal (without the surrounding |
| 383 | ** "...") and return a pointer to the encoding. Space to hold the encoding |
| 384 | ** is obtained from fossil_malloc() and must be freed by the caller. |
| 385 | */ |
| 386 | char *encode_json_string_literal(const char *zStr){ |
| 387 | const unsigned char *z; |
| 388 | char *zOut; |
| 389 | u32 c; |
| 390 | int n, i, j; |
| 391 | z = (const unsigned char*)zStr; |
| @@ -428,10 +432,13 @@ | |
| 428 | }else{ |
| 429 | zOut[i++] = c; |
| 430 | } |
| 431 | } |
| 432 | zOut[i] = 0; |
| 433 | return zOut; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | ** The characters used for HTTP base64 encoding. |
| 438 |
| --- src/encode.c | |
| +++ src/encode.c | |
| @@ -380,12 +380,16 @@ | |
| 380 | |
| 381 | /* |
| 382 | ** Encode a UTF8 string as a JSON string literal (without the surrounding |
| 383 | ** "...") and return a pointer to the encoding. Space to hold the encoding |
| 384 | ** is obtained from fossil_malloc() and must be freed by the caller. |
| 385 | ** |
| 386 | ** If nOut is not NULL then it is assigned to the length, in bytes, of |
| 387 | ** the returned string (its strlen(), not counting the terminating |
| 388 | ** NUL). |
| 389 | */ |
| 390 | char *encode_json_string_literal(const char *zStr, int * nOut){ |
| 391 | const unsigned char *z; |
| 392 | char *zOut; |
| 393 | u32 c; |
| 394 | int n, i, j; |
| 395 | z = (const unsigned char*)zStr; |
| @@ -428,10 +432,13 @@ | |
| 432 | }else{ |
| 433 | zOut[i++] = c; |
| 434 | } |
| 435 | } |
| 436 | zOut[i] = 0; |
| 437 | if(nOut!=0){ |
| 438 | *nOut = i; |
| 439 | } |
| 440 | return zOut; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | ** The characters used for HTTP base64 encoding. |
| 445 |
+1
-2
| --- src/printf.c | ||
| +++ src/printf.c | ||
| @@ -798,12 +798,11 @@ | ||
| 798 | 798 | /* Ignore the limit flag, if set, for JSON string |
| 799 | 799 | ** output. This block exists to squelch the associated |
| 800 | 800 | ** "unused variable" compiler warning. */ |
| 801 | 801 | } |
| 802 | 802 | if( zMem==0 ) zMem = ""; |
| 803 | - zExtra = bufpt = encode_json_string_literal(zMem); | |
| 804 | - length = strlen(bufpt); | |
| 803 | + zExtra = bufpt = encode_json_string_literal(zMem, &length); | |
| 805 | 804 | if( precision>=0 && precision<length ) length = precision; |
| 806 | 805 | break; |
| 807 | 806 | } |
| 808 | 807 | case etWIKISTR: { |
| 809 | 808 | int limit = flag_alternateform ? va_arg(ap,int) : -1; |
| 810 | 809 |
| --- src/printf.c | |
| +++ src/printf.c | |
| @@ -798,12 +798,11 @@ | |
| 798 | /* Ignore the limit flag, if set, for JSON string |
| 799 | ** output. This block exists to squelch the associated |
| 800 | ** "unused variable" compiler warning. */ |
| 801 | } |
| 802 | if( zMem==0 ) zMem = ""; |
| 803 | zExtra = bufpt = encode_json_string_literal(zMem); |
| 804 | length = strlen(bufpt); |
| 805 | if( precision>=0 && precision<length ) length = precision; |
| 806 | break; |
| 807 | } |
| 808 | case etWIKISTR: { |
| 809 | int limit = flag_alternateform ? va_arg(ap,int) : -1; |
| 810 |
| --- src/printf.c | |
| +++ src/printf.c | |
| @@ -798,12 +798,11 @@ | |
| 798 | /* Ignore the limit flag, if set, for JSON string |
| 799 | ** output. This block exists to squelch the associated |
| 800 | ** "unused variable" compiler warning. */ |
| 801 | } |
| 802 | if( zMem==0 ) zMem = ""; |
| 803 | zExtra = bufpt = encode_json_string_literal(zMem, &length); |
| 804 | if( precision>=0 && precision<length ) length = precision; |
| 805 | break; |
| 806 | } |
| 807 | case etWIKISTR: { |
| 808 | int limit = flag_alternateform ? va_arg(ap,int) : -1; |
| 809 |