summaryrefslogtreecommitdiffhomepage
path: root/include/stc/csview.h
blob: ac120e156d31e311b82994fd59d1ea031108e36f (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
/* MIT License
 *
 * Copyright (c) 2021 Tyge Løvset, NORCE, www.norceresearch.no
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef CSVIEW_H_INCLUDED
#define CSVIEW_H_INCLUDED

#include "cstr.h"

typedef                 struct csview { const char* str; size_t size; } csview;
typedef                 struct csview_iter { const char *ref; } csview_iter;
typedef                 char csview_value;

#define                 csview_null  c_make(csview){"", 0}
#define                 csview_npos  cstr_npos
#define                 c_svfmt      "%.*s"
#define                 c_svarg(sv)  (int)(sv).size, (sv).str
#define                 c_sv(literal) csview_new(literal)
#define                 cstr_sv(s) csview_from_s(s)

STC_API csview          csview_substr(csview sv, intptr_t pos, size_t n);
STC_API csview          csview_slice(csview sv, intptr_t p1, intptr_t p2);
STC_API csview          csview_first_token(csview sv, csview sep);
STC_API csview          csview_next_token(csview sv, csview sep, csview tok);

#define                 csview_new(literal) \
                            c_make(csview){literal, sizeof c_make(c_strlit){literal} - 1}
STC_INLINE csview       csview_from(const char* str)
                            { return c_make(csview){str, strlen(str)}; }
STC_INLINE csview       csview_from_n(const char* str, size_t n)
                            { return c_make(csview){str, n}; }
STC_INLINE csview       csview_from_s(cstr s)
                            { return c_make(csview){s.str, _cstr_rep(&s)->size}; }
STC_INLINE size_t       csview_size(csview sv) { return sv.size; }
STC_INLINE size_t       csview_length(csview sv) { return sv.size; }
STC_INLINE bool         csview_empty(csview sv) { return sv.size == 0; }
STC_INLINE char         csview_front(csview sv) { return sv.str[0]; }
STC_INLINE char         csview_back(csview sv) { return sv.str[sv.size - 1]; }

STC_INLINE void         csview_clear(csview* self) { *self = csview_null; }

STC_INLINE bool         csview_equals(csview sv, csview sv2)
                            { return sv.size == sv2.size && !memcmp(sv.str, sv2.str, sv.size); }
STC_INLINE size_t       csview_find(csview sv, csview needle)
                            { char* res = c_strnstrn(sv.str, needle.str, sv.size, needle.size);
                              return res ? res - sv.str : cstr_npos; }
STC_INLINE bool         csview_contains(csview sv, csview needle)
                            { return c_strnstrn(sv.str, needle.str, sv.size, needle.size) != NULL; }
STC_INLINE bool         csview_starts_with(csview sv, csview sub)
                            { if (sub.size > sv.size) return false;
                              return !memcmp(sv.str, sub.str, sub.size); }
STC_INLINE bool         csview_ends_with(csview sv, csview sub)
                            { if (sub.size > sv.size) return false;
                              return !memcmp(sv.str + sv.size - sub.size, sub.str, sub.size); }
STC_INLINE csview_iter  csview_begin(const csview* self)
                            { return c_make(csview_iter){self->str}; }
STC_INLINE csview_iter  csview_end(const csview* self)
                            { return c_make(csview_iter){self->str + self->size}; }
STC_INLINE void         csview_next(csview_iter* it) { ++it->ref; }


/* cstr interaction with csview: */

STC_INLINE cstr         cstr_from_v(csview sv)
                            { return cstr_from_n(sv.str, sv.size); }
STC_INLINE cstr         cstr_from_replace_all_v(csview sv, csview find, csview repl)
                            { return cstr_from_replace_all(sv.str, sv.size, find.str, find.size,
                                                           repl.str, repl.size); }
STC_INLINE csview       cstr_to_v(const cstr* self)
                            { return c_make(csview){self->str, _cstr_rep(self)->size}; }
STC_INLINE csview       cstr_substr(cstr s, intptr_t pos, size_t n)
                            { return csview_substr(csview_from_s(s), pos, n); }
STC_INLINE csview       cstr_slice(cstr s, intptr_t p1, intptr_t p2)
                            { return csview_slice(csview_from_s(s), p1, p2); }
STC_INLINE cstr*        cstr_assign_v(cstr* self, csview sv)
                            { return cstr_assign_n(self, sv.str, sv.size); }
STC_INLINE cstr*        cstr_append_v(cstr* self, csview sv)
                            { return cstr_append_n(self, sv.str, sv.size); }
STC_INLINE void         cstr_insert_v(cstr* self, size_t pos, csview sv)
                            { cstr_replace_n(self, pos, 0, sv.str, sv.size); }
STC_INLINE void         cstr_replace_v(cstr* self, size_t pos, size_t len, csview sv)
                            { cstr_replace_n(self, pos, len, sv.str, sv.size); }
STC_INLINE bool         cstr_equals_v(cstr s, csview sv)
                            { return sv.size == cstr_size(s) && !memcmp(s.str, sv.str, sv.size); }
STC_INLINE size_t       cstr_find_v(cstr s, csview needle)
                            { char* res = c_strnstrn(s.str, needle.str, cstr_size(s), needle.size);
                              return res ? res - s.str : cstr_npos; }
STC_INLINE bool         cstr_contains_v(cstr s, csview needle)
                            { return c_strnstrn(s.str, needle.str, cstr_size(s), needle.size) != NULL; }
STC_INLINE bool         cstr_starts_with_v(cstr s, csview sub)
                            { if (sub.size > cstr_size(s)) return false;
                              return !memcmp(s.str, sub.str, sub.size); }
STC_INLINE bool         cstr_ends_with_v(cstr s, csview sub)
                            { if (sub.size > cstr_size(s)) return false;
                              return !memcmp(s.str + cstr_size(s) - sub.size, sub.str, sub.size); }

/* ---- Container helper functions ---- */

STC_INLINE int          csview_cmp(const csview* x, const csview* y) { 
                            const size_t m = x->size < y->size ? x->size : y->size; 
                            const int c = memcmp(x->str, y->str, m);
                            return c ? c : x->size - y->size;
                        }
#define                 csview_hash(xp, dummy) c_strhash((xp)->str)
#define                 csview_equalto(xp, yp) (csview_cmp(xp, yp) == 0)

/* -------------------------- IMPLEMENTATION ------------------------- */

#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)

STC_DEF csview
csview_substr(csview sv, intptr_t pos, size_t n) {
    if (pos < 0) { pos += sv.size; if (pos < 0) pos = 0; }
    if (pos > sv.size) pos = sv.size; if (pos + n > sv.size) n = sv.size - pos;
    sv.str += pos, sv.size = n; return sv;
}

STC_DEF csview
csview_slice(csview sv, intptr_t p1, intptr_t p2) {
    if (p1 < 0) { p1 += sv.size; if (p1 < 0) p1 = 0; }
    if (p2 < 0) p2 += sv.size; if (p2 > sv.size) p2 = sv.size;
    sv.str += p1, sv.size = p2 > p1 ? p2 - p1 : 0; return sv;
}

STC_DEF csview
csview_first_token(csview sv, csview sep) {
    const char* res = c_strnstrn(sv.str, sep.str, sv.size, sep.size);
    return c_make(csview){sv.str, (res ? res - sv.str : sv.size)};
}

STC_DEF csview
csview_next_token(csview sv, csview sep, csview tok) {
    if (&tok.str[tok.size] == &sv.str[sv.size])
        return c_make(csview){&sv.str[sv.size], 0};
    tok.str += tok.size + sep.size;
    size_t n = sv.size - (tok.str - sv.str);
    const char* res = c_strnstrn(tok.str, sep.str, n, sep.size);
    tok.size = res ? res - tok.str : n;
    return tok;
}

#endif
#endif