osc: avoid unnecessary calls to strlen(3)

Checking for specific strings of length 0 or 1 can be done with e.g.:

    if (str[0] == '\0') {...}
    if (str[0] == '?' && str[1] == '\0') {...}

Doing it this way instead of using strlen(3) means we avoid the
function call overhead and also avoid scanning through more of the
string than is neceessary.

A compiler could perhaps optimize away calls to strlen(3) when the
result is compared to a small constant, but GCC 11.2 only seems to
actually do this[1] for lengths of 0.

[1]: https://godbolt.org/z/qxoW8qqW6
This commit is contained in:
Craig Barnes 2022-04-06 01:24:34 +01:00 committed by Daniel Eklöf
parent 5ce1589c60
commit 0d1e6960af
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

10
osc.c
View file

@ -252,7 +252,7 @@ osc_selection(struct terminal *term, char *string)
LOG_DBG("clipboard: target = %s data = %s", string, p);
if (strlen(p) == 1 && p[0] == '?')
if (p[0] == '?' && p[1] == '\0')
osc_from_clipboard(term, string);
else
osc_to_clipboard(term, string, p);
@ -587,7 +587,7 @@ osc_dispatch(struct terminal *term)
}
/* Client queried for current value */
if (strlen(s_color) == 1 && s_color[0] == '?') {
if (s_color[0] == '?' && s_color[1] == '\0') {
uint32_t color = term->colors.table[idx];
uint8_t r = (color >> 16) & 0xff;
uint8_t g = (color >> 8) & 0xff;
@ -683,7 +683,7 @@ osc_dispatch(struct terminal *term)
/* Set default foreground/background/highlight-bg/highlight-fg color */
/* Client queried for current value */
if (strlen(string) == 1 && string[0] == '?') {
if (string[0] == '?' && string[1] == '\0') {
uint32_t color = param == 10 ? term->colors.fg : term->colors.bg;
uint8_t r = (color >> 16) & 0xff;
uint8_t g = (color >> 8) & 0xff;
@ -752,7 +752,7 @@ osc_dispatch(struct terminal *term)
case 12: /* Set cursor color */
/* Client queried for current value */
if (strlen(string) == 1 && string[0] == '?') {
if (string[0] == '?' && string[1] == '\0') {
uint8_t r = (term->cursor_color.cursor >> 16) & 0xff;
uint8_t g = (term->cursor_color.cursor >> 8) & 0xff;
uint8_t b = (term->cursor_color.cursor >> 0) & 0xff;
@ -800,7 +800,7 @@ osc_dispatch(struct terminal *term)
case 104: {
/* Reset Color Number 'c' (whole table if no parameter) */
if (strlen(string) == 0) {
if (string[0] == '\0') {
LOG_DBG("resetting all colors");
for (size_t i = 0; i < ALEN(term->colors.table); i++)
term->colors.table[i] = term->conf->colors.table[i];