Skip to content

Commit

Permalink
bstr: change bstr0_s to bstr0_lit, restrict it only for string literals
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: nanahi <[email protected]>
  • Loading branch information
3 people committed Apr 8, 2024
1 parent 2d4fae4 commit d3941f2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions misc/bstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions video/out/vo_tct.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit d3941f2

Please sign in to comment.