OpenCores
URL https://opencores.org/ocsvn/thor/thor/trunk

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [software/] [CC64/] [source/] [xstoul.cpp] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
#include "stdafx.h"
2
#include <stdlib.h>
3
#include <ctype.h>
4
#include <errno.h>
5
#include <limits.h>
6
#include <stddef.h>
7
#include <string.h>
8
 
9
#define BASE_MAX        36
10
 
11
static const char digits[] = {
12
        "0123456789abcdefghijklmnopqrstuvwxyz" };
13
static const char ndigs[BASE_MAX+1] = {
14
        0,0,33,21,17,14,13,12,11,11,
15
        10,10,9,9,9,9,9,8,8,8,
16
        8,8,8,8,7,7,7,7,7,7,
17
        7,7,7,7,7,7,7 };
18
 
19
unsigned long _Stoul(const char *s, char **endptr, int base)
20
{
21
        const char *sc, *sd;
22
        const char *s1, *s2;
23
        char sign;
24
        ptrdiff_t n;
25
        unsigned long x, y;
26
 
27
        for (sc = s; my_isspace(*sc); ++sc)
28
                ;
29
        sign = *sc=='-' || *sc=='+' ? *sc++ : '+';
30
        if (base < 0 || base == 1 || BASE_MAX < base)
31
        {
32
                if (endptr)
33
                        *endptr = (char *)s;
34
                return (0);
35
        }
36
        else if (base) {
37
                if (base==16 && *sc=='0' && (sc[1]=='x' || sc[1]=='X'))
38
                        sc += 2;
39
        }
40
        else if (*sc != '0')
41
                base = 10;
42
        else if (sc[1]=='x' || sc[1]=='X')
43
                base = 16, sc += 2;
44
        else
45
                base = 8;
46
        for (s1 = sc; *sc == '0'; ++sc)
47
                ;
48
        x = 0;
49
        for (s2 = sc; (sd = (const char *)memchr((const void *)digits,tolower(*sc),base)) != (char *)NULL; ++sc) {
50
                y = x;
51
                x = x * base + (sd - digits);
52
        }
53
        if (s1 == sc) {
54
                if (endptr)
55
                        *endptr = (char *)s;
56
                return (0);
57
        }
58
        n = sc - s2 - ndigs[base];
59
        if (n < 0)
60
                ;
61
        else if (0 < n || x < x - sc[-1] || (x - sc[-1]) / base != y) {
62
                errno = ERANGE;
63
                x = ULONG_MAX;
64
        }
65
        if (sign=='-') {
66
                x = ~x;
67
                x++;
68
        }
69
        if (endptr)
70
                *endptr = (char *)sc;
71
        return (x);
72
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.