-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincrstring.h
More file actions
328 lines (263 loc) · 8.56 KB
/
incrstring.h
File metadata and controls
328 lines (263 loc) · 8.56 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
#ifndef INCRSTRING_H
#define INCRSTRING_H
#include <stdlib.h>
#include <string.h>
/* Returns 1 if the string is empty. */
#define strempty(str) (strlen(str)==0)
/* Returns 1 if the string ends with 'ch'. */
#define strew(str,ch) str[strlen(str)-1]==ch
/* Returns 1 if the string starts with 'ch'. */
#define strsw(str,ch) str[0]==ch
/* Returns the position of the first occurrence of a string. */
#define strfpos(str,substr) strposlr(str,substr,0)
/* Returns the position of the last occurrence of a string. */
#define strlpos(str,substr) strposrl(str,substr,strlen(str)-1)
/* Inserts a substring to the left of the string. */
#define strinsl(str,substr) strins(str,0,substr)
/* Inserts a substring to the right of the string. */
#define strinsr(str,substr) strcat(str,substr)
/* Sets 0 in all index. */
#define strrst(str) memset(str,0,strlen(str))
/* Converts uppercase to lowercase. */
#define strtolwr(str) for(size_t ___i = strlen(str);___i--;)if (str[___i]>='A' && str[___i]<='Z')str[___i]+=32
/* Converts lowercase to uppercase and uppercase to lowercase respectively. */
#define strtoopp(str) for(size_t ___i = strlen(str);___i--;)if (str[___i]>='A' && str[___i]<='Z')str[___i]+=32;else if (str[___i]>='a' && str[___i]<='z')str[___i]-=32
/* Converts lowercase to uppercase. */
#define strtoupr(str) for(size_t ___i = strlen(str);___i--;)if (str[___i]>='a' && str[___i]<='z')str[___i]-=32
/* Removes spaces that are to the right and left of the string. */
#define strtrim(str) strtriml(str);strtrimr(str);
/* Removes spaces that are to the left of the string. */
#define strtriml(str) while(str[0]==' ')strrem(str,0)
/* Removes spaces that are to the right of the string. */
#define strtrimr(str) for(size_t ___i = strlen(str)-1;str[___i]==' ';)strrem(str,___i--)
/* Reverses the position of the characters in the string. */
#define strrvrs(str) ({ \
size_t ___len = strlen(str); \
char ___temp;\
for (size_t ___i = 0; ___i < ___len / 2; ++___i) { \
___temp = str[___i]; \
str[___i] = str[___len - ___i - 1]; \
str[___len - ___i - 1] = ___temp; \
} \
})
/* Inserts a substring in a specific point. */
static char strins(char* str, long index,const char* substr){
if (str == NULL || substr == NULL) return -1; // Error: NULL input
const size_t str_len = strlen(str);
const size_t substr_len = strlen(substr);
if (index >= str_len) return -2; // Error: Not enough space
// Handle negative indices
if (index < 0) index = str_len + index;
if (index < 0 || (size_t)index > str_len) return -3; // Error: Index out of range
// Move existing characters to make space for the substring
memmove(str + index + substr_len, str + index, str_len - index + 1);
// Insert the substring
memcpy(str + index, substr, substr_len);
return 0; // Success
}
/* Inserts a character in a specific point. */
static void strchins(char* str,long x,const char ch ){ const char *temp = &ch; strins(str,x,temp) ;}
/* Inserts a character to the left of the string. */
static void strchinsl(char* str,const char ch ){ const char *temp = &ch; strinsl(str,temp) ;}
/* Inserts a character to the right of the string. */
static void strchinsr(char* str,const char ch ){ const char *temp = &ch; strcat(str,temp) ;}
/* Returns the first position of a substring analyzing it from left to right, starting from the incident 'start_index'. */
static long strposlr(const char *str,const char *substr, long start_index){
if (!str || !substr || !*str || !*substr) return -1;
size_t str_len = strlen(str);
size_t substr_len = strlen(substr);
if (substr_len > str_len) return -2;
if (start_index >= (long)str_len) return -3;
if (start_index < 0) start_index = str_len + start_index;
if (start_index < 0 || (size_t)start_index >= str_len) return -3;
const char *start = str + start_index;
const char *found = strstr(start, substr);
if (!found) return -1;
return (long)(found - str);
}
/* Returns the first position of a substring analyzing it from right to left, starting from the incident 'i'. */
static long strposrl(const char *str,const char *substr, long i){
const long substr_len = (long)strlen(substr);
const long len = (long)strlen(str);
if ( strempty(str) ) return -1;
if ( strempty(substr) ) return -2;
if ( substr_len > len ) return -2;
if (i >= (long)strlen(str)) return -3;
long i2 = substr_len;
if ( i<0 ) i = len-i;
char iqual = 0;
if (substr_len == 1)
{
while (i && str[i]!=substr[0]) i--;
return ( str[i]!=substr[0] )? -1 : i;
}else{
do{
if (str[i]==substr[i2-1])
{
iqual = 1;
while (i2--){
if (str[i-((substr_len-1)-i2)] != substr[i2]){
iqual = 0;
i2 = substr_len;
break;
}
}
}
}while(i-- >= substr_len && !iqual);
i++;
return (iqual)? i-(substr_len-1) : -1 ;
}
}
/* Returns the number of times a substring appears. */
static size_t strcount(const char *str, const char *word){
long count = 0,i=0;
const long word_len = (long)strlen(word);
if ( strempty(str) || strempty(word)) return 0;
if ( (i = strposlr(str,word,0)) < 0 ) return 0;
if (word_len == 1){
i = (long)strlen(str);
while(i--) if (str[i] == word[0]) count++;
return count;
}
else{
do count++; while( (i = strposlr(str,word,i+word_len) ) > -1 );
return count;
}
}
/* Removes the character from a string that is in a specific index. */
static char strrem(char* str, long i){
const long len = (long)strlen(str);
if (strempty(str)) return -1;
if (i >= len)return -2;
if (i < 0 && i*-1 >= len)return -2;
if (i<0) i = len-i;
for (; i < len; ++i)
str[i] = str[i+1];
str[i]='\0';
return 1;
}
/* Replaces a substring with another substring. */
static void strrep(char *str,const char *a,const char *b){
long i2,i;
const long a_len = i2 = (long)strlen(a);
const long b_len = (long)strlen(b);
if ((i = strfpos(str,a)) < 0 ) return;
if (a_len == 1 && b_len == 1)
{
i++;
while (i--)
if (str[i]==a[0])
str[i]=b[0];
return;
}
else if (a_len == 1 && b_len == 0)
{
i++;
while (i--)
if (str[i]==a[0])
strrem(str,i);
return;
}
else{
do{
while (i2--){
strrem(str,i);
}
strins(str,i,b);
i2 = a_len;
}while( (i = strfpos(str,a)) > -1 );
}
}
/* Replaces the first occurrence of the substring with another substring. */
static void strfrep(char *str,const char *a,const char *b){
//if (str[0] != a[0]) retrn;
long i2,i;
const long a_len = i2 = (long) strlen(a);
const long b_len = (long) strlen(b);
if ((i = strfpos(str,a)) < 0 ) return;
if (a_len == 1 && b_len == 1)
{
str[i]=b[0];
return;
}
else if (a_len == 1 && b_len == 0)
{
strrem(str,i);
return;
}
else{
while (i2--)
strrem(str,i);
strins(str,i,b);
}
}
/* Replaces the last occurrence of the substring with another substring. */
static void strlrep(char *str,const char *a,const char *b){
long i2,i;
const long a_len = i2 = (long)strlen(a);
const long b_len = (long)strlen(b);
if ((i = strlpos(str,a)) < 0 ) return;
if (a_len == 1 && b_len == 1)
{
str[i]=b[0];
return;
}
else if (a_len == 1 && b_len == 0)
{
strrem(str,i);
return;
}
else{
while (i2--)
strrem(str,i);
strins(str,i,b);
i2 = a_len;
}
}
/* Separates a string based on a separator and save the result in a matrix. And return the number of substrings that resulted from that separation returns. */
static size_t strsplit(const char *str,const char *sep,const size_t w,char ***arr){
long i = 0,x = 0;
long y = 0;
char **aux = (char**)malloc((strcount(str,sep)+1) * sizeof(char*));
if ( strfpos(str,sep) < 0 ){
aux[0] = (char*)calloc((strlen(str)+1), sizeof(char) );
strcpy(aux[0],str);
*arr = aux;
return 1;
}else{
aux[0] = (char*)calloc((w+1), sizeof(char) );
}
long len = strlen(str);
const int sep_len = strlen(sep);
if ( sep_len == 1)
{
for (; i < len; ++i,++x)
{
aux[y][x] = str[i];
if (str[i]==sep[0]){
aux[y][x] = '\0';
++y;
x=-1;
aux[y] = (char*)calloc((w+1), sizeof(char) );
}
}
}else{
long nextSep = strposlr(str,sep,0);
for (; i < len; ++i,++x)
{
aux[y][x] = str[i];
if (i==nextSep){
aux[y][x] = '\0';
++y;
x=-1;
i+=sep_len-1;
nextSep = strposlr(str,sep,i);
aux[y] = (char*)calloc((w+1), sizeof(char) );
}
}
}
aux[y][x] = '\0';
*arr = aux;
return y+1;
}
#endif