Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add findLast string method #723

Merged
merged 7 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/docs/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ To find the index of a given substring, use the `.find()` method. This method ta
"House".find("Lost Keys"); // -1 (Not found)
```

### string.findLast(String) -> Number

Returns the last index of the given string. If the substring doesn't exist, -1 is returned.

```cs
"woolly woolly mammoth".findLast("woolly"); // 7
"mammoth".findLast("woolly"); // -1
```

### string.leftStrip() -> String

Strips whitespace from the left side of a string and returns the result.
Expand Down
44 changes: 44 additions & 0 deletions src/vm/datatypes/strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,49 @@ static Value findString(DictuVM *vm, int argCount, Value *args) {
return NUMBER_VAL(position);
}

static Value findLastString(DictuVM *vm, int argCount, Value *args) {
if (argCount != 1) {
runtimeError(vm, "findLast() takes 1 argument (%d given)", argCount);
return EMPTY_VAL;
}

const char *str = AS_CSTRING(args[0]);
const char *ss = AS_CSTRING(args[1]);
const char *p = str;
int found = !*ss;

if (!found) {
while (*p) {
++p;
}

const char *q = ss;
while (*q) {
++q;
}

while (!found && !(p-str < q-ss)) {
const char *s = p;
const char *t = q;

while (t != ss && *(s-1) == *(t-1)) {
--s;
--t;
}

found = t == ss;

if (found) {
p = s;
} else {
--p;
}
}
}

return NUMBER_VAL(found ? p-str : -1);
}

static Value replaceString(DictuVM *vm, int argCount, Value *args) {
if (argCount != 2) {
runtimeError(vm, "replace() takes 2 arguments (%d given)", argCount);
Expand Down Expand Up @@ -562,6 +605,7 @@ void declareStringMethods(DictuVM *vm) {
defineNative(vm, &vm->stringMethods, "split", splitString);
defineNative(vm, &vm->stringMethods, "contains", containsString);
defineNative(vm, &vm->stringMethods, "find", findString);
defineNative(vm, &vm->stringMethods, "findLast", findLastString);
defineNative(vm, &vm->stringMethods, "replace", replaceString);
defineNative(vm, &vm->stringMethods, "lower", lowerString);
defineNative(vm, &vm->stringMethods, "upper", upperString);
Expand Down
17 changes: 17 additions & 0 deletions tests/strings/findLast.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* findLast.du
*
* Testing the str.findLast() method
*
* .findLast() returns the last index of the given string
*/
from UnitTest import UnitTest;

class TestStringLastIndexOf < UnitTest {
testStringLower() {
this.assertEquals("woolly woolly mammoth".findLast("woolly"), 7);
this.assertEquals("mammoth".findLast("woolly"), -1);
}
}

TestStringLastIndexOf().run();
1 change: 1 addition & 0 deletions tests/strings/import.du
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "concat.du";
import "startsWith.du";
import "endsWith.du";
import "find.du";
import "findLast.du";
import "contains.du";
import "strip.du";
import "format.du";
Expand Down