-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.c
350 lines (319 loc) · 11.2 KB
/
String.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "String.h"
#include "Object.h"
#include "Print.h"
#include "GenericList.h"
#include "Math.h"
#include "IntList.h"
static String* _StringConstructor(void);
static void _StringPrintObj(const String *obj);
static bool _StringDebug(void);
static bool _StringDebugAdd(void);
static bool _StringDebugSet(void);
static bool _StringDebugRemove(void);
static bool _StringDebugSearch(void);
static bool _StringDebugMisc(void);
static void _StringDestructor(String **obj);
static void String_Print(const String *self);
static void String_SetLength(String *self);
static void String_ToUpperCase(String *self);
static void String_ToLowerCase(String *self);
static void String_TrimWhitespace(String *self);
static void String_SetSize(String *self, const int len);
static void String_Set(String *self, const char *newStr);
static bool String_Equals(const String *self, const char *other);
static void String_Concat(String *self, const char *newStr);
static bool String_Contains(const String *self, const char *token);
static void String_ConcatChar(String *self, const char newChar);
static void String_ConvertFromGenericListToStr(String *self);
static IntList* String_GetIndexesOf(const String *self, const char token);
static int String_GetLastIndexOf(const String *self, const char searchChar);
static int String_GetFirstIndexOf(const String *self, const char searchChar);
static void String_RemoveChars(String *self, const char *unwantedChars);
static int String_GetCharOccurences(const String *self, const char *searchChars);
static void String_TrimSubstring(String *self, const int startIndex, const int endIndex);
static void String_CopyStrBetween(String *self, const String *other, int startIndex, int endIndex);
static const struct StringMethods stringMethods={
.print=&String_Print,
.equals=&String_Equals,
.set=&String_Set,
.concat=&String_Concat,
.concatChar=&String_ConcatChar,
.setSize=&String_SetSize,
.getFirstIndexOf=&String_GetFirstIndexOf,
.getLastIndexOf=&String_GetLastIndexOf,
.copyStrBetween=&String_CopyStrBetween,
.removeChars=&String_RemoveChars,
.trimSubString=&String_TrimSubstring,
.trimWhitespace=&String_TrimWhitespace,
.toUpperCase=&String_ToUpperCase,
.toLowerCase=&String_ToLowerCase,
.contains=&String_Contains,
.getIndexesOf=&String_GetIndexesOf,
.getCharOccurences=&String_GetCharOccurences,
};
const struct StringClass_t StringClass={
.new=&_StringConstructor,
.delete=&_StringDestructor,
.print=&_StringPrintObj,
.debug=&_StringDebug,
.objSize=sizeof(String)
};
//Object Methods================================================================
//Public Methods
static void String_Set(String *self, const char *newStr){
self->genericList->methods->set(self->genericList,newStr,strlen(newStr)+1);
String_ConvertFromGenericListToStr(self);
}
static void String_Concat(String *self, const char *newStr){
GenericList *temp=self->genericList;
if (newStr!=NULL){
temp->methods->removeAt(temp,temp->numElements-1);
temp->methods->add(temp,newStr,strlen(newStr)+1);
String_ConvertFromGenericListToStr(self);
}
}
static void String_ConcatChar(String *self, const char newChar){
GenericList *temp=self->genericList;
temp->methods->addAt(temp,&newChar,1,temp->numElements-1);
String_ConvertFromGenericListToStr(self);
}
static void String_SetSize(String *self, const int len){
char *blank=" ";
char *newLine="\n";
GenericList *temp=self->genericList;
temp->methods->setListSize(self->genericList,len);
for (int i=0; i<temp->numElements-1; i++){
temp->methods->setAt(temp,blank,1,i);
}
temp->methods->setAt(temp,newLine,1,temp->numElements-1);
String_ConvertFromGenericListToStr(self);
}
static int String_GetFirstIndexOf(const String *self, const char searchChar){
GenericList *temp=self->genericList;
return temp->methods->getFirstIndexOf(temp,&searchChar);
}
static int String_GetLastIndexOf(const String *self, const char searchChar){
GenericList *temp=self->genericList;
return temp->methods->getLastIndexOf(temp,&searchChar);
}
static int String_GetCharOccurences(const String *self, const char *searchChars){
//bool _break=false;
//int curIndex=0;
int rv=0;
for (int i=0; i<self->length; i++){
if (strchr(searchChars,self->str[i])!=NULL){
rv++;
}
}
//while (curIndex<self->length-1){
// _break=false;
// for (int i=curIndex+1; i<self->length && !_break; i++){
// curIndex=i;
// if (strchr(searchChars,self->str[i])!=NULL){
// _break=true;
// rv++;
// }
// }
//}
return rv;
}
static void String_CopyStrBetween(String *self, const String *other, const int startIndex, const int endIndex){
GenericList *selfTemp=self->genericList;
GenericList *otherTemp=other->genericList;
selfTemp->methods->copyOtherBetween(selfTemp,otherTemp,startIndex,endIndex);
String_ConvertFromGenericListToStr(self);
}
static void String_RemoveChars(String *self, const char *unwantedChars){
GenericList *temp=self->genericList;
for (int i=0; i<strlen(unwantedChars); i++){
temp->methods->remove(temp,&unwantedChars[i]);
}
String_ConvertFromGenericListToStr(self);
}
static void String_TrimSubstring(String *self, const int startIndex, const int endIndex){
GenericList *temp=self->genericList;
temp->methods->removeBetween(temp,startIndex,endIndex);
String_ConvertFromGenericListToStr(self);
}
static void String_TrimWhitespace(String *self){
bool exit=false;
if (self->str!=NULL){
for (int i=0; i<self->length && !exit; i++){
if (self->str[i]!=' ' && self->str[i]!='\t'){
self->methods->trimSubString(self,0,i);
exit=true;
}
}
exit=false;
for (int i=self->length-1; i>0 && !exit; i--){
if (self->str[i]!=' ' && self->str[i]!='\t'){
self->methods->trimSubString(self,i+1,self->length);
exit=true;
}
}
}
}
static void String_ToUpperCase(String *self){
if (self->str!=NULL){
for(int i=0; i<self->length; i++){
self->str[i]=toupper(self->str[i]);
}
}
}
static void String_ToLowerCase(String *self){
if (self->str!=NULL){
for(int i=0; i<self->length; i++){
self->str[i]=tolower(self->str[i]);
}
}
}
static IntList* String_GetIndexesOf(const String *self, const char token){
IntList *rv=IntListClass.new();
for (int i=0; i<self->length; i++){
if (self->str[i]==token){
rv->methods->add(rv,i);
}
}
return rv;
}
static bool String_Contains(const String *self, const char *token){
return (strstr(self->str,token)!=NULL)? true: false;
}
static bool String_Equals(const String *self, const char *other){
bool rv=(self->str==NULL && other==NULL)? true: false;
if (!rv && self->str!=NULL && other!=NULL && strcmp(self->str,other)==0){
rv=true;
}
return rv;
}
static void String_Print(const String *self){
printf("%s\n",self->str);
}
//Private Methods
static void String_ConvertFromGenericListToStr(String *self){
char *nullTerminator="\0";
GenericList *temp=self->genericList;
bool strExists=(temp->numElements>0)? true: false;
bool hasNullTerm=(temp->methods->getLastIndexOf(temp,nullTerminator)==temp->numElements-1)? true: false;
if (strExists){
if (!hasNullTerm){
temp->methods->add(temp,nullTerminator,1);
}
self->str=(char*)temp->list;
self->length=(self->genericList->numElements>0)? self->genericList->numElements-1: 0;
//String_SetLength(self);
} else {
self->str=NULL;
self->length=0;
}
}
static void String_SetLength(String *self){
self->length=strlen(self->str);
}
// Class Methods================================================================
static String* _StringConstructor(void){
String *rv=(String*)createObject(StringClass.objSize);
rv->genericList=GenericListClass.new();
rv->genericList->methods->setElementSize(rv->genericList,sizeof(char));
rv->str=NULL;
rv->length=0;
rv->methods=&stringMethods;
return rv;
}
static void _StringPrintObj(const String *obj){
PrintClass.print("<String Obj[Addr: %p]: '%s'[%d chars]>\n",obj,obj->str,obj->length);
}
static bool _StringDebug(void){
bool successfull=true;
successfull&=_StringDebugAdd();
successfull&=_StringDebugSet();
successfull&=_StringDebugRemove();
successfull&=_StringDebugSearch();
successfull&=_StringDebugMisc();
PrintClass.objectDebug("String",successfull);
return successfull;
}
static bool _StringDebugAdd(void){
bool successfull=true;
String *test=StringClass.new();
test->methods->concat(test,"Testing");
(test->length!=7)? successfull=false: 0;
(strcmp(test->str,"Testing")!=0)? successfull=false: 0;
test->methods->concatChar(test,'3');
(strcmp(test->str,"Testing3")!=0)? successfull=false: 0;
(test->length!=8)? successfull=false: 0;
//StringClass.print(test);
StringClass.delete(&test);
return successfull;
}
static bool _StringDebugSet(void){
bool successfull=true;
String *test=StringClass.new();
test->methods->concat(test,"Testing");
test->methods->set(test,"TESTING!");
(test->length!=8)? successfull=false: 0;
(strcmp(test->str,"TESTING!")!=0)? successfull=false: 0;
StringClass.delete(&test);
return successfull;
}
static bool _StringDebugRemove(void){
bool successfull=true;
String *test=StringClass.new();
String *test2=StringClass.new();
test2->methods->set(test2,"NewStr");
test->methods->set(test," abcdefghijklmnopqrstuvwxyz ");
test->methods->trimWhitespace(test);
(test->length!=26)? successfull=false: 0;
(strcmp(test->str,"abcdefghijklmnopqrstuvwxyz")!=0)? successfull=false: 0;
test->methods->removeChars(test,"abc");
(test->length!=23)? successfull=false: 0;
(strcmp(test->str,"defghijklmnopqrstuvwxyz")!=0)? successfull=false: 0;
test->methods->trimSubString(test,6,9);
(test->length!=20)? successfull=false: 0;
(strcmp(test->str,"defghimnopqrstuvwxyz")!=0)? successfull=false: 0;
test2->methods->copyStrBetween(test2,test,0,6);
(test2->length!=6)? successfull=false: 0;
(strcmp(test2->str,"defghi")!=0)? successfull=false: 0;
StringClass.delete(&test);
StringClass.delete(&test2);
return successfull;
}
static bool _StringDebugSearch(void){
bool successfull=true;
String *test=StringClass.new();
test->methods->set(test,"abcd,cdef,efgh,ghij,ijkl,klmn,mnop,opqr,qruv,uvwx,wxyz");
// 54 charaters long
(!test->methods->contains(test,"efgh"))? successfull=false: 0;
(test->methods->getFirstIndexOf(test,'k')!=22)? successfull=false: 0;
(test->methods->getLastIndexOf(test,'k')!=25)? successfull=false: 0;
(test->methods->getCharOccurences(test,"cde")!=6)? successfull=false: 0;
(test->methods->equals(test,"abcdefghijklmnopqrstuvwxyz"))? successfull=false: 0;
(!test->methods->equals(test,"abcd,cdef,efgh,ghij,ijkl,klmn,mnop,opqr,qruv,uvwx,wxyz"))? successfull=false: 0;
IntList *indexes=test->methods->getIndexesOf(test,',');
(indexes->length!=10 || indexes->list[0]!=4)? successfull=false: 0;
IntListClass.delete(&indexes);
StringClass.delete(&test);
return successfull;
}
static bool _StringDebugMisc(void){
bool successfull=true;
String *test=StringClass.new();
test->methods->set(test,"Testing This String");
test->methods->toUpperCase(test);
(strcmp(test->str,"TESTING THIS STRING")!=0)? successfull=false: 0;
test->methods->toLowerCase(test);
(strcmp(test->str,"testing this string")!=0)? successfull=false: 0;
test->methods->setSize(test,10);
(test->length!=10)? successfull=false: 0;
StringClass.delete(&test);
return successfull;
}
static void _StringDestructor(String **obj){
GenericListClass.delete(&(*obj)->genericList);
deleteObject((void**)obj);
}