ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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 <stdint.h>
40#include <stddef.h>
41
42/* ============================================================================
43 * Digit Counting
44 * ============================================================================
45 */
46
65static inline int digits_u32(uint32_t v) {
66 if (v >= 1000000000u)
67 return 10;
68 if (v >= 100000000u)
69 return 9;
70 if (v >= 10000000u)
71 return 8;
72 if (v >= 1000000u)
73 return 7;
74 if (v >= 100000u)
75 return 6;
76 if (v >= 10000u)
77 return 5;
78 if (v >= 1000u)
79 return 4;
80 if (v >= 100u)
81 return 3;
82 if (v >= 10u)
83 return 2;
84 return 1;
85}
86
103static inline int digits_u16(uint16_t v) {
104 if (v >= 10000u)
105 return 5;
106 if (v >= 1000u)
107 return 4;
108 if (v >= 100u)
109 return 3;
110 if (v >= 10u)
111 return 2;
112 return 1;
113}
114
131static inline int digits_u8(uint8_t v) {
132 if (v >= 100u)
133 return 3;
134 if (v >= 10u)
135 return 2;
136 return 1;
137}
138
139/* ============================================================================
140 * Decimal Conversion
141 * ============================================================================
142 */
143
167static inline size_t write_decimal(int value, char *dst) {
168 if (value == 0) {
169 *dst = '0';
170 return 1;
171 }
172
173 if (value < 0) {
174 // For negative numbers, just return without writing
175 // (not typically used in this codebase but handle gracefully)
176 return 0;
177 }
178
179 char temp[UTIL_NUMBER_TEMP_BUFFER_SIZE]; // Enough for 32-bit int
180 int pos = 0;
181 int v = value;
182
183 while (v > 0) {
184 temp[pos++] = '0' + (v % 10);
185 v /= 10;
186 }
187
188 // Reverse digits into dst
189 for (int i = 0; i < pos; i++) {
190 dst[i] = temp[pos - 1 - i];
191 }
192
193 return (size_t)pos;
194}
195
196/* ============================================================================
197 * Byte Formatting (for ANSI sequences)
198 * ============================================================================
199 */
200
224static inline char *write_u8(char *p, uint8_t n) {
225 if (n < 10) {
226 *p++ = '0' + n;
227 } else if (n < 100) {
228 *p++ = '0' + (n / 10);
229 *p++ = '0' + (n % 10);
230 } else {
231 *p++ = '0' + (n / 100);
232 *p++ = '0' + ((n / 10) % 10);
233 *p++ = '0' + (n % 10);
234 }
235 return p;
236}
237
256static inline char *write_u16(char *p, uint16_t n) {
257 char temp[5];
258 int pos = 0;
259 uint16_t v = n;
260
261 if (v == 0) {
262 *p++ = '0';
263 return p;
264 }
265
266 while (v > 0) {
267 temp[pos++] = '0' + (v % 10);
268 v /= 10;
269 }
270
271 // Reverse digits
272 for (int i = pos - 1; i >= 0; i--) {
273 *p++ = temp[i];
274 }
275
276 return p;
277}
278
297static inline char *write_u32(char *p, uint32_t n) {
299 int pos = 0;
300 uint32_t v = n;
301
302 if (v == 0) {
303 *p++ = '0';
304 return p;
305 }
306
307 while (v > 0) {
308 temp[pos++] = '0' + (v % 10);
309 v /= 10;
310 }
311
312 // Reverse digits
313 for (int i = pos - 1; i >= 0; i--) {
314 *p++ = temp[i];
315 }
316
317 return p;
318}
319
unsigned short uint16_t
Definition common.h:61
unsigned int uint32_t
Definition common.h:62
unsigned char uint8_t
Definition common.h:60
#define UTIL_NUMBER_TEMP_BUFFER_SIZE
Definition number.h:37