ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
number.h
Go to the documentation of this file.
1#pragma once
2
36#include <stdint.h>
37#include <stddef.h>
38
39/* ============================================================================
40 * Digit Counting
41 * ============================================================================
42 */
43
62static inline int digits_u32(uint32_t v) {
63 if (v >= 1000000000u)
64 return 10;
65 if (v >= 100000000u)
66 return 9;
67 if (v >= 10000000u)
68 return 8;
69 if (v >= 1000000u)
70 return 7;
71 if (v >= 100000u)
72 return 6;
73 if (v >= 10000u)
74 return 5;
75 if (v >= 1000u)
76 return 4;
77 if (v >= 100u)
78 return 3;
79 if (v >= 10u)
80 return 2;
81 return 1;
82}
83
100static inline int digits_u16(uint16_t v) {
101 if (v >= 10000u)
102 return 5;
103 if (v >= 1000u)
104 return 4;
105 if (v >= 100u)
106 return 3;
107 if (v >= 10u)
108 return 2;
109 return 1;
110}
111
128static inline int digits_u8(uint8_t v) {
129 if (v >= 100u)
130 return 3;
131 if (v >= 10u)
132 return 2;
133 return 1;
134}
135
136/* ============================================================================
137 * Decimal Conversion
138 * ============================================================================
139 */
140
164static inline size_t write_decimal(int value, char *dst) {
165 if (value == 0) {
166 *dst = '0';
167 return 1;
168 }
169
170 if (value < 0) {
171 // For negative numbers, just return without writing
172 // (not typically used in this codebase but handle gracefully)
173 return 0;
174 }
175
176 char temp[10]; // Enough for 32-bit int
177 int pos = 0;
178 int v = value;
179
180 while (v > 0) {
181 temp[pos++] = '0' + (v % 10);
182 v /= 10;
183 }
184
185 // Reverse digits into dst
186 for (int i = 0; i < pos; i++) {
187 dst[i] = temp[pos - 1 - i];
188 }
189
190 return (size_t)pos;
191}
192
193/* ============================================================================
194 * Byte Formatting (for ANSI sequences)
195 * ============================================================================
196 */
197
221static inline char *write_u8(char *p, uint8_t n) {
222 if (n < 10) {
223 *p++ = '0' + n;
224 } else if (n < 100) {
225 *p++ = '0' + (n / 10);
226 *p++ = '0' + (n % 10);
227 } else {
228 *p++ = '0' + (n / 100);
229 *p++ = '0' + ((n / 10) % 10);
230 *p++ = '0' + (n % 10);
231 }
232 return p;
233}
234
253static inline char *write_u16(char *p, uint16_t n) {
254 char temp[5];
255 int pos = 0;
256 uint16_t v = n;
257
258 if (v == 0) {
259 *p++ = '0';
260 return p;
261 }
262
263 while (v > 0) {
264 temp[pos++] = '0' + (v % 10);
265 v /= 10;
266 }
267
268 // Reverse digits
269 for (int i = pos - 1; i >= 0; i--) {
270 *p++ = temp[i];
271 }
272
273 return p;
274}
275
294static inline char *write_u32(char *p, uint32_t n) {
295 char temp[10];
296 int pos = 0;
297 uint32_t v = n;
298
299 if (v == 0) {
300 *p++ = '0';
301 return p;
302 }
303
304 while (v > 0) {
305 temp[pos++] = '0' + (v % 10);
306 v /= 10;
307 }
308
309 // Reverse digits
310 for (int i = pos - 1; i >= 0; i--) {
311 *p++ = temp[i];
312 }
313
314 return p;
315}
316
unsigned short uint16_t
Definition common.h:57
unsigned int uint32_t
Definition common.h:58
unsigned char uint8_t
Definition common.h:56