Fossil SCM
Added %jobs to %j, which means to include double quotes around the resulting JSON string, and added the corresponding flag to encode_json_string_literal().
Commit
2cccc12d0482cc137c30c4fea92fb69fc1eb62914cc8a0f2cc6d1f2921713335
Parent
ef2166a0ac4cb33…
2 files changed
+16
-4
+4
-2
+16
-4
| --- src/encode.c | ||
| +++ src/encode.c | ||
| @@ -377,19 +377,22 @@ | ||
| 377 | 377 | } |
| 378 | 378 | return c; |
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 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. | |
| 382 | +** Encode a UTF8 string as a JSON string literal (with or without the | |
| 383 | +** surrounding "...", depending on whether the 2nd argument is true or | |
| 384 | +** false) and return a pointer to the encoding. Space to hold the | |
| 385 | +** encoding is obtained from fossil_malloc() and must be freed by the | |
| 386 | +** caller. | |
| 385 | 387 | ** |
| 386 | 388 | ** If nOut is not NULL then it is assigned to the length, in bytes, of |
| 387 | 389 | ** the returned string (its strlen(), not counting the terminating |
| 388 | 390 | ** NUL). |
| 389 | 391 | */ |
| 390 | -char *encode_json_string_literal(const char *zStr, int * nOut){ | |
| 392 | +char *encode_json_string_literal(const char *zStr, int fAddQuotes, | |
| 393 | + int * nOut){ | |
| 391 | 394 | const unsigned char *z; |
| 392 | 395 | char *zOut; |
| 393 | 396 | u32 c; |
| 394 | 397 | int n, i, j; |
| 395 | 398 | z = (const unsigned char*)zStr; |
| @@ -404,15 +407,21 @@ | ||
| 404 | 407 | n += 6; |
| 405 | 408 | } |
| 406 | 409 | }else{ |
| 407 | 410 | n++; |
| 408 | 411 | } |
| 412 | + } | |
| 413 | + if(fAddQuotes){ | |
| 414 | + n += 2; | |
| 409 | 415 | } |
| 410 | 416 | zOut = fossil_malloc(n+1); |
| 411 | 417 | if( zOut==0 ) return 0; |
| 412 | 418 | z = (const unsigned char*)zStr; |
| 413 | 419 | i = 0; |
| 420 | + if(fAddQuotes){ | |
| 421 | + zOut[i++] = '"'; | |
| 422 | + } | |
| 414 | 423 | while( (c = fossil_utf8_read(&z))!=0 ){ |
| 415 | 424 | if( c=='\\' ){ |
| 416 | 425 | zOut[i++] = '\\'; |
| 417 | 426 | zOut[i++] = c; |
| 418 | 427 | }else if( c<' ' || c>=0x7f ){ |
| @@ -431,10 +440,13 @@ | ||
| 431 | 440 | } |
| 432 | 441 | }else{ |
| 433 | 442 | zOut[i++] = c; |
| 434 | 443 | } |
| 435 | 444 | } |
| 445 | + if(fAddQuotes){ | |
| 446 | + zOut[i++] = '"'; | |
| 447 | + } | |
| 436 | 448 | zOut[i] = 0; |
| 437 | 449 | if(nOut!=0){ |
| 438 | 450 | *nOut = i; |
| 439 | 451 | } |
| 440 | 452 | return zOut; |
| 441 | 453 |
| --- src/encode.c | |
| +++ src/encode.c | |
| @@ -377,19 +377,22 @@ | |
| 377 | } |
| 378 | return c; |
| 379 | } |
| 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; |
| @@ -404,15 +407,21 @@ | |
| 404 | n += 6; |
| 405 | } |
| 406 | }else{ |
| 407 | n++; |
| 408 | } |
| 409 | } |
| 410 | zOut = fossil_malloc(n+1); |
| 411 | if( zOut==0 ) return 0; |
| 412 | z = (const unsigned char*)zStr; |
| 413 | i = 0; |
| 414 | while( (c = fossil_utf8_read(&z))!=0 ){ |
| 415 | if( c=='\\' ){ |
| 416 | zOut[i++] = '\\'; |
| 417 | zOut[i++] = c; |
| 418 | }else if( c<' ' || c>=0x7f ){ |
| @@ -431,10 +440,13 @@ | |
| 431 | } |
| 432 | }else{ |
| 433 | zOut[i++] = c; |
| 434 | } |
| 435 | } |
| 436 | zOut[i] = 0; |
| 437 | if(nOut!=0){ |
| 438 | *nOut = i; |
| 439 | } |
| 440 | return zOut; |
| 441 |
| --- src/encode.c | |
| +++ src/encode.c | |
| @@ -377,19 +377,22 @@ | |
| 377 | } |
| 378 | return c; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | ** Encode a UTF8 string as a JSON string literal (with or without the |
| 383 | ** surrounding "...", depending on whether the 2nd argument is true or |
| 384 | ** false) and return a pointer to the encoding. Space to hold the |
| 385 | ** encoding is obtained from fossil_malloc() and must be freed by the |
| 386 | ** caller. |
| 387 | ** |
| 388 | ** If nOut is not NULL then it is assigned to the length, in bytes, of |
| 389 | ** the returned string (its strlen(), not counting the terminating |
| 390 | ** NUL). |
| 391 | */ |
| 392 | char *encode_json_string_literal(const char *zStr, int fAddQuotes, |
| 393 | int * nOut){ |
| 394 | const unsigned char *z; |
| 395 | char *zOut; |
| 396 | u32 c; |
| 397 | int n, i, j; |
| 398 | z = (const unsigned char*)zStr; |
| @@ -404,15 +407,21 @@ | |
| 407 | n += 6; |
| 408 | } |
| 409 | }else{ |
| 410 | n++; |
| 411 | } |
| 412 | } |
| 413 | if(fAddQuotes){ |
| 414 | n += 2; |
| 415 | } |
| 416 | zOut = fossil_malloc(n+1); |
| 417 | if( zOut==0 ) return 0; |
| 418 | z = (const unsigned char*)zStr; |
| 419 | i = 0; |
| 420 | if(fAddQuotes){ |
| 421 | zOut[i++] = '"'; |
| 422 | } |
| 423 | while( (c = fossil_utf8_read(&z))!=0 ){ |
| 424 | if( c=='\\' ){ |
| 425 | zOut[i++] = '\\'; |
| 426 | zOut[i++] = c; |
| 427 | }else if( c<' ' || c>=0x7f ){ |
| @@ -431,10 +440,13 @@ | |
| 440 | } |
| 441 | }else{ |
| 442 | zOut[i++] = c; |
| 443 | } |
| 444 | } |
| 445 | if(fAddQuotes){ |
| 446 | zOut[i++] = '"'; |
| 447 | } |
| 448 | zOut[i] = 0; |
| 449 | if(nOut!=0){ |
| 450 | *nOut = i; |
| 451 | } |
| 452 | return zOut; |
| 453 |
+4
-2
| --- src/printf.c | ||
| +++ src/printf.c | ||
| @@ -99,11 +99,12 @@ | ||
| 99 | 99 | #define etFOSSILIZE 20 /* The fossil header encoding format. */ |
| 100 | 100 | #define etPATH 21 /* Path type */ |
| 101 | 101 | #define etWIKISTR 22 /* Timeline comment text rendered from a char*: %W */ |
| 102 | 102 | #define etSTRINGID 23 /* String with length limit for a UUID prefix: %S */ |
| 103 | 103 | #define etROOT 24 /* String value of g.zTop: %R */ |
| 104 | -#define etJSONSTR 25 /* String encoded as a JSON string literal: %j */ | |
| 104 | +#define etJSONSTR 25 /* String encoded as a JSON string literal: %j | |
| 105 | + Use %!j to include double-quotes around it. */ | |
| 105 | 106 | |
| 106 | 107 | |
| 107 | 108 | /* |
| 108 | 109 | ** An "etByte" is an 8-bit unsigned value. |
| 109 | 110 | */ |
| @@ -798,11 +799,12 @@ | ||
| 798 | 799 | /* Ignore the limit flag, if set, for JSON string |
| 799 | 800 | ** output. This block exists to squelch the associated |
| 800 | 801 | ** "unused variable" compiler warning. */ |
| 801 | 802 | } |
| 802 | 803 | if( zMem==0 ) zMem = ""; |
| 803 | - zExtra = bufpt = encode_json_string_literal(zMem, &length); | |
| 804 | + zExtra = bufpt = | |
| 805 | + encode_json_string_literal(zMem, flag_altform2, &length); | |
| 804 | 806 | if( precision>=0 && precision<length ) length = precision; |
| 805 | 807 | break; |
| 806 | 808 | } |
| 807 | 809 | case etWIKISTR: { |
| 808 | 810 | int limit = flag_alternateform ? va_arg(ap,int) : -1; |
| 809 | 811 |
| --- src/printf.c | |
| +++ src/printf.c | |
| @@ -99,11 +99,12 @@ | |
| 99 | #define etFOSSILIZE 20 /* The fossil header encoding format. */ |
| 100 | #define etPATH 21 /* Path type */ |
| 101 | #define etWIKISTR 22 /* Timeline comment text rendered from a char*: %W */ |
| 102 | #define etSTRINGID 23 /* String with length limit for a UUID prefix: %S */ |
| 103 | #define etROOT 24 /* String value of g.zTop: %R */ |
| 104 | #define etJSONSTR 25 /* String encoded as a JSON string literal: %j */ |
| 105 | |
| 106 | |
| 107 | /* |
| 108 | ** An "etByte" is an 8-bit unsigned value. |
| 109 | */ |
| @@ -798,11 +799,12 @@ | |
| 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 |
| --- src/printf.c | |
| +++ src/printf.c | |
| @@ -99,11 +99,12 @@ | |
| 99 | #define etFOSSILIZE 20 /* The fossil header encoding format. */ |
| 100 | #define etPATH 21 /* Path type */ |
| 101 | #define etWIKISTR 22 /* Timeline comment text rendered from a char*: %W */ |
| 102 | #define etSTRINGID 23 /* String with length limit for a UUID prefix: %S */ |
| 103 | #define etROOT 24 /* String value of g.zTop: %R */ |
| 104 | #define etJSONSTR 25 /* String encoded as a JSON string literal: %j |
| 105 | Use %!j to include double-quotes around it. */ |
| 106 | |
| 107 | |
| 108 | /* |
| 109 | ** An "etByte" is an 8-bit unsigned value. |
| 110 | */ |
| @@ -798,11 +799,12 @@ | |
| 799 | /* Ignore the limit flag, if set, for JSON string |
| 800 | ** output. This block exists to squelch the associated |
| 801 | ** "unused variable" compiler warning. */ |
| 802 | } |
| 803 | if( zMem==0 ) zMem = ""; |
| 804 | zExtra = bufpt = |
| 805 | encode_json_string_literal(zMem, flag_altform2, &length); |
| 806 | if( precision>=0 && precision<length ) length = precision; |
| 807 | break; |
| 808 | } |
| 809 | case etWIKISTR: { |
| 810 | int limit = flag_alternateform ? va_arg(ap,int) : -1; |
| 811 |