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 4 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 @@ -199,6 +199,15 @@ Returns true if a string contains another string.

To find the index of a given substring, use the `.find()` method. This method takes an optional second parameter which can be used to skip the first `n` number of appearances of the substring. This method returns `-1` if the substring could not be found. Otherwise, it returns the index of the string.

```cs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a little bit of a mix up with the docs here, the find examples have went under .findLast and the findLast have went under .find

"woolly woolly mammoth".lastIndexOf("woolly"); // 7
"mammoth".lastIndexOf("woolly"); // -1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"woolly woolly mammoth".lastIndexOf("woolly"); // 7
"mammoth".lastIndexOf("woolly"); // -1
"woolly woolly mammoth".findLast("woolly"); // 7
"mammoth".findLast("woolly"); // -1

```

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

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

```cs
"Hello, how are you?".find("how"); // 7
"hello something hello".find("hello", 2); // 16 (Skipped first occurrence)
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, "lastInxedOf() takes 1 argument (%d given)", argCount);
Jason2605 marked this conversation as resolved.
Show resolved Hide resolved
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 @@
/**
* lastIndexOf.du
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* lastIndexOf.du
* findLast.du

*
* Testing the str.lastIndexOf() method
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Testing the str.lastIndexOf() method
* Testing the str.findLast() method

*
* .lastIndexOf() returns the last index of the given string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* .lastIndexOf() returns the last index of the given string
* .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