From d3941f235a73f0eb881919bafe208f63ad6a33d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Mon, 8 Apr 2024 01:28:14 +0200 Subject: [PATCH] bstr: change bstr0_s to bstr0_lit, restrict it only for string literals Allows to avoid non-portable strlen usage. Also avoid "initializer element is not constant" warnings on older GCC that doesn't like explicit type specification in aggregate initialization. Co-authored-by: NRK Co-authored-by: nanahi <130121847+na-na-hi@users.noreply.github.com> --- misc/bstr.h | 4 ++-- video/out/vo_tct.c | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/misc/bstr.h b/misc/bstr.h index 482faa661e..aaae7d695c 100644 --- a/misc/bstr.h +++ b/misc/bstr.h @@ -56,11 +56,11 @@ static inline struct bstr bstrdup(void *talloc_ctx, struct bstr str) return r; } -#define bstr0_s(s) (struct bstr){(unsigned char *)(s), (s) ? strlen(s) : 0} +#define bstr0_lit(s) {(unsigned char *)(s), sizeof("" s) - 1} static inline struct bstr bstr0(const char *s) { - return bstr0_s(s); + return (struct bstr){(unsigned char *)s, s ? strlen(s) : 0}; } int bstrcmp(struct bstr str1, struct bstr str2); diff --git a/video/out/vo_tct.c b/video/out/vo_tct.c index ca13cf04cf..0ca6ea647f 100644 --- a/video/out/vo_tct.c +++ b/video/out/vo_tct.c @@ -42,13 +42,13 @@ #define DEFAULT_WIDTH 80 #define DEFAULT_HEIGHT 25 -static const bstr TERM_ESC_CLEAR_COLORS = bstr0_s("\033[0m"); -static const bstr TERM_ESC_COLOR256_BG = bstr0_s("\033[48;5"); -static const bstr TERM_ESC_COLOR256_FG = bstr0_s("\033[38;5"); -static const bstr TERM_ESC_COLOR24BIT_BG = bstr0_s("\033[48;2"); -static const bstr TERM_ESC_COLOR24BIT_FG = bstr0_s("\033[38;2"); +static const bstr TERM_ESC_CLEAR_COLORS = bstr0_lit("\033[0m"); +static const bstr TERM_ESC_COLOR256_BG = bstr0_lit("\033[48;5"); +static const bstr TERM_ESC_COLOR256_FG = bstr0_lit("\033[38;5"); +static const bstr TERM_ESC_COLOR24BIT_BG = bstr0_lit("\033[48;2"); +static const bstr TERM_ESC_COLOR24BIT_FG = bstr0_lit("\033[38;2"); -static const bstr UNICODE_LOWER_HALF_BLOCK = bstr0_s("\xe2\x96\x84"); +static const bstr UNICODE_LOWER_HALF_BLOCK = bstr0_lit("\xe2\x96\x84"); #define WRITE_STR(str) fwrite((str), strlen(str), 1, stdout) @@ -120,14 +120,14 @@ static void print_seq3(bstr *frame, struct lut_item *lut, bstr prefix, bstr_xappend(NULL, frame, (bstr){ lut[r].str, lut[r].width }); bstr_xappend(NULL, frame, (bstr){ lut[g].str, lut[g].width }); bstr_xappend(NULL, frame, (bstr){ lut[b].str, lut[b].width }); - bstr_xappend(NULL, frame, bstr0_s("m")); + bstr_xappend(NULL, frame, (bstr)bstr0_lit("m")); } static void print_seq1(bstr *frame, struct lut_item *lut, bstr prefix, uint8_t c) { bstr_xappend(NULL, frame, prefix); bstr_xappend(NULL, frame, (bstr){ lut[c].str, lut[c].width }); - bstr_xappend(NULL, frame, bstr0_s("m")); + bstr_xappend(NULL, frame, (bstr)bstr0_lit("m")); } static void print_buffer(bstr *frame) @@ -157,7 +157,7 @@ static void write_plain(bstr *frame, } else { print_seq3(frame, lut, TERM_ESC_COLOR24BIT_BG, r, g, b); } - bstr_xappend(NULL, frame, bstr0_s(" ")); + bstr_xappend(NULL, frame, (bstr)bstr0_lit(" ")); if (buffering <= VO_TCT_BUFFER_PIXEL) print_buffer(frame); } @@ -288,7 +288,7 @@ static void flip_page(struct vo *vo) p->opts.term256, p->lut, p->opts.buffering); } - bstr_xappend(NULL, &p->frame_buf, bstr0_s("\n")); + bstr_xappend(NULL, &p->frame_buf, (bstr)bstr0_lit("\n")); if (p->opts.buffering <= VO_TCT_BUFFER_FRAME) print_buffer(&p->frame_buf);