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.

stephan 2020-05-14 04:13 fileedit-ajaxify
Commit 57edd1814461ef2dea885b89d211ee3ad90c6f6177ffea281dba0d8f3d56baf4
2 files changed +8 -1 +1 -2
+8 -1
--- src/encode.c
+++ src/encode.c
@@ -380,12 +380,16 @@
380380
381381
/*
382382
** Encode a UTF8 string as a JSON string literal (without the surrounding
383383
** "...") and return a pointer to the encoding. Space to hold the encoding
384384
** 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).
385389
*/
386
-char *encode_json_string_literal(const char *zStr){
390
+char *encode_json_string_literal(const char *zStr, int * nOut){
387391
const unsigned char *z;
388392
char *zOut;
389393
u32 c;
390394
int n, i, j;
391395
z = (const unsigned char*)zStr;
@@ -428,10 +432,13 @@
428432
}else{
429433
zOut[i++] = c;
430434
}
431435
}
432436
zOut[i] = 0;
437
+ if(nOut!=0){
438
+ *nOut = i;
439
+ }
433440
return zOut;
434441
}
435442
436443
/*
437444
** The characters used for HTTP base64 encoding.
438445
--- 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 @@
798798
/* Ignore the limit flag, if set, for JSON string
799799
** output. This block exists to squelch the associated
800800
** "unused variable" compiler warning. */
801801
}
802802
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);
805804
if( precision>=0 && precision<length ) length = precision;
806805
break;
807806
}
808807
case etWIKISTR: {
809808
int limit = flag_alternateform ? va_arg(ap,int) : -1;
810809
--- 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

Keyboard Shortcuts

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