We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
jv_dump_string_trunc
1 parent fa6a2ff commit a6b77bbCopy full SHA for a6b77bb
2 files changed
src/jv_print.c
@@ -381,16 +381,17 @@ jv jv_dump_string(jv x, int flags) {
381
}
382
383
char *jv_dump_string_trunc(jv x, char *outbuf, size_t bufsize) {
384
- x = jv_dump_string(x,0);
385
- const char* p = jv_string_value(x);
386
- const size_t len = strlen(p);
387
- strncpy(outbuf, p, bufsize);
388
- outbuf[bufsize - 1] = 0;
+ x = jv_dump_string(x, 0);
+ const char *str = jv_string_value(x);
+ const size_t len = strlen(str);
+ strncpy(outbuf, str, bufsize);
389
if (len > bufsize - 1 && bufsize >= 4) {
390
- // Indicate truncation with '...'
391
- outbuf[bufsize - 2]='.';
392
- outbuf[bufsize - 3]='.';
393
- outbuf[bufsize - 4]='.';
+ // Indicate truncation with '...' without breaking UTF-8.
+ const char *s = jvp_utf8_backtrack(outbuf + bufsize - 4, outbuf, NULL);
+ if (s) bufsize = s + 4 - outbuf;
+ strcpy(outbuf + bufsize - 4, "...");
+ } else {
394
+ outbuf[bufsize - 1] = '\0';
395
396
jv_free(x);
397
return outbuf;
tests/jq.test
@@ -1788,6 +1788,19 @@ try -. catch .
1788
"very-long-string"
1789
"string (\"very-long-...) cannot be negated"
1790
1791
+try (.-.) catch .
1792
+"very-long-string"
1793
+"string (\"very-long-...) and string (\"very-long-...) cannot be subtracted"
1794
+
1795
+"x" * range(0; 12; 2) + "☆" * 5 | try -. catch .
1796
+null
1797
+"string (\"☆☆☆...) cannot be negated"
1798
+"string (\"xx☆☆...) cannot be negated"
1799
+"string (\"xxxx☆☆...) cannot be negated"
1800
+"string (\"xxxxxx☆...) cannot be negated"
1801
+"string (\"xxxxxxxx...) cannot be negated"
1802
+"string (\"xxxxxxxxxx...) cannot be negated"
1803
1804
join(",")
1805
["1",2,true,false,3.4]
1806
"1,2,true,false,3.4"
0 commit comments