ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
output_buffer.c
Go to the documentation of this file.
1
6
7/* Storage capacities in elements; text buffers include the terminating NUL. */
8#define VIDEO_ASCII_OUTPUT_BUFFER_TMP_BUFFER_SIZE 10
9
11#include <ascii-chat/common.h>
14
15/*****************************************************************************/
16// char* output buffer helpers
17//
18// ob_* - build strings with an output buffer
19// ob_reserve - reserve space in the output buffer
20// ob_putc - append a single character to the output buffer
21// ob_write - append a string to the output buffer
22// ob_term - terminate the output buffer
23// ob_u8 - append a decimal number to the output buffer
24// ob_u32 - append a decimal number to the output buffer
25
26void ob_reserve(outbuf_t *ob, size_t need) {
27 if (!ob)
28 return;
29 if (need > SIZE_MAX - ob->len)
30 FATAL(ERROR_MEMORY, "Output buffer size overflow");
31 size_t required = ob->len + need;
32 if (ob->cap && required <= ob->cap)
33 return;
34 size_t capacity = ob->cap ? ob->cap : 4096;
35 while (capacity < required) {
36 size_t increment = capacity / 2 + 1;
37 if (capacity > SIZE_MAX - increment) {
38 capacity = required;
39 break;
40 }
41 capacity += increment;
42 }
43 ob->buf = SAFE_REALLOC(ob->buf, capacity, char *);
44 ob->cap = capacity;
45}
46
47void ob_putc(outbuf_t *ob, char c) {
48 if (!ob)
49 return;
50 ob_reserve(ob, 1);
51 ob->buf[ob->len++] = c;
52}
53
54void ob_write(outbuf_t *ob, const void *s, size_t n) {
55 if (!ob || n == 0)
56 return;
57 ob_reserve(ob, n);
58 SAFE_MEMCPY(ob->buf + ob->len, ob->cap - ob->len, s, n);
59 ob->len += n;
60}
61
62void ob_term(outbuf_t *ob) {
63 if (!ob)
64 return;
65 // Reserve space for null terminator if needed
66 if (ob->len >= ob->cap) {
67 ob_reserve(ob, 1);
68 }
69 // Append null terminator WITHOUT incrementing length
70 // (null terminator is not part of the string length)
71 ob->buf[ob->len] = '\0';
72}
73
74// Fast decimal for uint8_t (0..255)
75void ob_u8(outbuf_t *ob, uint8_t v) {
76 if (!ob)
77 return;
78 if (v >= 100) {
79 uint8_t d0 = v / 100;
80 uint8_t r = v % 100;
81 uint8_t d1 = r / 10;
82 uint8_t d2 = r % 10;
83 ob_putc(ob, '0' + d0);
84 ob_putc(ob, '0' + d1);
85 ob_putc(ob, '0' + d2);
86 } else if (v >= 10) {
87 uint8_t d1 = v / 10;
88 uint8_t d2 = v % 10;
89 ob_putc(ob, '0' + d1);
90 ob_putc(ob, '0' + d2);
91 } else {
92 ob_putc(ob, '0' + v);
93 }
94}
95
96void ob_u32(outbuf_t *ob, uint32_t v) {
97 if (!ob)
98 return;
100 int i = 0;
101 do {
102 tmp[i++] = '0' + (v % 10u);
103 v /= 10u;
104 } while (v);
105 ob_reserve(ob, (size_t)i);
106 while (i--)
107 ob->buf[ob->len++] = tmp[i];
108}
109
110// Reserve once per SGR sequence; no ISA-specific encoding is necessary.
111static void emit_truecolor(outbuf_t *out, bool background, uint8_t r, uint8_t g, uint8_t b) {
112 if (!out)
113 return;
114 ob_reserve(out, 19);
115 char *start = out->buf + out->len;
116 char *p = background ? append_sgr_truecolor_bg(start, r, g, b) : append_sgr_truecolor_fg(start, r, g, b);
117 out->len += (size_t)(p - start);
118}
119
121 emit_truecolor(out, false, r, g, b);
122}
123
125 emit_truecolor(out, true, r, g, b);
126}
127
129 if (!out)
130 return;
131 ob_reserve(out, 4);
132 append_sgr_reset(out->buf + out->len);
133 out->len += 4;
134}
135
136// REP profitability calculation
138 if (runlen <= 2)
139 return false;
140 uint32_t k = runlen - 1; // Extra repetitions beyond the first character
141 uint32_t rep_cost = (uint32_t)(digits_u32(k) + 3); // ESC [ digits b
142 return k > rep_cost; // Manual repetition cost vs REP cost
143}
144
145void emit_rep(outbuf_t *ob, uint32_t extra) {
146 if (!ob)
147 return;
148 // ESC [ extra b
149 ob_putc(ob, 0x1b);
150 ob_putc(ob, '[');
151 ob_u32(ob, extra);
152 ob_putc(ob, 'b');
153}
154
155// 256-color SGR emission (foreground) - uses cached sequences for performance
157 if (!ob)
158 return;
159 // Use pre-cached color sequence instead of building on-demand
160 uint8_t seq_len;
161 const char *seq = get_sgr256_fg_string(color_idx, &seq_len);
162 ob_write(ob, seq, seq_len);
163}
164
165// 256-color SGR emission (background) - uses cached sequences for performance
167 if (!ob)
168 return;
169 // Use pre-cached color sequence instead of building on-demand
170 uint8_t seq_len;
171 const char *seq = get_sgr256_bg_string(color_idx, &seq_len);
172 ob_write(ob, seq, seq_len);
173}
174
176 emit_set_truecolor_fg(ob, r, g, b);
177}
178
180 emit_set_truecolor_bg(ob, r, g, b);
181}
182
183void emit_glyph_run(outbuf_t *out, const void *glyph, size_t bytes, uint32_t count) {
184 if (!count || !bytes)
185 return;
186 ob_write(out, glyph, bytes);
187 uint32_t extra = count - 1;
188 // Compare division rather than multiplying count by glyph size (overflow).
189 size_t cost = digits_u32(extra) + 3u;
190 if (extra > cost / bytes) {
191 emit_rep(out, extra);
192 } else {
193 for (uint32_t i = 0; i < extra; ++i)
194 ob_write(out, glyph, bytes);
195 }
196}
197
198void emit_row_end(outbuf_t *out, bool colored, bool newline) {
199 if (colored)
201 if (newline)
202 ob_putc(out, '\n');
203}
#define SAFE_REALLOC(ptr, size, cast)
unsigned int uint32_t
#define FATAL(code,...)
Exit with error code and custom message, with stack trace in debug builds.
unsigned char uint8_t
#define SAFE_MEMCPY(dest, dest_size, src, count)
@ ERROR_MEMORY
Definition error_codes.h:55
void emit_set_256_color_bg(outbuf_t *ob, uint8_t color_idx)
Emit 256-color background ANSI sequence.
void emit_set_bg(outbuf_t *ob, uint8_t r, uint8_t g, uint8_t b)
Emit background color sequence (auto-select mode).
void ob_u32(outbuf_t *ob, uint32_t v)
Append unsigned 32-bit integer as decimal string.
void ob_u8(outbuf_t *ob, uint8_t v)
Append unsigned 8-bit integer as decimal string.
void emit_set_256_color_fg(outbuf_t *ob, uint8_t color_idx)
Emit 256-color foreground ANSI sequence.
void ob_term(outbuf_t *ob)
Append null terminator to buffer.
void ob_putc(outbuf_t *ob, char c)
Append a character to buffer.
bool rep_is_profitable(uint32_t runlen)
Check if run-length encoding is profitable.
void ob_reserve(outbuf_t *ob, size_t need)
Dynamic output buffer (auto-expanding).
void emit_set_truecolor_fg(outbuf_t *out, uint8_t r, uint8_t g, uint8_t b)
Emit truecolor foreground ANSI sequence.
void emit_rep(outbuf_t *ob, uint32_t extra)
Emit run-length encoded sequence.
void emit_reset(outbuf_t *out)
Emit ANSI reset sequence.
char * get_sgr256_bg_string(uint8_t bg, uint8_t *len_out)
Get 256-color background ANSI sequence string.
Definition sgr.c:108
void emit_set_truecolor_bg(outbuf_t *out, uint8_t r, uint8_t g, uint8_t b)
Emit truecolor background ANSI sequence.
char * get_sgr256_fg_string(uint8_t fg, uint8_t *len_out)
Get 256-color foreground ANSI sequence string.
Definition sgr.c:100
void emit_set_fg(outbuf_t *ob, uint8_t r, uint8_t g, uint8_t b)
Emit foreground color sequence (auto-select mode).
void ob_write(outbuf_t *ob, const void *s, size_t n)
Append a string to buffer.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
SIMD-optimized ASCII conversion interface.
#define VIDEO_ASCII_OUTPUT_BUFFER_TMP_BUFFER_SIZE
void emit_glyph_run(outbuf_t *out, const void *glyph, size_t bytes, uint32_t count)
void emit_row_end(outbuf_t *out, bool colored, bool newline)
static bool size_t gotel_query_t * out
Definition queries.h:8
char * append_sgr_reset(char *dst)
Definition sgr.c:122
char * append_sgr_truecolor_bg(char *dst, uint8_t r, uint8_t g, uint8_t b)
Definition sgr.c:146
char * append_sgr_truecolor_fg(char *dst, uint8_t r, uint8_t g, uint8_t b)
Definition sgr.c:142
ANSI SGR (Select Graphic Rendition) color sequence generation.
Dynamic output buffer (auto-expanding).
size_t cap
Buffer capacity in bytes (maximum length before reallocation).
size_t len
Current length in bytes (excluding null terminator).
char * buf
Buffer pointer (allocated, owned by caller, must be freed).