From 7606e562d7032928d3e86bb19f637398f6872a24 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Mon, 1 Jan 2024 16:50:21 -0700 Subject: [PATCH 1/7] add lastIndexOf string method Signed-off-by: Brian Downs --- docs/docs/strings.md | 9 ++++++++ src/vm/datatypes/strings.c | 44 ++++++++++++++++++++++++++++++++++++ tests/strings/import.du | 1 + tests/strings/lastIndexOf.du | 17 ++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 tests/strings/lastIndexOf.du diff --git a/docs/docs/strings.md b/docs/docs/strings.md index 5dfb75c8..596d9c02 100644 --- a/docs/docs/strings.md +++ b/docs/docs/strings.md @@ -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 +"woolly woolly mammoth".lastIndexOf("woolly"); // 7 +"mammoth".lastIndexOf("woolly"); // -1 +``` + +### string.lastIndexOf(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) diff --git a/src/vm/datatypes/strings.c b/src/vm/datatypes/strings.c index 296084e0..6d3aca74 100644 --- a/src/vm/datatypes/strings.c +++ b/src/vm/datatypes/strings.c @@ -275,6 +275,49 @@ static Value findString(DictuVM *vm, int argCount, Value *args) { return NUMBER_VAL(position); } +static Value lastIndexOfString(DictuVM *vm, int argCount, Value *args) { + if (argCount != 1) { + runtimeError(vm, "lastInxedOf() takes 1 argument (%d given)", argCount); + return EMPTY_VAL; + } + + char *str = AS_CSTRING(args[0]); + const char *p = str; + char *ss = AS_CSTRING(args[1]); + 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); @@ -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, "lastIndexOf", lastIndexOfString); defineNative(vm, &vm->stringMethods, "replace", replaceString); defineNative(vm, &vm->stringMethods, "lower", lowerString); defineNative(vm, &vm->stringMethods, "upper", upperString); diff --git a/tests/strings/import.du b/tests/strings/import.du index 151d4745..fb1a9cfa 100644 --- a/tests/strings/import.du +++ b/tests/strings/import.du @@ -12,6 +12,7 @@ import "concat.du"; import "startsWith.du"; import "endsWith.du"; import "find.du"; +import "lastIndexOf.du"; import "contains.du"; import "strip.du"; import "format.du"; diff --git a/tests/strings/lastIndexOf.du b/tests/strings/lastIndexOf.du new file mode 100644 index 00000000..bed5ecf6 --- /dev/null +++ b/tests/strings/lastIndexOf.du @@ -0,0 +1,17 @@ +/** + * lastIndexOf.du + * + * Testing the str.lastIndexOf() method + * + * .lastIndexOf() returns the last index of the given string + */ +from UnitTest import UnitTest; + +class TestStringLastIndexOf < UnitTest { + testStringLower() { + this.assertEquals("woolly woolly mammoth".lastIndexOf("woolly"), 7); + this.assertEquals("mammoth".lastIndexOf("woolly"), -1); + } +} + +TestStringLastIndexOf().run(); \ No newline at end of file From e1ae3bcb328601f84a04ba287b3f7c506987fa1c Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Mon, 1 Jan 2024 16:52:33 -0700 Subject: [PATCH 2/7] make vars const Signed-off-by: Brian Downs --- src/vm/datatypes/strings.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vm/datatypes/strings.c b/src/vm/datatypes/strings.c index 6d3aca74..cabbea1c 100644 --- a/src/vm/datatypes/strings.c +++ b/src/vm/datatypes/strings.c @@ -281,9 +281,9 @@ static Value lastIndexOfString(DictuVM *vm, int argCount, Value *args) { return EMPTY_VAL; } - char *str = AS_CSTRING(args[0]); + const char *str = AS_CSTRING(args[0]); + const char *ss = AS_CSTRING(args[1]); const char *p = str; - char *ss = AS_CSTRING(args[1]); int found = !*ss; if (!found) { From 956499938f6831f1c425ffb598483ebd995010fc Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Tue, 2 Jan 2024 10:29:04 -0700 Subject: [PATCH 3/7] pr remediations Signed-off-by: Brian Downs --- docs/docs/strings.md | 2 +- src/vm/datatypes/strings.c | 4 ++-- tests/strings/{lastIndexOf.du => findLast.du} | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename tests/strings/{lastIndexOf.du => findLast.du} (65%) diff --git a/docs/docs/strings.md b/docs/docs/strings.md index 596d9c02..62dfbbf9 100644 --- a/docs/docs/strings.md +++ b/docs/docs/strings.md @@ -204,7 +204,7 @@ To find the index of a given substring, use the `.find()` method. This method ta "mammoth".lastIndexOf("woolly"); // -1 ``` -### string.lastIndexOf(String) -> Number +### string.findLast(String) -> Number Returns the last index of the given string. If the substring doesn't exist, -1 is returned. diff --git a/src/vm/datatypes/strings.c b/src/vm/datatypes/strings.c index cabbea1c..360ae3ea 100644 --- a/src/vm/datatypes/strings.c +++ b/src/vm/datatypes/strings.c @@ -275,7 +275,7 @@ static Value findString(DictuVM *vm, int argCount, Value *args) { return NUMBER_VAL(position); } -static Value lastIndexOfString(DictuVM *vm, int argCount, Value *args) { +static Value findLastString(DictuVM *vm, int argCount, Value *args) { if (argCount != 1) { runtimeError(vm, "lastInxedOf() takes 1 argument (%d given)", argCount); return EMPTY_VAL; @@ -605,7 +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, "lastIndexOf", lastIndexOfString); + defineNative(vm, &vm->stringMethods, "findLast", findLastString); defineNative(vm, &vm->stringMethods, "replace", replaceString); defineNative(vm, &vm->stringMethods, "lower", lowerString); defineNative(vm, &vm->stringMethods, "upper", upperString); diff --git a/tests/strings/lastIndexOf.du b/tests/strings/findLast.du similarity index 65% rename from tests/strings/lastIndexOf.du rename to tests/strings/findLast.du index bed5ecf6..56b7a202 100644 --- a/tests/strings/lastIndexOf.du +++ b/tests/strings/findLast.du @@ -9,8 +9,8 @@ from UnitTest import UnitTest; class TestStringLastIndexOf < UnitTest { testStringLower() { - this.assertEquals("woolly woolly mammoth".lastIndexOf("woolly"), 7); - this.assertEquals("mammoth".lastIndexOf("woolly"), -1); + this.assertEquals("woolly woolly mammoth".findLast("woolly"), 7); + this.assertEquals("mammoth".findLast("woolly"), -1); } } From 7344f187eab808c680c5a902052e24134e2c47ce Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Tue, 2 Jan 2024 10:35:27 -0700 Subject: [PATCH 4/7] update tests Signed-off-by: Brian Downs --- tests/strings/import.du | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/strings/import.du b/tests/strings/import.du index fb1a9cfa..624c70e2 100644 --- a/tests/strings/import.du +++ b/tests/strings/import.du @@ -12,7 +12,7 @@ import "concat.du"; import "startsWith.du"; import "endsWith.du"; import "find.du"; -import "lastIndexOf.du"; +import "findLast.du"; import "contains.du"; import "strip.du"; import "format.du"; From eb526edfa0314db471705b5afdc34466988bb98a Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Tue, 2 Jan 2024 15:47:11 -0700 Subject: [PATCH 5/7] update function name Signed-off-by: Brian Downs --- docs/docs/strings.md | 4 ++-- tests/strings/findLast.du | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/docs/strings.md b/docs/docs/strings.md index 62dfbbf9..3b79e1af 100644 --- a/docs/docs/strings.md +++ b/docs/docs/strings.md @@ -200,8 +200,8 @@ 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 -"woolly woolly mammoth".lastIndexOf("woolly"); // 7 -"mammoth".lastIndexOf("woolly"); // -1 +"woolly woolly mammoth".findLast("woolly"); // 7 +"mammoth".findLast("woolly"); // -1 ``` ### string.findLast(String) -> Number diff --git a/tests/strings/findLast.du b/tests/strings/findLast.du index 56b7a202..1f7d04ea 100644 --- a/tests/strings/findLast.du +++ b/tests/strings/findLast.du @@ -1,9 +1,9 @@ /** - * lastIndexOf.du + * findLast.du * - * Testing the str.lastIndexOf() method + * Testing the str.findLast() method * - * .lastIndexOf() returns the last index of the given string + * .findLast() returns the last index of the given string */ from UnitTest import UnitTest; From 256ca6f2834d2fc5b293c319e2f41532996a82b2 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Tue, 2 Jan 2024 15:48:59 -0700 Subject: [PATCH 6/7] fix docs mix up Signed-off-by: Brian Downs --- docs/docs/strings.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/docs/strings.md b/docs/docs/strings.md index 3b79e1af..bc4520af 100644 --- a/docs/docs/strings.md +++ b/docs/docs/strings.md @@ -200,8 +200,9 @@ 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 -"woolly woolly mammoth".findLast("woolly"); // 7 -"mammoth".findLast("woolly"); // -1 +"Hello, how are you?".find("how"); // 7 +"hello something hello".find("hello", 2); // 16 (Skipped first occurrence) +"House".find("Lost Keys"); // -1 (Not found) ``` ### string.findLast(String) -> Number @@ -209,9 +210,8 @@ To find the index of a given substring, use the `.find()` method. This method ta 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) -"House".find("Lost Keys"); // -1 (Not found) +"woolly woolly mammoth".findLast("woolly"); // 7 +"mammoth".findLast("woolly"); // -1 ``` ### string.leftStrip() -> String From dd93796911d28206ac022ce527ce342bc9071dd0 Mon Sep 17 00:00:00 2001 From: Jason_000 Date: Tue, 2 Jan 2024 23:05:50 +0000 Subject: [PATCH 7/7] Update src/vm/datatypes/strings.c --- src/vm/datatypes/strings.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm/datatypes/strings.c b/src/vm/datatypes/strings.c index 360ae3ea..297ca010 100644 --- a/src/vm/datatypes/strings.c +++ b/src/vm/datatypes/strings.c @@ -277,7 +277,7 @@ static Value findString(DictuVM *vm, int argCount, Value *args) { static Value findLastString(DictuVM *vm, int argCount, Value *args) { if (argCount != 1) { - runtimeError(vm, "lastInxedOf() takes 1 argument (%d given)", argCount); + runtimeError(vm, "findLast() takes 1 argument (%d given)", argCount); return EMPTY_VAL; }