Skip to content

Commit 5fc8403

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 886aec8 commit 5fc8403

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

src/jv_print.c

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

4949
for (num_colors = 0;; 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' || num_colors + 1 >= COLORS_LEN) {
53+
break;
54+
} else if (code_str[0] != ':') {
6355
return 0; // invalid character
6456
}
65-
if (num_colors + 1 >= COLORS_LEN) {
66-
goto set_codes_end; // done
67-
}
57+
code_str++;
6858
}
69-
set_codes_end:
7059
if (codes[num_colors] != code_str) {
7160
// count the last color and store its end (plus one byte for consistency with starts)
7261
// an empty last color would be ignored (for cases like "" and "0:")

0 commit comments

Comments
 (0)