-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsob.h
246 lines (192 loc) · 4.95 KB
/
sob.h
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#ifndef SOB_H
#define SOB_H
#include<stddef.h>
#include<stdbool.h>
static size_t nopos = -1;
typedef struct
{
const char * str;
size_t length;
} sob_t;
sob_t sob_create(const char * str, size_t length);
sob_t sob_create_from_cstr(const char * str);
sob_t sob_triml(sob_t * s);
sob_t sob_trimr(sob_t * s);
sob_t sob_trim(sob_t * s);
void sob_swap(sob_t * s1, sob_t * s2);
void sob_copy_to_cstr(sob_t * s, char * dist, size_t count);
void sob_copy(sob_t * s, sob_t * dist, size_t count);
sob_t sob_substr(sob_t * s, size_t pos, size_t count);
sob_t sob_slice(sob_t * s, size_t start, size_t end);
bool sob_compare(sob_t * s1, sob_t * s2);
bool sob_contains_cstr(sob_t * s, const char * sub, size_t sub_length);
bool sob_contains(sob_t * s, sob_t * sub);
size_t sob_find_cstr(sob_t * s, const char * sub, size_t sub_length);
size_t sob_find(sob_t * s, sob_t * sub);
bool sob_starts_with_cstr(sob_t * s, const char * sub, size_t sub_len);
bool sob_starts_with(sob_t * s, sob_t * sub);
bool sob_ends_with_cstr(sob_t * s, const char * sub, size_t sub_len);
bool sob_ends_with(sob_t * s, sob_t * sub);
const char * sob_at(sob_t * s, size_t index);
size_t sob_length(sob_t * s);
bool sob_is_empty(sob_t * s);
void sob_print(sob_t * s);
#endif // SOB_H
/////////////////////////////////////////////////////////////////
#ifdef SOB_IMPLEMENTATION
#include<stdio.h>
#include<string.h>
//TODO: null checking
sob_t sob_create(const char * str, size_t length)
{
return (sob_t) {
.str = str,
.length = length
};
}
sob_t sob_create_from_cstr(const char * str)
{
return (sob_t) {
.str = str,
.length = strlen(str)
};
}
sob_t sob_triml(sob_t * s)
{
size_t offset = 0;
for (size_t i = 0; i < s->length; ++i) {
if (s->str[i] != ' ')
break;
offset += 1;
}
sob_t result = sob_create(s->str + offset, s->length - offset);
return result;
}
sob_t sob_trimr(sob_t * s)
{
size_t offset = 0;
for (int i = s->length - 1; i >= 0; --i) {
if (s->str[i] != ' ')
break;
offset += 1;
}
sob_t result = sob_create(s->str, s->length - offset);
return result;
}
sob_t sob_trim(sob_t * s)
{
sob_t result = sob_triml(s);
return sob_trimr(&result);
}
void sob_swap(sob_t * s1, sob_t * s2)
{
sob_t tmp = *s1;
*s1 = *s2;
*s2 = tmp;
}
void sob_copy_to_cstr(sob_t * s, char * dist, size_t count)
{
strncpy(dist, s->str, count);
strncpy(dist + count, "\0", 1);
}
void sob_copy(sob_t * s, sob_t * dist, size_t count)
{
sob_copy_to_cstr(s, dist->str, count);
}
sob_t sob_substr(sob_t * s, size_t index, size_t count)
{
return sob_create(s->str + index, count);
}
sob_t sob_slice(sob_t * s, size_t start, size_t end)
{
return sob_create(s->str + start, (end - start) + 1);
}
bool sob_compare(sob_t * s1, sob_t * s2)
{
if (s1 == s2) return true;
if (s1->length > s2->length || s2->length > s1->length)
return false;
for (size_t i = 0; i < s1->length; ++i) {
if (s1->str[i] != s2->str[i]) {
return false;
}
}
return true;
}
bool sob_contains_cstr(sob_t * s, const char * sub, size_t sub_length)
{
if (sub_length > s->length) return false;
size_t i = 0;
size_t j = 0;
while (j < sub_length && i < s->length) {
if (sub[j] != s->str[i]) {
j = 0;
++i;
continue;
}
++j;
++i;
}
return j == 0 ? false : true;
}
bool sob_contains(sob_t * s, sob_t * sub)
{
return sob_contains_cstr(s, sub->str, sub->length);
}
size_t sob_find_cstr(sob_t * s, const char * sub, size_t sub_length)
{
if (sub_length > s->length) return nopos;
size_t i = 0;
size_t j = 0;
while (j < sub_length && i < s->length) {
if (sub[j] != s->str[i]) {
j = 0;
++i;
continue;
}
++j;
++i;
}
return j == 0 ? nopos : (i - j);
}
size_t sob_find(sob_t * s, sob_t * sub)
{
return sob_find_cstr(s, sub->str, sub->length);
}
bool sob_starts_with_cstr(sob_t * s, const char * sub, size_t sub_len)
{
return sob_find(s, sub, sub_len) == 0;
}
bool sob_starts_with(sob_t * s, sob_t * sub)
{
return sob_starts_with_cstr(s, sub->str, sub->length);
}
bool sob_ends_with_cstr(sob_t * s, const char * sub, size_t sub_len)
{
return (sob_find(s, sub, sub_len) + sub_len) == s->length;
}
bool sob_ends_with(sob_t * s, sob_t * sub)
{
return sob_ends_with_cstr(s, sub->str, sub->length);
}
const char * sob_at(sob_t * s, size_t index)
{
return s->str[index];
}
size_t sob_length(sob_t * s)
{
return s->length;
}
bool sob_is_empty(sob_t * s)
{
return s->length > 0 ? false : true;
}
void sob_print(sob_t * s)
{
char arr[s->length];
for (size_t i = 0; i < s->length; ++i)
arr[i] = s->str[i];
arr[s->length] = '\0';
printf("%s\n", arr);
}
#endif // SOB_IMPLEMENTATION