-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstring_functions.h
More file actions
358 lines (333 loc) · 8.83 KB
/
cstring_functions.h
File metadata and controls
358 lines (333 loc) · 8.83 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* String Functions in C.
* Copyright (C) 2017 Ulaganathan Natarajan
* <ulaganathan.n@hotmail.com>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#ifndef STRINGFUNCTIONS_H
#define STRINGFUNCTIONS_H
//Prototypes
char** split_string(char*, const char*);
char** find_all_string(char*, char*, char*);
char* find_string(char*, char*, char*);
char* replace_all(char*, char*, char*);
char* replace(char*, char*, char*);
char* sub_string(char, int, int);
bool contains(char*, char*);
bool contains_ignore_case(char*, char*);
bool equals(char*, char*);
bool equals_ignore_case(char*, char*);
bool starts_with(char*, char*);
bool ends_with(char*, char*);
char* to_lower_case(char*);
char* to_upper_case(char*);
char* trim(char*);
int index_of(char*, char*);
int index_of_from(char*, char*, int);
char* get_random_string(const int, const int);
int commonArrayLen = 0;
//String split_string
char** split_string(char *source, const char* delimiter) {
char **outStr = NULL, *token;
int arrayLen = 0;
source = strdup(source);
if(source == NULL || delimiter == NULL)
return NULL;
token = strtok(source, delimiter);
while (token != NULL) {
outStr = realloc(outStr, (arrayLen + 1) * sizeof(char*));
if(outStr == NULL)
return NULL;
*(outStr + arrayLen) = malloc((strlen(token) + 1) * sizeof(char));
*(outStr + arrayLen) = strdup(token);
arrayLen++;
token = strtok(NULL, delimiter);
}
commonArrayLen = arrayLen;
return outStr;
}
//Find All Strings
char **find_all_string(char *source, char *startString, char *endString) {
char **outStr = NULL, *startPos, *endPos, *findValue;
int arrayLen = 0;
int totalLen = 0, endLen = 0, startLen = 0;
if(source == NULL || startString == NULL || endString == NULL)
return NULL;
if(strlen(startString) == 0 || strlen(endString) == 0)
return NULL;
if(strstr(source, startString) == NULL || strstr(source, endString) == NULL)
return NULL;
while(true) {
startPos = strstr(source + startLen, startString);
if(startPos == NULL)
break;
startLen = (startPos - source) + strlen(startString);
endPos = strstr(source + startLen, endString);
if(endPos == NULL)
break;
endLen = endPos - source;
totalLen = endLen - startLen;
outStr = realloc(outStr, (arrayLen + 1) * sizeof(char*));
if(outStr == NULL)
return NULL;
findValue = malloc((totalLen + 1) * sizeof(char));
strncpy(findValue, source + startLen, totalLen);
findValue[totalLen] = '\0';
*(outStr + arrayLen) = malloc((totalLen + 1) * sizeof(char));
*(outStr + arrayLen) = strdup(findValue);
arrayLen++;
}
commonArrayLen = arrayLen;
return outStr;
}
//Find String
char *find_string(char *source, char *startString, char *endString) {
char *outStr;
int startLen = 0, endLen = 0, totalLen = 0;
if(source == NULL)
return NULL;
if(startString == NULL){
startLen = 0;
}else{
if(strlen(startString)==0)
startLen = 0;
else
startLen = (strstr(source, startString) - source) + strlen(startString);
}
if(endString == NULL){
endLen = strlen(source);
}else{
if(strlen(endString)==0){
endLen = strlen(source);
}else{
if(startLen > 0)
endLen = strstr(source+startLen, endString) - source;
else
endLen = strstr(source, endString) - source;
}
}
if(startLen < 0 || endLen < 0 )
return NULL;
totalLen = endLen - startLen;
outStr = malloc((totalLen + 1) * sizeof(char));
strncpy(outStr, source + startLen, totalLen);
outStr[totalLen] = '\0';
return outStr;
}
//replace_all
char *replace_all(char *source, char *orig, char *rep) {
char *outStr, *p;
int pos = 0, replen = 0, origlen = 0;
if(source == NULL || orig == NULL || rep == NULL)
return source;
p = strstr(source, orig);
if(p == NULL)
return source;
replen = strlen(rep);
origlen = strlen(orig);
pos = (p - source) + replen;
outStr = malloc(1 * sizeof(char));
while(p != NULL) {
outStr = realloc(outStr, (strlen(source) + replen + 1));
*outStr = '\0';
strcat(outStr, source);
sprintf(outStr + (p - source), "%s%s", rep, p + origlen);
source = strdup(outStr);
p = strstr(source + pos, orig);
pos = (p - source) + replen;
}
free(outStr);
return source;
}
// Replace
char *replace(char* source, char* orig, char* rep) {
char *outStr, *p;
if(source == NULL || orig == NULL || rep == NULL)
return source;
p = strstr(source, orig);
if(p == NULL)
return source;
outStr = malloc(strlen(source) + strlen(rep) + 1 * sizeof(char));
*outStr = '\0';
strcat(outStr, source);
sprintf(outStr + (p - source), "%s%s", rep, p + strlen(orig));
return outStr;
}
//Sub String
char *sub_string(char *source, int start_len, int end_len){
char *out_str;
if(source == NULL)
return NULL;
if(start_len>end_len)
end_len = strlen(source);
int total_len=end_len - start_len;
out_str = malloc((total_len + 1) * sizeof(char));
strncpy(out_str, source + start_len, total_len);
}
//Contains
bool contains(char *source, char *find) {
char *exist;
if(source == NULL || find == NULL)
return false;
exist = strstr(source, find);
if(exist != NULL) {
return true;
}
return false;
}
//Contains Ignore Case
bool contains_ignore_case(char *source, char *find) {
char *exist;
if(source == NULL || find == NULL)
return false;
source = to_lower_case(source);
find = to_lower_case(find);
exist = strstr(source, find);
if(exist != NULL) {
return true;
}
return false;
}
//Equals
bool equals(char *source, char *find) {
if(source == NULL || find == NULL)
return false;
if(strcmp(source, find) == 0) {
return true;
}
return false;
}
//Equls Ignore Case
bool equals_ignore_case(char *source, char *find) {
if(source == NULL || find == NULL)
return false;
source = to_lower_case(source);
find = to_lower_case(find);
if(strcmp(source, find) == 0) {
return true;
}
return false;
}
//Starts With
bool starts_with(char *source, char* find) {
if(source == NULL || find == NULL)
return false;
if((strstr(source, find) - source) == 0) {
return true;
}
return false;
}
//Ends With
bool ends_with(char *source, char *find) {
int sourcelen, findlen;
if(source == NULL || find == NULL)
return false;
sourcelen = strlen(source);
findlen = strlen(find);
if((sourcelen >= findlen) && (0 == strcmp(source + sourcelen - findlen, find))) {
return true;
}
return false;
}
// To Lower Case
char* to_lower_case(char *source) {
char *outStr;
int i, length = 0;
if(source == NULL)
return NULL;
outStr = strdup(source);
length = strlen(source);
for (i = 0; i < length; i++)
outStr[i] = (char) tolower(source[i]);
return outStr;
}
//To Upper Case
char* to_upper_case(char *source) {
char *outStr;
int i, length = 0;
if(source == NULL)
return NULL;
outStr = strdup(source);
length = strlen(source);
for(i = 0; i < length; i++)
outStr[i] = (char) toupper(source[i]);
return outStr;
}
//Trim
char* trim(char *source) {
char *outStr = malloc((strlen(source) + 1) * sizeof(char));
int sourceLen = strlen(source), startLen = 0, endLen = 0, totalLen = 0, x;
if(source == NULL)
return NULL;
for(x = 0; x < sourceLen; x++, startLen++) {
if(!isspace(source[x]))
break;
}
for(x = sourceLen - 1; x >= 0; x--, endLen++) {
if(!isspace(source[x]))
break;
}
totalLen = sourceLen - (startLen + endLen);
if(totalLen < 1)
return NULL;
strncpy(outStr, source + startLen, (size_t) totalLen);
outStr[totalLen] = '\0';
return outStr;
}
//get index of string
int index_of(char *source, char *find){
if(source == NULL)
return NULL;
char *p = strstr(source, find);
if (p != NULL)
return p - source;
return -1;
}
//get index of string
int index_of(char *source, char *find, int start){
if(source == NULL)
return NULL;
char *p = strstr(source + start, find);
if (p != NULL)
return p - source;
return -1;
}
char *get_random_string(const int size, const int randomType) {
char *outStr=malloc(size*sizeof(char)+1), alphaNumerics[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
int x, sIndex = 0, eIndex = 0;
if(randomType == 0 || randomType > 2){
sIndex = 0;
eIndex = 35;
}else if(randomType == 1){
sIndex = 0;
eIndex = 25;
}else if(randomType == 2){
sIndex = 26;
eIndex = 35;
}
for(x=0; x<size; x++){
outStr[x] = alphaNumerics[rand() % (eIndex - sIndex + 1) + sIndex];
}
outStr[size] = '\0';
return outStr;
}
#endif /* STRINGFUNCTIONS_H */