Skip to content

Commit 8d93a80

Browse files
committed
fixup! dynamically allocate JQ_COLORS escapes for truecolor support
change the switch case to a strspn gcc will do a worse job optimizing this but it makes the code shorter and easier to read
1 parent 308bf7c commit 8d93a80

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

src/jv_print.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,14 @@ int jq_set_colors(const char *code_str) {
4848

4949
for (num_colors = 0; num_colors < COLORS_LEN; num_colors++) {
5050
codes[num_colors] = code_str;
51-
letter:
52-
switch (code_str[0]) {
53-
// technically posix doesn't specify ascii so a range wouldn't be portable
54-
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case ';':
55-
code_str++;
56-
goto letter; // loops until end of color code
57-
case ':':
58-
code_str++;
59-
continue; // next color
60-
case '\0':
61-
goto set_codes_end; // done
62-
default:
51+
code_str += strspn(code_str, "0123456789;");
52+
if (code_str[0] == '\0') {
53+
break;
54+
} else if (code_str[0] != ':') {
6355
return 0; // invalid character
6456
}
57+
code_str++;
6558
}
66-
set_codes_end:
6759
if (codes[num_colors] != code_str) {
6860
// count the last color and store its end (plus one byte for consistency with starts)
6961
// an empty last color would be ignored (for cases like "" and "0:")

0 commit comments

Comments
 (0)