-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgostring.c
More file actions
228 lines (218 loc) · 5.33 KB
/
gostring.c
File metadata and controls
228 lines (218 loc) · 5.33 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Only ASCII supported; bugs still exist on UTF-8
_Bool EqualFold(const char *s1, const char *s2)
{
int i = strlen(s1);
if (i != strlen(s2))
{
return 0;
}
else
{
while (i + 1)
{
if (s1[i] != s2[i])
{
if ((s1[i] >= 'A' && s1[i] <= 'Z' && s2[i] >= 'a' &&
s2[i] <= 'z' && s1[i] - s2[i] == 'A' - 'a'))
{
i--;
}
else if ((s2[i] >= 'A' && s2[i] <= 'Z' && s1[i] >= 'a' &&
s1[i] <= 'z' && s2[i] - s1[i] == 'A' - 'a'))
{
i--;
}
else
{
return 0;
}
}
else
{
i--;
}
}
}
return 1;
}
void testEqualFold()
{
printf("%s", EqualFold("AbcDefGhIjKl", "aBcdEfGhiJkl") ? "True" : "False");
}
int IndexByte(const char *s, char c)
{
int i = 0;
while (s[i] != c)
{
if (s[i] == '\0')
{
return -1;
}
else
{
i++;
}
}
return i;
}
void testIndexByte()
{
printf("%d\n", IndexByte("chicken", 'k'));
printf("%d\n", IndexByte("chicken", 'd'));
printf("%d\n", IndexByte("你好", '\xA0'));
}
int Index(const char *s, const char *sep)
{
char *i = strstr(s, sep);
if (i == NULL)
{
return -1;
}
else
{
return i - s;
}
}
void testIndex()
{
printf("%d\n", Index("RUNOOB", "NOOB"));
printf("%d\n", Index("你好", "\xA0"));
}
// Only ASCII supported; bugs still exist on UTF-8
#define HasPrefix(s, prefix) \
((_Bool)(strncmp((s), (prefix), strlen((prefix))) == 0))
void testHasPrefix()
{
printf("%s\n", HasPrefix("abcdefg", "abc") ? "True" : "False");
printf("%s\n", HasPrefix("abcdefg", "ac") ? "True" : "False");
printf("%s\n", HasPrefix("你好", "\xE4") ? "True" : "False");
}
// Only ASCII supported; bugs still exist on UTF-8
#define HasSuffix(s, prefix) \
((_Bool)(strcmp((s) + strlen((s)) - strlen((prefix)), (prefix)) == 0))
void testHasSuffix()
{
printf("%s\n", HasSuffix("abcdefg", "efg") ? "True" : "False");
printf("%s\n", HasSuffix("abcdefg", "eg") ? "True" : "False");
printf("%s\n", HasSuffix("你好", "\xBD") ? "True" : "False");
}
char *ToUpper(const char *str)
{
char *ret = (char *)malloc(sizeof(char) * (strlen(str) + 1));
int i;
if (ret == NULL)
{
return NULL;
}
else
{
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
ret[i] = str[i] - 'a' + 'A';
}
else
{
ret[i] = str[i];
}
}
ret[i] = '\0';
return ret;
}
}
void testToUpper()
{
char array[] = "你好Go!\nHello,GO!";
char *tmp = ToUpper(array);
printf("ToUpper(%s)\n%s", array, tmp);
free(tmp);
}
char *ToLower(const char *str)
{
char *ret = (char *)malloc(sizeof(char) * (strlen(str) + 1));
int i;
if (ret == NULL)
{
return NULL;
}
else
{
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
ret[i] = str[i] - 'A' + 'a';
}
else
{
ret[i] = str[i];
}
}
ret[i] = '\0';
return ret;
}
}
void testToLower()
{
char array[] = "你好Go!\nHello,GO!";
char *tmp = ToLower(array);
printf("ToLower(%s)\n%s", array, tmp);
free(tmp);
}
int Count(const char *s, const char *sep)
{
if (sep[0] == '\0')
{
return strlen(s) + 1;
}
int count = 0;
while ((s = (strstr(s, sep))) != NULL)
{
s++;
count++;
}
return count;
}
void testCount()
{
char *s1 = "1145141919810";
char *s2 = "2";
char *s3 = "";
printf("Count(\"%s\",\"%s\")=%d\n", s1, s2, Count(s1, s2));
printf("Count(\"%s\",\"%s\")=%d\n", s1, s3, Count(s1, s3));
}
#define Contains(str, substr) ((_Bool)strstr((str), (substr)))
void testContains()
{
char s1[] = "I love Java Programming!";
char s2[] = "Programming";
char s3[] = "网络上学习Java";
char s4[] = "I love Java Programming!";
char s5[] = "I love Java programming!";
char s6[] = "Java";
char s7[] = "Java programming!";
printf("Contains(\"%s\",\"%s\")=%d\n", s1, s2, Contains(s1, s2));
printf("Contains(\"%s\",\"%s\")=%d\n", s1, s3, Contains(s1, s3));
printf("Contains(\"%s\",\"%s\")=%d\n", s1, s4, Contains(s1, s4));
printf("Contains(\"%s\",\"%s\")=%d\n", s1, s5, Contains(s1, s5));
printf("Contains(\"%s\",\"%s\")=%d\n", s1, s6, Contains(s1, s6));
printf("Contains(\"%s\",\"%s\")=%d\n", s1, s7, Contains(s1, s7));
printf("Contains(\"%s\",\"%s\")=%d\n", "", "", Contains("", ""));
printf("Contains(\"%s\",\"%s\")=%d\n", "1", "", Contains("1", ""));
}
int main()
{
// testEqualFold();
// testIndexByte();
// testIndex();
// testHasPrefix();
// testHasSuffix();
// testToUpper();
// testToLower();
// testCount();
// testContains();
}