-
Notifications
You must be signed in to change notification settings - Fork 0
/
itoa-FSM.c
70 lines (61 loc) · 1.49 KB
/
itoa-FSM.c
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Zhivago's itoa() implemented with an FSM */
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct emit {
void (*emitter)(struct emit *out, char value);
};
void emit(struct emit *out, char value) {
out->emitter(out, value);
}
void uitoat(struct emit *out, unsigned int value, unsigned char base) {
unsigned char digit = value % base;
if (digit > 0) {
uitoat(out, value / base, base);
emit(out, "0123456789ABCDEF"[digit]);
}
}
void uitoa(struct emit *out, unsigned int value, unsigned char base) {
unsigned char digit = value % base;
if (digit > 0) {
uitoat(out, value / base, base);
}
emit(out, "0123456789ABCDEF"[digit]);
}
void itoa(struct emit *out, int value, unsigned char base) {
if (value < 0) {
emit(out, '-');
uitoa(out, (-1U) - value + 1, base);
} else {
uitoa(out, value, base);
}
}
struct emit_to_string {
struct emit emit;
char *string;
size_t space;
size_t used;
};
void emit_to_string(struct emit *base, char value) {
struct emit_to_string *out = (void *)base;
if (out->space > 0) {
out->space -= 1;
*out->string++ = value;
}
out->used += 1;
}
int main() {
char buffer[100];
struct emit_to_string to_string = { { emit_to_string }, buffer, sizeof buffer, 0 };
struct emit *out = (void *)&to_string;
itoa(out, 12345, 10);
emit(out, ',');
itoa(out, -12345, 10);
emit(out, ',');
itoa(out, 0, 10);
emit(out, '\0');
if (to_string.used < sizeof buffer) {
puts(buffer);
}
return 0;
}