aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Barnes <craigbarnes@protonmail.com>2022-04-06 00:24:34 +0000
committerDaniel Eklöf <daniel@ekloef.se>2022-04-06 16:19:47 +0000
commit0d1e6960af35ab4aa289e4e0c2443320e2a3adb0 (patch)
treed7c5396acf3a2970f39c5d8a050e6dd57cedb632
parentrender: ensure an underline cursor is not positioned too low (diff)
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
-rw-r--r--osc.c10
1 files 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];