blob: 593b20cce564003fffe051baa3f67d342b0a8340 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include <stdbool.h>
#include <stdint.h>
bool try_sntoi(const char *s, uint32_t n, uint32_t *out) {
uint32_t calc = 0;
for (uint32_t i = 0; i < n; ++i) {
if ((s[i] < '0') || (s[i] > '9'))
return false;
calc = calc * 10 + s[i] - '0';
}
*out = calc;
return true;
}
__attribute__ ((access (write_only, 2)))
void itosz(uint32_t i, char *out) {
if (!i) {
*(uint16_t *)out = (uint16_t)'0';
return;
}
bool zero = false;
for (uint32_t m = 1000000000; m; m /= 10) {
uint8_t d = (i / m) % 10;
if (zero)
*(out++) = d + '0';
else if (d) {
zero = true;
*(out++) = d + '0';
}
}
*out = '\0';
}
const char *const hex_digits = "0123456789abcdef";
__attribute__ ((access (write_only, 2)))
void itosz_h8(uint8_t i, char *out) {
out[0] = hex_digits[i >> 4];
out[1] = hex_digits[i & 0xf];
out[2] = '\0';
}
__attribute__ ((access (write_only, 2)))
void itosz_h32(uint32_t i, char *out) {
for (uint8_t digit = 0; digit < 8; ++digit)
out[digit] = hex_digits[(i >> (28 - digit * 4)) & 0xf];
out[8] = '\0';
}
|