@@ -66,6 +66,7 @@ void testIndexByte()
6666 printf ("%d\n" , IndexByte ("chicken" , 'd' ));
6767 printf ("%d\n" , IndexByte ("你好" , '\xA0' ));
6868}
69+
6970int 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+
97112char * 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-
123137void 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+
130145char * 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+
162178int 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)))
186202void 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