forked from hishamhm/dit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.c
218 lines (193 loc) · 4.77 KB
/
String.c
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
#define _GNU_SOURCE
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "Prototypes.h"
int String_forwardWord(char* s, int len, int cursor) {
char curr = s[cursor];
if (isalnum(curr)) {
while (isalnum(curr) && cursor < len)
curr = s[++cursor];
} else if (!isalnum(curr) && !isblank(curr)) {
while (!isalnum(curr) && !isblank(curr) && cursor < len)
curr = s[++cursor];
} else {
while (isblank(curr) && cursor < len)
curr = s[++cursor];
}
return cursor;
}
int String_backwardWord(char* s, int len, int cursor) {
char curr = s[cursor - 1];
if (isalnum(curr)) {
while (isalnum(curr) && cursor > 0)
curr = s[--cursor - 1];
} else if (!isalnum(curr) && !isblank(curr)) {
while (!isalnum(curr) && !isblank(curr) && cursor > 0)
curr = s[--cursor - 1];
} else {
while (isblank(curr) && cursor > 0)
curr = s[--cursor - 1];
}
return cursor;
}
inline void String_delete(char* s) {
assert(s);
free(s);
}
inline char* String_copy(char* orig) {
assert(orig);
return strdup(orig);
}
char* String_cat(char* s1, char* s2) {
int l1 = strlen(s1);
int l2 = strlen(s2);
char* out = malloc(l1 + l2 + 1);
strncpy(out, s1, l1);
strncpy(out+l1, s2, l2+1);
return out;
}
char* String_trim(char* in) {
while (in[0] == ' ' || in[0] == '\t' || in[0] == '\n') {
in++;
}
int len = strlen(in);
while (len > 0 && (in[len-1] == ' ' || in[len-1] == '\t' || in[len-1] == '\n')) {
len--;
}
char* out = malloc(len+1);
strncpy(out, in, len);
out[len] = '\0';
return out;
}
char* String_copyUpTo(char* orig, char upTo) {
int len;
int origLen = strlen(orig);
char* at = index(orig, upTo);
if (at != NULL)
len = at - orig;
else
len = origLen;
char* copy = (char*) malloc(len+1);
strncpy(copy, orig, len);
copy[len] = '\0';
return copy;
}
char* String_sub(char* orig, int from, int to) {
char* copy;
int len;
len = strlen(orig);
if (to > len)
to = len;
if (from > len)
to = len;
len = to-from+1;
copy = (char*) malloc(len+1);
strncpy(copy, orig+from, len);
copy[len] = '\0';
return copy;
}
void String_println(char* s) {
printf("%s\n", s);
}
void String_print(char* s) {
printf("%s", s);
}
void String_printInt(int i) {
printf("%i", i);
}
void String_printPointer(void* p) {
printf("%p", p);
}
inline int String_eq(char* s1, char* s2) {
if (s1 == NULL || s2 == NULL) {
if (s1 == NULL && s2 == NULL)
return 1;
else
return 0;
}
return (strcmp(s1, s2) == 0);
}
inline int String_startsWith(const char* s, const char* match) {
return (strstr(s, match) == s);
}
inline int String_endsWith(const char* s, const char* match) {
int slen = strlen(s);
int matchlen = strlen(match);
if (matchlen > slen) return 0;
return String_startsWith(s+(slen-matchlen), match);
}
char** String_split(char* s, char sep) {
const int rate = 10;
char** out = (char**) malloc(sizeof(char*) * rate);
int ctr = 0;
int blocks = rate;
char* where;
while (*s == sep) s++;
while ((where = strchr(s, sep)) != NULL) {
int size = where - s;
char* token = (char*) malloc(size + 1);
strncpy(token, s, size);
token[size] = '\0';
out[ctr] = token;
ctr++;
if (ctr == blocks) {
blocks += rate;
out = (char**) realloc(out, sizeof(char*) * blocks);
}
while (*(where+1) == sep) where++;
s = where + 1;
}
if (s[0] != '\0') {
int size = strlen(s);
char* token = (char*) malloc(size + 1);
strncpy(token, s, size + 1);
out[ctr] = token;
ctr++;
}
out = realloc(out, sizeof(char*) * (ctr + 1));
out[ctr] = NULL;
return out;
}
void String_freeArray(char** s) {
for (int i = 0; s[i] != NULL; i++) {
free(s[i]);
}
free(s);
}
int String_startsWith_i(char* s, char* match) {
return (strncasecmp(s, match, strlen(match)) == 0);
}
/* deprecated */
int String_contains_i(char* s, char* match) {
int lens = strlen(s);
int lenmatch = strlen(match);
for (int i = 0; i < lens-lenmatch; i++) {
if (strncasecmp(s, match, lenmatch) == 0)
return 1;
s++;
}
return 0;
}
/* deprecated */
int String_indexOf_i(char* s, char* match, int lens) {
int lenmatch = strlen(match);
for (int i = 0; i < lens-lenmatch+1; i++) {
if (strncasecmp(s, match, lenmatch) == 0)
return i;
s++;
}
return -1;
}
/* deprecated */
int String_indexOf(char* s, char* match, int lens) {
int lenmatch = strlen(match);
for (int i = 0; i < lens-lenmatch+1; i++) {
if (strncmp(s, match, lenmatch) == 0)
return i;
s++;
}
return -1;
}