fixing bugs with lists and strings

This commit is contained in:
2026-04-16 17:41:37 +10:00
parent e4b5aafe35
commit 538cf4c666
2 changed files with 55 additions and 8 deletions

View File

@@ -64,16 +64,18 @@ GroundValue stringSubstring(GroundScope* scope, List args) {
ERROR("End can't be less than start when getting substring!", "EndBeforeStart");
}
int64_t inputLen = strlen(string);
size_t inputLen = strlen(string);
if (start >= inputLen)
if (start >= inputLen) {
ERROR("Start is outside string!", "OutOfBounds");
else if (end >= inputLen || end < 0)
} else if (end >= inputLen || end < 0) {
ERROR("End is outside string!", "OutOfBounds");
}
char* buffer = malloc(inputLen + 1);
if (!buffer)
if (!buffer) {
ERROR("Failed to allocate memory while getting substring of string!", "AllocFail");
}
// string_Substring("Hello!", 1, 3) -> "ell"
// amount = 3 - 1 + 1 = 3
@@ -106,8 +108,9 @@ GroundValue stringFind(GroundScope* scope, List args) {
char* needle = args.values[1].data.stringVal;
char* result = strstr(haystack, needle);
if (!result)
if (!result) {
return groundCreateValue(INT, (int64_t)-1);
}
return groundCreateValue(INT, result - haystack);
}