ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
number.h
Go to the documentation of this file.
1#pragma once
2
35
36/* Storage capacities in elements; text buffers include the terminating NUL. */
37#define UTIL_NUMBER_TEMP_BUFFER_SIZE 10
38
39#include <stddef.h>
40#include <stdint.h>
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46/* ============================================================================
47 * Digit Counting
48 * ============================================================================
49 */
50
69static inline int digits_u32(uint32_t v) {
70 if (v >= 1000000000u)
71 return 10;
72 if (v >= 100000000u)
73 return 9;
74 if (v >= 10000000u)
75 return 8;
76 if (v >= 1000000u)
77 return 7;
78 if (v >= 100000u)
79 return 6;
80 if (v >= 10000u)
81 return 5;
82 if (v >= 1000u)
83 return 4;
84 if (v >= 100u)
85 return 3;
86 if (v >= 10u)
87 return 2;
88 return 1;
89}
90
107static inline int digits_u16(uint16_t v) {
108 if (v >= 10000u)
109 return 5;
110 if (v >= 1000u)
111 return 4;
112 if (v >= 100u)
113 return 3;
114 if (v >= 10u)
115 return 2;
116 return 1;
117}
118
135static inline int digits_u8(uint8_t v) {
136 if (v >= 100u)
137 return 3;
138 if (v >= 10u)
139 return 2;
140 return 1;
141}
142
143/* ============================================================================
144 * Decimal Conversion
145 * ============================================================================
146 */
147
172static inline size_t write_decimal(int value, char *dst) {
173 if (value == 0) {
174 *dst = '0';
175 return 1;
176 }
177
178 if (value < 0) {
179 // For negative numbers, just return without writing
180 // (not typically used in this codebase but handle gracefully)
181 return 0;
182 }
183
184 char temp[UTIL_NUMBER_TEMP_BUFFER_SIZE]; // Enough for 32-bit int
185 int pos = 0;
186 int v = value;
187
188 while (v > 0) {
189 temp[pos++] = '0' + (v % 10);
190 v /= 10;
191 }
192
193 // Reverse digits into dst
194 for (int i = 0; i < pos; i++) {
195 dst[i] = temp[pos - 1 - i];
196 }
197
198 return (size_t)pos;
199}
200
201/* ============================================================================
202 * Byte Formatting (for ANSI sequences)
203 * ============================================================================
204 */
205
229static inline char *write_u8(char *p, uint8_t n) {
230 if (n < 10) {
231 *p++ = '0' + n;
232 } else if (n < 100) {
233 *p++ = '0' + (n / 10);
234 *p++ = '0' + (n % 10);
235 } else {
236 *p++ = '0' + (n / 100);
237 *p++ = '0' + ((n / 10) % 10);
238 *p++ = '0' + (n % 10);
239 }
240 return p;
241}
242
261static inline char *write_u16(char *p, uint16_t n) {
262 char temp[5];
263 int pos = 0;
264 uint16_t v = n;
265
266 if (v == 0) {
267 *p++ = '0';
268 return p;
269 }
270
271 while (v > 0) {
272 temp[pos++] = '0' + (v % 10);
273 v /= 10;
274 }
275
276 // Reverse digits
277 for (int i = pos - 1; i >= 0; i--) {
278 *p++ = temp[i];
279 }
280
281 return p;
282}
283
302static inline char *write_u32(char *p, uint32_t n) {
304 int pos = 0;
305 uint32_t v = n;
306
307 if (v == 0) {
308 *p++ = '0';
309 return p;
310 }
311
312 while (v > 0) {
313 temp[pos++] = '0' + (v % 10);
314 v /= 10;
315 }
316
317 // Reverse digits
318 for (int i = pos - 1; i >= 0; i--) {
319 *p++ = temp[i];
320 }
321
322 return p;
323}
324
326
327#ifdef __cplusplus
328}
329#endif
unsigned short uint16_t
unsigned int uint32_t
unsigned char uint8_t
#define UTIL_NUMBER_TEMP_BUFFER_SIZE
Definition number.h:37