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().

stephan 2020-05-15 03:34 fileedit-ajaxify
Commit 2cccc12d0482cc137c30c4fea92fb69fc1eb62914cc8a0f2cc6d1f2921713335
2 files changed +16 -4 +4 -2
+16 -4
--- src/encode.c
+++ src/encode.c
@@ -377,19 +377,22 @@
377377
}
378378
return c;
379379
}
380380
381381
/*
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.
385387
**
386388
** If nOut is not NULL then it is assigned to the length, in bytes, of
387389
** the returned string (its strlen(), not counting the terminating
388390
** NUL).
389391
*/
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){
391394
const unsigned char *z;
392395
char *zOut;
393396
u32 c;
394397
int n, i, j;
395398
z = (const unsigned char*)zStr;
@@ -404,15 +407,21 @@
404407
n += 6;
405408
}
406409
}else{
407410
n++;
408411
}
412
+ }
413
+ if(fAddQuotes){
414
+ n += 2;
409415
}
410416
zOut = fossil_malloc(n+1);
411417
if( zOut==0 ) return 0;
412418
z = (const unsigned char*)zStr;
413419
i = 0;
420
+ if(fAddQuotes){
421
+ zOut[i++] = '"';
422
+ }
414423
while( (c = fossil_utf8_read(&z))!=0 ){
415424
if( c=='\\' ){
416425
zOut[i++] = '\\';
417426
zOut[i++] = c;
418427
}else if( c<' ' || c>=0x7f ){
@@ -431,10 +440,13 @@
431440
}
432441
}else{
433442
zOut[i++] = c;
434443
}
435444
}
445
+ if(fAddQuotes){
446
+ zOut[i++] = '"';
447
+ }
436448
zOut[i] = 0;
437449
if(nOut!=0){
438450
*nOut = i;
439451
}
440452
return zOut;
441453
--- 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 @@
9999
#define etFOSSILIZE 20 /* The fossil header encoding format. */
100100
#define etPATH 21 /* Path type */
101101
#define etWIKISTR 22 /* Timeline comment text rendered from a char*: %W */
102102
#define etSTRINGID 23 /* String with length limit for a UUID prefix: %S */
103103
#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. */
105106
106107
107108
/*
108109
** An "etByte" is an 8-bit unsigned value.
109110
*/
@@ -798,11 +799,12 @@
798799
/* Ignore the limit flag, if set, for JSON string
799800
** output. This block exists to squelch the associated
800801
** "unused variable" compiler warning. */
801802
}
802803
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);
804806
if( precision>=0 && precision<length ) length = precision;
805807
break;
806808
}
807809
case etWIKISTR: {
808810
int limit = flag_alternateform ? va_arg(ap,int) : -1;
809811
--- 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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button