-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
executable file
·261 lines (217 loc) · 9.72 KB
/
test.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
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
#include "strs.h"
#include <stdio.h>
static void test();
int main()
{
test();
return 0;
}
void test()
{
println("strs lib test");
println("[%s] is space: %s", " ", strs_is_space(' ') ? "TRUR" : "FALSE");
println("[%s] is space: %s", "\\t", strs_is_space('\t') ? "TRUR" : "FALSE");
println("[%s] is space: %s", "\\n", strs_is_space('\n') ? "TRUR" : "FALSE");
println("[%s] is space: %s", "\\r", strs_is_space('\r') ? "TRUR" : "FALSE");
println("[%s] is space: %s", "c", strs_is_space('c') ? "TRUR" : "FALSE");
println("");
for (char i = '0'; i < '9'; i++) {
println("[%c] is a number: %s", i, strs_is_number(i) ? "TRUE" : "FALSE");
}
println("[%c] is a number: %s", 'c', strs_is_number('c') ? "TRUE" : "FALSE");
println("");
for (char i = 'a'; i <= 'z'; i++) {
println("[%c] is letter: %s", i, strs_is_letter(i) ? "TRUE" : "FALSE");
}
for (char i = 'A'; i <= 'Z'; i++) {
println("[%c] is letter: %s", i, strs_is_letter(i) ? "TRUE" : "FALSE");
}
println("[%c] is letter: %s", '9', strs_is_letter('9') ? "TRUE" : "FALSE");
println("");
for (char i = 'a'; i <= 'z'; i++) {
println("[%c] is a low letter: %s", i, strs_is_low_letter(i) ? "TRUE" : "FALSE");
}
for (char i = 'A'; i <= 'Z'; i++) {
println("[%c] is a up letter: %s", i, strs_is_up_letter(i) ? "TRUE" : "FALSE");
}
println("[%c] is a up letter: %s", 'a', strs_is_up_letter('a') ? "TRUE" : "FALSE");
println("[%c] is a low letter: %s", 'A', strs_is_low_letter('A') ? "TRUE" : "FALSE");
println("");
char str1[] = "hello world!";
println("[%s] size (contain \\0) is: %d", str1, strslen(str1));
println("");
char *str2 = strs_dup(str1);
println("[%s] reverse is: [%s]", str2, strs_reverse(str1));
strs_reverse(str1);
strs_free(str2);
println("");
println("[%s] sub str number is: %d", str1, strs_num(str1));
println("");
str2 = strs_dup(str1);
println("[%s] dup is: [%s]", str1, str2);
println("[%s] to upper is: [%s]", str1, strs_to_upper(str2));
println("[%s] to lower is: [%s]", str1, strs_to_lower(str2));
strs_free(str2);
println("");
println("[%s] contain char %c: %s", str1, 'r', strs_contain_char(str1, 'r') ? "TRUE" : "FALSE");
println("[%s] contain char %c: %s", str1, 'R', strs_contain_char(str1, 'R') ? "TRUE" : "FALSE");
println("[%s] contain char %c: %s", str1, 'x', strs_contain_char(str1, 'x') ? "TRUE" : "FALSE");
println("[%s] contain char %c: %s", str1, 'X', strs_contain_char(str1, 'X') ? "TRUE" : "FALSE");
println("");
println("[%s] constain sub str [%s]: %s", str1, "orl", strs_contains(str1, "orl") ? "TRUE" : "FALSE");
println("[%s] constain sub str [%s]: %s", str1, "ORL", strs_contains(str1, "ORL") ? "TRUE" : "FALSE");
println("[%s] constain sub str [%s]: %s", str1, "wow", strs_contains(str1, "wow") ? "TRUE" : "FALSE");
println("");
println("[%s] == [%s]: %s", "hello", "hello1", strs_compare("hello", "hello\r") ? "TRUE" : "FALSE");
println("");
println("[%s] contains any char from sub str [%s]: %s", str1, "it works",
strs_contains_any(str1, "it works") ? "TRUE" : "FALSE");
println("");
str2 = strs_dup(str1);
strs_to_upper(str2);
println("[%s] == [%s] (ignore upper or lower letter): %s", str1, str2,
strs_equal_case(str1, str2) ? "TRUE" : "FALSE");
strs_free(str2);
println("");
println("[%s] has sub str [%s]: %d", str1, "l", strs_count(str1, "l"));
println("[%s] has sub str [%s]: %d", "Today is a funny day", "day", strs_count("Today is a funny day", "day"));
println("");
str2 = "Hello world! \nToday is a funny day.";
int listSize;
char **strlist = strs_fields(str2, &listSize);
for (int i = 0; i < listSize; i++) {
println("splite %d: [%s]", i, strlist[i]);
}
strs_free2(strlist, listSize);
println("");
println("[%s] has prefix [%s]: %s", str1, "hel", strs_has_prefix(str1, "hel") ? "TRUE" : "FALSE");
println("[%s] has prefix [%s]: %s", str1, "ello", strs_has_prefix(str1, "ello") ? "TRUE" : "FALSE");
println("");
println("[%s] has suffix [%s]: %s", str1, "rld!", strs_has_suffix(str1, "rld!") ? "TRUE" : "FALSE");
println("[%s] has suffix [%s]: %s", str1, "rld", strs_has_suffix(str1, "rld") ? "TRUE" : "FALSE");
println("");
println("[%s] in the [%s]: %d", "wor", str1, strs_index(str1, "wor"));
println("[%s] in the [%s]: %d", "ddd", str1, strs_index(str1, "ddd"));
println("");
str2 = strs_join("hello", "world", " ");
println("[%s] + [%s] = [%s]", "hello", "world", str2);
strs_free(str2);
str2 = strs_join("hello", "world", "");
println("[%s] + [%s] = [%s]", "hello", "world", str2);
strs_free(str2);
println("");
str2 = strs_join_n(" ", "Today", "is", "a", "funny", "day", "!", NULL);
println("[%s] + [%s] + [%s] + [%s] + [%s] + [%s] = [%s]", "To", "is", "a", "funny", "day", "!", str2);
strs_free(str2);
println("");
str2 = "Today is a funny day!";
println("[%s] in the [%s] last pos: %d", "day", str2, strs_last_index(str2, "day"));
println("[%s] in the [%s] last pos: %d", "is", str2, strs_last_index(str2, "is"));
println("[%s] in the [%s] last pos: %d", "ddd", str2, strs_last_index(str2, "ddd"));
println("");
println("[%s] [%s] index any: %d", "chicken", "aeiouy", strs_index_any("chicken", "aeiouy"));
println("[%s] [%s] index any: %d", "crwth", "aeiouy", strs_index_any("crwth", "aeiouy"));
println("");
println("%c in the [%s] pos: %d", 'o', str1, strs_index_char(str1, 'o'));
println("%c in the [%s] pos: %d", 'x', str1, strs_index_char(str1, 'x'));
println("");
println("%c in the [%s] pos: %d", 'o', str1, strs_last_index_char(str1, 'o'));
println("%c in the [%s] pos: %d", 'x', str1, strs_last_index_char(str1, 'x'));
println("");
str2 = strs_repeat("good ", 3);
println("[%s] repeat %d: [%s]", "good ", 3, str2);
strs_free(str2);
println("");
str2 = strs_replace(str1, "l", "xx", 2);
println("[%s] replace [%s] to [%s] (count %d): %s", str1, "l", "xx", 2, str2);
strs_free(str2);
str2 = strs_replace(str1, "l", "xx", 0);
println("[%s] replace [%s] to [%s] (count %d): %s", str1, "l", "xx", 0, str2);
strs_free(str2);
str2 = strs_replace(str1, "l", "xx", -1);
println("[%s] replace [%s] to [%s] (count %d): %s", str1, "l", "xx", -1, str2);
strs_free(str2);
println("");
str2 = strs_replace_all(str1, "l", "xx");
println("[%s] replace all [%s] to [%s]: %s", str1, "l", "xx", str2);
strs_free(str2);
println("");
str2 = strs_dup(str1);
strs_remove(str2, "l");
println("[%s] remove [%s]: [%s]", str1, "l", str2);
strs_free(str2);
println("");
int num;
strlist = strs_split(str1, "o", &num);
for (int i = 0; i < num; i++) {
println("split[%d]: [%s]", i, strlist[i]);
}
strs_free2(strlist, num);
println("");
num = 2;
strlist = strs_split_n(str1, "o", &num);
for (int i = 0; i < num; i++) {
println("split[%d]: [%s]", i, strlist[i]);
}
strs_free2(strlist, num);
num = 4;
strlist = strs_split_n(str1, "lo", &num);
for (int i = 0; i < num; i++) {
println("split[%d]: [%s]", i, strlist[i]);
}
println("num: %d", num);
strs_free2(strlist, num);
println("");
strlist = strs_split_after(str1, "o", &num);
for (int i = 0; i < num; i++) {
println("split[%d]: [%s]", i, strlist[i]);
}
strs_free2(strlist, num);
println("");
num = 2;
strlist = strs_split_after_n(str1, "o", &num);
for (int i = 0; i < num; i++) {
println("split[%d]: [%s]", i, strlist[i]);
}
strs_free2(strlist, num);
println("");
str2 = strs_dup(str1);
println("[%s] title: [%s]", str1, strs_title(str2));
strs_free(str2);
println("");
str2 = strs_dup("xoxo Hello, Gophers!!!oxox");
println("[%s] trim [%s]: [%s]", "xoxo Hello, Gophers!!!oxox", "xo", strs_trim(str2, "xo"));
strs_free(str2);
println("");
str2 = strs_dup("xoxo Hello, Gophers!!!oxox");
println("[%s] trim left [%s]: [%s]", "xoxo Hello, Gophers!!!oxox", "xo", strs_trim_left(str2, "xo"));
strs_free(str2);
println("");
str2 = strs_dup("xoxo Hello, Gophers!!!oxox");
println("[%s] trim right [%s]: [%s]", "xoxo Hello, Gophers!!!oxox", "xo", strs_trim_right(str2, "xo"));
strs_free(str2);
println("");
str2 = strs_dup("xoxo Hello, Gophers!!!oxox");
println("[%s] trim prefix [%s]: [%s]", "xoxo Hello, Gophers!!!oxox", "xo", strs_trim_prefix(str2, "xo"));
strs_free(str2);
str2 = strs_dup("xoxo Hello, Gophers!!!oxox");
println("[%s] trim prefix [%s]: [%s]", "xoxo Hello, Gophers!!!oxox", "xo H", strs_trim_prefix(str2, "xo H"));
strs_free(str2);
println("");
str2 = strs_dup("xoxo Hello, Gophers!!!oxox");
println("[%s] trim suffix [%s]: [%s]", "xoxo Hello, Gophers!!!oxox", "ox", strs_trim_suffix(str2, "ox"));
strs_free(str2);
str2 = strs_dup("xoxo Hello, Gophers!!!oxox");
println("[%s] trim suffix [%s]: [%s]", "xoxo Hello, Gophers!!!oxox", "xo", strs_trim_suffix(str2, "xo"));
strs_free(str2);
println("");
str2 = "\t hello world!\n\r ";
char *str3 = strs_dup(str2);
println("[%s] trim space: [%s]", str2, strs_trim_space(str3));
strs_free(str3);
println("");
str2 = strs_insert(str1, "my ", 6);
println("[%s] insert [%s] on pos %d: [%s]", "my ", str1, 6, str2);
strs_free(str2);
println("\ntest complete!!!!!!\n");
}