summaryrefslogtreecommitdiff
path: root/archive/Podium/p_string.c
blob: 73cb0ea97bdb1fbe37c42d44d5df327070ed8eaf (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
PODEF char*
p_find_char(char *buf, char needle)
{
    while (buf && *buf != needle) buf++;
    return buf;
}

PODEF bool
p_strcmp(const char *a, const char *b)
{
    if (p_strlen(a) != p_strlen(b))
        return false;

    while (a && b && *a != '\0') {
        if (*(a++) != *(b++)) return false;
    }

    return true;
}

PODEF int
p_strlen(const char *a)
{
    int i = 0;
    while (*a++ != '\0') i++;
    return i;
}

PODEF int
p_strtoi(const char *str)
{
    int res = 0;

    while (*str) {
        if (*str >= '0' && *str <= '9') {
            res = res * 10 + (*str - '0');
        } else {
            return res;
        }
        str++;
    }

    return res;
}

PODEF char*
p_strtok(const char *str, char c)
{
    while (*str != c && *str++ != '\0');
    return (*str == c) ? (char*) ++str : 0;
}

PODEF P_StringView
p_strview(const char *cstr) 
{
    P_StringView sv = {0};
    sv.cstr = cstr;
    sv.len = p_strlen(cstr);
    return sv;
}

PODEF void
p_strview_chop_left(P_StringView *sv, u32 n) 
{
    if (n > sv->len) n = sv->len;
    sv->cstr += n;
    sv->len  -= n;
}

PODEF void
p_strview_chop_right(P_StringView *sv, u32 n) 
{
    if (n > sv->len) n = sv->len;
    sv->len  -= n;
}

PODEF P_StringView
p_strview_chop_delim(P_StringView *sv, char delim) 
{
    u32 i = 0;
    while (i < sv->len && sv->cstr[i] != delim) {
        i++;
    }

    P_StringView tok;
    if (i < sv->len) {
        tok.cstr = sv->cstr;
        tok.len  = i; 
        p_strview_chop_left(sv, i + 1);
        return tok;
    }

    tok = *sv;
    p_strview_chop_left(sv, sv->len);
    return tok;
}

PODEF P_StringView
p_strview_chop_type(P_StringView *sv, int(*istype)(int))
{
    u32 i = 0;
    while (i < sv->len && istype(sv->cstr[i])) {
        i++;
    }

    P_StringView tok;
    if (i < sv->len) {
        tok.cstr = sv->cstr;
        tok.len  = i; 
        p_strview_chop_left(sv, i + 1);
        return tok;
    }

    tok = *sv;
    p_strview_chop_left(sv, sv->len);
    return tok;
}


PODEF void
p_strview_trim_left(P_StringView *sv) 
{
    while (sv->len > 0 && p_is_space(sv->cstr[0])) {
        p_strview_chop_left(sv, 1);
    }
}

PODEF void
p_strview_trim_right(P_StringView *sv) 
{
    while (sv->len > 0 && p_is_space(sv->cstr[sv->len-1])) {
        p_strview_chop_right(sv, 1);
    }
}

PODEF void
p_strview_trim(P_StringView *sv) 
{
    p_strview_trim_left(sv);
    p_strview_trim_right(sv);
}

PODEF int 
p_is_space(int c) 
{
    /* Checks for: space, form feed (\f), line feed (\n), 
     * carriage return (\r), horizontal tab (\t), vertical tab (\v) */
    return (c == ' ' || (c >= '\t' && c <= '\r'));
}

PODEF int 
p_is_digit(int c) 
{
    return (c >= '0' && c <= '9');
}

PODEF int 
p_is_alpha(int c) 
{
    return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}

PODEF int 
p_is_alnum(int c) 
{
    return (p_is_alpha(c) || p_is_digit(c));
}

PODEF int 
p_is_hex(int c) 
{
    return (p_is_digit(c) || 
           (c >= 'a' && c <= 'f') || 
           (c >= 'A' && c <= 'F'));
}

PODEF int 
p_is_upper(int c) 
{
    return (c >= 'A' && c <= 'Z');
}

PODEF int 
p_is_lower(int c) 
{
    return (c >= 'a' && c <= 'z');
}

PODEF int 
p_to_lower(int c) 
{
    if (p_is_upper(c)) return (c + ('a' - 'A'));
    return c;
}

PODEF int 
p_to_upper(int c) 
{
    if (p_is_lower(c)) return (c - ('a' - 'A'));
    return c;
}