summaryrefslogtreecommitdiffstats
path: root/wlite/wlite_widetoll_.c
blob: f4610975588fdc53f82ba1c28b696ea415cd5338 (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
/*
 * $Id$
 *
 * Copyright (C) 2003  Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Original Author: Adrian Havill <havill@redhat.com>
 *
 * Contributors:
 */

#include <limits.h>  // INT_MIN, ULONG_MAX, LONG_MIN
#include "wlite_config.h"   // wchar_t, NULL, size_t

#include "wlite_wchar.h"    // prototypes
#include "wlite_wctype.h"

static wlite_wint_t
next_char(const wchar_t **s) {
    wlite_wint_t c = **s;

#if WLITE_EXTENSIONS
    while (c != L'\0') {
        if (!wlite_iswctype(c, wlite_ignore_)) break;
        c = *++*s;
    }
#endif
    if (c != L'\0') {
        ++*s;
        c = wlite_towctrans(c, wlite_tolower_);
#if WLITE_EXTENSIONS
        c = wlite_towctrans(c, wlite_tocase_);
        c = wlite_towctrans(c, wlite_tonorm_);
#endif
    }
    return c;
}

long long
wlite_widetoll_(const wchar_t *s, wchar_t **endptr, unsigned base) {
    static const wchar_t *const valid = L"0123456789abcdefghijklmnopqrstuvwxyz";
    const size_t base_max = 36;
    const wchar_t *ptr = s, *digit, *start;
    wlite_wint_t c;
    wchar_t sign;
    long long i = 0;

    do {
        c = next_char(&ptr);
        if (c == L'\0') break;
    } while (wlite_iswctype(c, wlite_space_));
    if (c != L'-' && c != L'+') {
        if (ptr != s) {
            --ptr;
        }
        sign = L'+';
    }
    else sign = c;
    if (base > base_max) {
        if (endptr) {
            *endptr = (wchar_t *) s;
        }
        return 0;
    }
    else if (base != 0) {
        if (base == 16 && ptr[0] == L'0' && (ptr[1] == L'x')) {
            ptr += 2;
        }
    }
    else {
        if (ptr[0] != L'0') {
            base = 10;
        }
        else if (ptr[1] == L'x') {
            base = 16;
            ptr += 2;
        }
        else base = 8;
    }
    start = ptr;
    while ((digit = wlite_wmemchr(valid, c = next_char(&ptr), base)) != NULL) {
        i *= base;
        i += (unsigned) (digit - valid);
        if ((i > ULONG_MAX) || (i < LONG_MIN)) break;
    }
    if (start == ptr) {
        if (endptr != NULL) {
            *endptr = (wchar_t *) s;
        }
        return 0;
    }
    if (sign == L'-') {
        i = -i;
    }
    if (endptr != NULL) {
        if (digit == NULL && c != L'\0') {
            ptr--;
        }
        *endptr = (wchar_t *) ptr;
    }
    return i;
}