summaryrefslogtreecommitdiff
path: root/lib/string.c
blob: 2da2070e7cee2840ce8832ef317e23eb3c25318a (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
#include <libc/string.h> 
#include <stdbool.h>

int strcmp(const char *s1, const char *s2)
{
	size_t s1len = strlen(s1);
	size_t s2len = strlen(s2);

	return memcmp(s1, s2, s1len < s2len ? s1len : s2len);
}

size_t strlen(const char *s)
{
    size_t l = 0;

    while (s != nullptr && *s++) l++;

    return l;
}

size_t wcslen(const wchar_t *s)
{
	size_t l = 0;

	while (s != nullptr && *s++) l++;

	return l;
}

size_t mbstowcs(wchar_t *dst, const char *src, size_t len)
{
	if (dst == nullptr || src == nullptr)
	{
		return -1;
	}

	size_t i;

	for (i = 0; i < len; i++)
	{
		if ((dst[i] = src[i]) == 0)
		{
			break;
		}
	}

	dst[i] = L'\0';

	return i;
}

static long long valueof(int c, int base)
{
    long long result = 0;

    if (base < 2 || base > 36)
    {
        // bases less than 2 or greater than 36 are invalid.
        return -1;
    }

    // check if value is an ascii numeric character
    if (c >= '0' && c <= '9')
    {
        // simple as
        result = (long long)(c ^ 0x30);
    }

    // check if value is an uppercase ASCII alphabetical character
    else if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
    {
        // mask in uppercase bit
        c |= 0x20;
        // value is 10 plus the alphabetical order of the character
        result = (long long)(10 + c - 'a');
    }
    else
    {
        // character has no valid interpretation under base 36
        return -1LL;
    }

    if (result > (base - 1))
    {
        // character is not valid under given base
        return -1LL;
    }

    return result;
}

unsigned long long strtoull(
        const char *restrict begin,
        char **restrict end,
        int base)
{
    long result = 0;
    const char *current = begin;

    // check for empty string
    if (*current == 0)
    {
        if (end)
        {
            *end = (char *)begin;
            return result;
        }
    }

    // skip initial whitespace
    bool whitespace = true;
    while (whitespace)
    {
        switch (*current)
        {
            case 0x9:
            case 0xa:
            case 0xb:
            case 0xc:
            case 0xd:
            case 0x20:
                current++;
                break;
            default:
                whitespace = false;
        }
    }

    bool negative = false;
    // detect if negative sign is used
    if (*current == '-')
    {
        negative = true;
        current++;
    }

    // detect base from input
    if (base == 0)
    {
        // base is non-decimal
        if (current[0] == 0)
        {
            if (current[1] == 'b')
            {
                base = 2;
                current += 2;
            }
            else if (current[1] == 'x')
            {
                base = 16;
                current += 2;
            }
            else
            {
                base = 8;
                current++;
            }
        }
        // base is decimal otherwise
        else
        {
            base = 10;
        }
    }
    // do the actual conversion now
    while (*current)
    {
        long long value = valueof(*current, base);

        // we've reached the end of the conversion
        if (value == -1)
        {
            break;
        }

        result *= base;
        result += value;
        current++;
    }

    // store the end pointer if needed
    if (end)
    {
        *end = (char *)current;
    }

    // flip the result if we're meant to
    if (negative)
    {
        result = -result;
    }

    return result;
}