-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dynamically allocate space for JQ_COLORS #3282
base: master
Are you sure you want to change the base?
Conversation
Thanks for submitting a PR. My honest impression is the patch makes the implementation too complicated by reducing the number of allocations and avoiding strspn. I submitted #3288 to support longer color configurations like true color, while keeping the code as simple as before. What do you think? |
i can't tell which functions are part of libjq and which aren't. if jq_set_colors is part of the library, it's leaking memory. if it's not, then a lot of the code in your function is unneccecary and confusing, and i could write a simpler version of the function than what i gave in this pr. i could also just do a better job on comments and variable names. i kept close to the original style because i figure that's just what most people from contributions, even for as lax as this project has it. i would've done it differently, most notably better variable names. i prefer checking the entire string first not just because it saves the allocation but also because it's just clearer that everything gets discarded when any of the colors are invalid. as for the code style, in the end it's your codebase, your choice, and i don't really write c. but if you're asking what i think on that: even reading the new code now, i keep getting if ((e = strchr(c, ':')) != NULL)
l = e++ - c;
else
e = c + (l = strlen(c));
near the start you set on that note, the allocations also get leaked if JQ_COLORS is invalid. i also just noticed that on the only call to jq_set_colors, it already doesn't even pass in the environment variable if it's NULL, so the check isn't doing anything. i get that the tiny leaks don't matter much for performance, but all these little issues throw me off when just trying to make sense of the code. |
i made the extra pr before i fully thought things through, ignore it. |
Thank you for your valuable comment! Yes, you're right about the allocation leak, and since the function is included in |
valid color escapes can be over 100 characters long, so a static buffer isn't a good option. resolves jqlang#2426
better variable names and comments
move assignments out of expressions
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
dropped the commit that assumes the function can only be called once, and cleaned up the history a bit. each point in the history builds and passes tests. |
this allows for larger escapes, so you can fit in full rgb foregrounds, background, underlines, and whatever else into every color. only one allocation is used and it only allocates what's needed.
the function should work if called multiple times, i set it up this way only because it seemed to already be set up with that in mind. if that's not needed i can make another commit to throw out unnecessary bits.
you may want to write a few more tests, i didn't bother.
resolves #2426