Skip to content

Commit 4b2dec4

Browse files
committed
Add :
_Bool HasSuffix(const char *s, const char *prefix);
1 parent c579344 commit 4b2dec4

2 files changed

Lines changed: 31 additions & 13 deletions

File tree

‎c/String/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
- [ ] func LastIndexFunc(s string, f func(rune) bool) int
1313

1414
- [ ] func TrimSpace(str string) string
15-
- [ ] func HasPrefix(s string, prefix string) bool
16-
- [ ] func HasSuffix(s string, suffix string) bool
15+
- [x] func HasPrefix(s string, prefix string) bool
16+
- [x] func HasSuffix(s string, suffix string) bool
1717
- [ ] func Replace(str string, old string, new string, n int)
1818
- [ ] func Title(s string) string
1919
- [ ] func ToTitle(s string) string

‎c/String/gostring.c‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void testIndexByte()
6666
printf("%d\n", IndexByte("chicken", 'd'));
6767
printf("%d\n", IndexByte("你好", '\xA0'));
6868
}
69+
6970
int Index(const char *s, const char *sep)
7071
{
7172
char *i = strstr(s, sep);
@@ -83,17 +84,31 @@ void testIndex()
8384
printf("%d\n", Index("RUNOOB", "NOOB"));
8485
printf("%d\n", Index("你好", "\xA0"));
8586
}
87+
88+
// Only ASCII supported; bugs still exist on UTF-8
8689
_Bool HasPrefix(const char *s, const char *prefix)
8790
{
88-
if (strstr(s, prefix) - s != 0)
89-
{
90-
return 0;
91-
}
92-
else
93-
{
94-
return 1;
95-
}
91+
return (_Bool)(strstr(s, prefix) == s);
9692
}
93+
void testHasPrefix()
94+
{
95+
printf("%s\n", HasPrefix("abcdefg", "abc") ? "True" : "False");
96+
printf("%s\n", HasPrefix("abcdefg", "ac") ? "True" : "False");
97+
printf("%s\n", HasPrefix("你好", "\xE4") ? "True" : "False");
98+
}
99+
100+
// Only ASCII supported; bugs still exist on UTF-8
101+
_Bool HasSuffix(const char *s, const char *prefix)
102+
{
103+
return (_Bool)(strcmp(s + strlen(s) - strlen(prefix), prefix) == 0);
104+
}
105+
void testHasSuffix()
106+
{
107+
printf("%s\n", HasSuffix("abcdefg", "efg") ? "True" : "False");
108+
printf("%s\n", HasSuffix("abcdefg", "eg") ? "True" : "False");
109+
printf("%s\n", HasSuffix("你好", "\xBD") ? "True" : "False");
110+
}
111+
97112
char *ToUpper(const char *str)
98113
{
99114
char *ret = (char *)malloc(sizeof(char) * (strlen(str) + 1));
@@ -119,14 +134,14 @@ char *ToUpper(const char *str)
119134
return ret;
120135
}
121136
}
122-
123137
void testToUpper()
124138
{
125139
char array[] = "你好Go!\nHello,GO!";
126140
char *tmp = ToUpper(array);
127141
printf("ToUpper(%s)\n%s", array, tmp);
128142
free(tmp);
129143
}
144+
130145
char *ToLower(const char *str)
131146
{
132147
char *ret = (char *)malloc(sizeof(char) * (strlen(str) + 1));
@@ -159,6 +174,7 @@ void testToLower()
159174
printf("ToLower(%s)\n%s", array, tmp);
160175
free(tmp);
161176
}
177+
162178
int Count(const char *s, const char *sep)
163179
{
164180
if (sep[0] == '\0')
@@ -181,8 +197,8 @@ void testCount()
181197
printf("Count(\"%s\",\"%s\")=%d\n", s1, s2, Count(s1, s2));
182198
printf("Count(\"%s\",\"%s\")=%d\n", s1, s3, Count(s1, s3));
183199
}
184-
#define Contains(str, substr) ((_Bool)strstr((str), (substr)))
185200

201+
#define Contains(str, substr) ((_Bool)strstr((str), (substr)))
186202
void testContains()
187203
{
188204
char s1[] = "I love Java Programming!";
@@ -207,8 +223,10 @@ int main()
207223
// testEqualFold();
208224
// testIndexByte();
209225
// testIndex();
226+
testHasPrefix();
227+
testHasSuffix();
210228
// testToUpper();
211229
// testToLower();
212230
// testCount();
213-
testContains();
231+
// testContains();
214232
}

0 commit comments

Comments
 (0)