forked from MetalOxideSemi/EJack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioconv.c
113 lines (106 loc) · 3.66 KB
/
ioconv.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* ioconv.c
* Created by Wang Haoren
* --------------------------------
* Implementation of conversion between string and Numeric objects
*/
#include "numeric.h"
#include <stdlib.h>
#include <string.h>
static const Udbyte P10[] = { // Power of 10
1ull,
10ull,
100ull,
1000ull,
10000ull,
100000ull,
1000000ull,
10000000ull,
100000000ull,
1000000000ull
};
Udbyte fastconvert(const char *start, const char *end) {
Udbyte ret = 0;
while (start < end) {
ret *= 10;
ret += *start++ - '0';
}
return ret;
}
bool strtoNumeric(const char *str, Numeric *numeric) {
Numeric_free(numeric);
Numeric_new(numeric);
numeric->sign = POS;
while (*str == '+' || *str == '-') {
if (*str == '-') {
numeric->sign = !numeric->sign;
}
++str;
}
size_t len = strlen(str);
const char *const p2p = strchr(str, '.');
if (p2p) {
size_t ilen = p2p - str;
size_t dlen = len - ilen - 1;
if (dlen > CALSTACK_FIXED_POINT * CALSTACK_UDBYTE_LENGTH) {
dlen = CALSTACK_FIXED_POINT * CALSTACK_UDBYTE_LENGTH;
}
numeric->nbytes = ilen / CALSTACK_UDBYTE_LENGTH + (!!(ilen % CALSTACK_UDBYTE_LENGTH));
numeric->data = malloc(numeric->nbytes * sizeof(Udbyte));
const char *current = p2p;
for (size_t i = 0; i < numeric->nbytes - 1; ++i) {
current -= CALSTACK_UDBYTE_LENGTH;
numeric->data[i] = fastconvert(current, current + CALSTACK_UDBYTE_LENGTH);
}
numeric->data[numeric->nbytes - 1] = fastconvert(str, current);
size_t nd = dlen / CALSTACK_UDBYTE_LENGTH + (!!(dlen % CALSTACK_UDBYTE_LENGTH));
if (nd > CALSTACK_FIXED_POINT) {
return false;
}
for (size_t i = 0; i < nd - 1; ++i) {
numeric->fraction[i] = fastconvert(p2p + CALSTACK_UDBYTE_LENGTH * i + 1,
p2p + CALSTACK_UDBYTE_LENGTH * i + 1 + CALSTACK_UDBYTE_LENGTH);
}
numeric->fraction[nd - 1] = fastconvert(p2p + CALSTACK_UDBYTE_LENGTH * (nd - 1) + 1,
str + len);
size_t fix = dlen % CALSTACK_UDBYTE_LENGTH;
if (fix) {
numeric->fraction[nd - 1] *= P10[CALSTACK_UDBYTE_LENGTH - fix];
}
} else {
size_t ilen = len;
numeric->nbytes = ilen / CALSTACK_UDBYTE_LENGTH + (!!(ilen % CALSTACK_UDBYTE_LENGTH));
numeric->data = malloc(numeric->nbytes * sizeof(Udbyte));
const char *current = str + ilen;
for (size_t i = 0; i < numeric->nbytes - 1; ++i) {
current -= CALSTACK_UDBYTE_LENGTH;
numeric->data[i] = fastconvert(current, current + CALSTACK_UDBYTE_LENGTH);
}
numeric->data[numeric->nbytes - 1] = fastconvert(str, current);
}
return true;
}
char *Numerictostr(const Numeric *numeric) {
char *res = malloc(CALSTACK_UDBYTE_LENGTH * (CALSTACK_FIXED_POINT + numeric->nbytes)), *dst = res;
if (numeric->sign == NEG) {
*dst++ = '-';
}
dst += sprintf(dst, "%llu", numeric->data[numeric->nbytes - 1]);
for (size_t i = numeric->nbytes - 1; i > 0; --i) {
dst += sprintf(dst, CALSTACK_OUTPUT_FIX, numeric->data[i - 1]);
}
int last = CALSTACK_FIXED_POINT - 1;
for (; last >= 0; --last) {
if (numeric->fraction[last]) {
break;
}
}
if (last >= 0) {
*dst++ = '.';
for (int i = 0; i <= last; ++i) {
dst += sprintf(dst, CALSTACK_OUTPUT_FIX, numeric->fraction[i]);
}
while (*--dst == '0');
*++dst = '\0';
}
return res;
}