From 0d1e6960af35ab4aa289e4e0c2443320e2a3adb0 Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Wed, 6 Apr 2022 01:24:34 +0100 Subject: [PATCH] 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 --- osc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/osc.c b/osc.c index 5d04910b..2abd3e86 100644 --- a/osc.c +++ b/osc.c @@ -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];