Skip to content

Commit

Permalink
Merge pull request #723 from briandowns/feature/lastIndexOf
Browse files Browse the repository at this point in the history
add findLast string method
  • Loading branch information
Jason2605 authored Jan 2, 2024
2 parents 0cec9fd + dd93796 commit 023940e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
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

0 comments on commit 023940e

Please sign in to comment.