Code formatting changes and update if condition for empty delimiter
@@ -64,8 +64,7 @@ static Value formatString(VM *vm, int argCount, Value *args) {
memcpy(tmp, string->chars, stringLen);
int count = 0;
- while((tmp = strstr(tmp, "{}")))
- {
+ while ((tmp = strstr(tmp, "{}"))) {
count++;
tmp++;
}
@@ -135,20 +134,17 @@ static Value splitString(VM *vm, int argCount, Value *args) {
ObjList *list = initList(vm);
push(vm, OBJ_VAL(list));
- if(!strlen(delimiter))
- {
- for(int tokencount=0;tokencount<string->length;tokencount++)
- {
- *(tmp)=string->chars[tokencount];
+ if (delimiterLength == 0) {
+ for (int tokenCount = 0; tokenCount < string->length; tokenCount++) {
+ *(tmp) = string->chars[tokenCount];
*(tmp + 1) = '\0';
- Value str = OBJ_VAL(copyString(vm, tmp, strlen(tmp)));
+ Value str = OBJ_VAL(copyString(vm, tmp, 1));
// Push to stack to avoid GC
push(vm, str);
writeValueArray(vm, &list->values, str);
pop(vm);
}
- }
-else{
+ } else {
do {
token = strstr(tmp, delimiter);
if (token)
@@ -450,8 +446,7 @@ static Value countString(VM *vm, int argCount, Value *args) {
char *needle = AS_CSTRING(args[1]);
int count = 0;
- while((haystack = strstr(haystack, needle)))
- {
+ while ((haystack = strstr(haystack, needle))) {
count++;
haystack++;
}
