aboutsummaryrefslogtreecommitdiff
path: root/src/String/Int.c
blob: a9edf90074058b0a2772f2519bff488e9cc401ab (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <BH/String.h>
#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>


static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
static const char lookup[] =
{
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
    -1, 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, -1, -1, -1, -1, -1,
    -1, 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, -1, -1, -1, -1, -1,
};


static void skipSpace(const char **string,
                      size_t *size)
{
    while (isspace(**string))
    {
        (*string)++;
        if (size) (*size)++;
    }
}


static void handleSign(const char **string,
                       size_t *size,
                       int *sign)
{
    *sign = 1;
    if (**string == '-')
    {
        *sign = -1; (*string)++;
        if (size) (*size)++;
    }
}


static void guessBase(const char **string,
                      size_t *size,
                      int *base)
{
    if (*base != 0)
        return;

    *base = 10;
    if (**string != '0')
        return;

    (*string)++;
    if (size)
        (*size)++;

    switch (**string)
    {
    case 'x': case 'X':
        *base = 16;
        (*string)++;
        if (size)
            (*size)++;
        break;

    case 'b': case 'B':
        *base = 2;
        (*string)++;
        if (size)
            (*size)++;
        break;

    default:
        *base = 8;
    }
}


#define TEMPLATE_IMPL \
    char tmp[sizeof(value) * CHAR_BIT + 1]; char *current, *end, *result; \
    int sign; end = tmp + sizeof(tmp); current = end; sign = 0; \
    if (value < 0) { sign = 1; value = -value; } while (value) { \
    *(--current) = digits[value % base]; value /= base; } \
    if (sign) { *(--current) = '-'; } result = malloc(end - current + 1); \
    if (!result) { return NULL; } memcpy(result, current, end - current); \
    result[end - current] = 0; return result;


char *BH_StringFromInt8s(int8_t value,
                         int base)
{
    TEMPLATE_IMPL
}


char *BH_StringFromInt16s(int16_t value,
                          int base)
{
    TEMPLATE_IMPL
}


char *BH_StringFromInt32s(int32_t value,
                          int base)
{
    TEMPLATE_IMPL
}


char *BH_StringFromInt64s(int64_t value,
                          int base)
{
    TEMPLATE_IMPL
}


#undef TEMPLATE_IMPL
#define TEMPLATE_IMPL \
    char tmp[sizeof(value) * CHAR_BIT]; char *current, *end, *result; \
    end = tmp + sizeof(tmp); current = end; while (value) { \
    *(--current) = digits[value % base]; value /= base; } \
    result = malloc(end - current + 1); if (!result) { return NULL; } \
    memcpy(result, current, end - current); result[end - current] = 0; \
    return result;


char *BH_StringFromInt8u(uint8_t value,
                         int base)
{
    TEMPLATE_IMPL
}


char *BH_StringFromInt16u(uint16_t value,
                          int base)
{
    TEMPLATE_IMPL
}


char *BH_StringFromInt32u(uint32_t value,
                          int base)
{
    TEMPLATE_IMPL
}


char *BH_StringFromInt64u(uint64_t value,
                          int base)
{
    TEMPLATE_IMPL
}


#undef TEMPLATE_IMPL
#define TEMPLATE_IMPL(type) \
    type result = 0; int sign, flag = 0; char sym; *size = 0; \
    if (base != 0 && (base < 2 || base > 36)) { return 0; } \
    skipSpace(&string, size); handleSign(&string, size, &sign); \
    guessBase(&string, size, &base); while(*string) { sym = *(string++); \
    sym = lookup[(unsigned int)sym]; if (sym >= base || sym == -1) { break; } \
    if (size) { (*size)++; } result = result * base + sym; flag = 1; } \
    if (!result && !flag && size) { *size = 0; } return result * sign;


int8_t BH_StringToInt8s(const char *string,
                        size_t *size,
                        int base)
{
    TEMPLATE_IMPL(int8_t)
}


int16_t BH_StringToInt16s(const char *string,
                          size_t *size,
                          int base)
{
    TEMPLATE_IMPL(int16_t)
}


int32_t BH_StringToInt32s(const char *string,
                          size_t *size,
                          int base)
{
    TEMPLATE_IMPL(int32_t)
}


int64_t BH_StringToInt64s(const char *string,
                          size_t *size,
                          int base)
{
    TEMPLATE_IMPL(int64_t)
}


uint8_t BH_StringToInt8u(const char *string,
                         size_t *size,
                         int base)
{
    TEMPLATE_IMPL(uint8_t)
}


uint16_t BH_StringToInt16u(const char *string,
                           size_t *size,
                           int base)
{
    TEMPLATE_IMPL(uint16_t)
}


uint32_t BH_StringToInt32u(const char *string,
                           size_t *size,
                           int base)
{
    TEMPLATE_IMPL(uint32_t)
}


uint64_t BH_StringToInt64u(const char *string,
                           size_t *size,
                           int base)
{
    TEMPLATE_IMPL(uint64_t)
}


#undef TEMPLATE_IMPL