aboutsummaryrefslogtreecommitdiff
path: root/src/String/ToIntS.inl
blob: f6ba991f0bccc09cd42e8b1a7a83010de85a5f66 (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
char tmp[sizeof(value) * CHAR_BIT + 2];
char *current, *end;
int sign;

end = tmp + sizeof(tmp);
current = end;
sign = 0;

/* Check for sign */
if (value < 0)
{
    sign = 1;
    value = -value;
}

/* Fill buffer from the end */
*(--current) = 0;
while (value)
{
    *(--current) = digits[value % base];
    value /= base;
}

/* Append sign */
if (sign)
    *(--current) = '-';

/* Check that string have space for the result */
if (size < (size_t)(end - current))
    return BH_ERROR;

/* Copy data */
memcpy(string, current, end - current);
if (actual)
    *actual = end - current;

return BH_OK;