From aa7b9cefaa14ffb68cb48e1236e5a7e950214a6c Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Sat, 13 Jan 2024 19:08:16 -0700 Subject: [PATCH 01/11] add collapseSpaces method to string type Signed-off-by: Brian Downs --- docs/docs/strings.md | 8 ++++++++ src/vm/datatypes/strings.c | 20 ++++++++++++++++++++ tests/strings/collapseSpaces.du | 19 +++++++++++++++++++ tests/strings/concat.du | 2 +- 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/strings/collapseSpaces.du diff --git a/docs/docs/strings.md b/docs/docs/strings.md index 8b8e6cf9..ba8d4c3f 100644 --- a/docs/docs/strings.md +++ b/docs/docs/strings.md @@ -310,3 +310,11 @@ Returns the number of words in the given string. Empty strings are considered fa "This is a sentence".wordCount(); // 4 "This is an even longer sentence".wordCount(); // 6 ``` + +### string.collapseSpaces() -> Number + +Returns a string with extraneous whitespace removed. + +```cs +"This is a huge string of a lot of spaces.".collapseSpaces(); // "This is a huge string of a lot of spaces." +``` diff --git a/src/vm/datatypes/strings.c b/src/vm/datatypes/strings.c index 929acdba..c730a700 100644 --- a/src/vm/datatypes/strings.c +++ b/src/vm/datatypes/strings.c @@ -668,6 +668,25 @@ static Value isLowerString(DictuVM *vm, int argCount, Value *args) { return BOOL_VAL(true); } +static Value collapseSpacesString(DictuVM *vm, int argCount, Value *args) { + if (argCount != 0) { + runtimeError(vm, "collapseSpaces() takes no arguments (%d given)", argCount); + return EMPTY_VAL; + } + + char *string = AS_CSTRING(args[0]); + + int i, j; + for (i = j = 0; string[i]; ++i) { + if(!isspace(string[i]) || (i > 0 && !isspace(string[i-1]))) { + string[j++] = string[i]; + } + } + string[j+1] = '\0'; + + return OBJ_VAL(copyString(vm, string, strlen(string) - 1)); +} + void declareStringMethods(DictuVM *vm) { defineNative(vm, &vm->stringMethods, "len", lenString); defineNative(vm, &vm->stringMethods, "toNumber", toNumberString); @@ -691,5 +710,6 @@ void declareStringMethods(DictuVM *vm) { defineNative(vm, &vm->stringMethods, "repeat", repeatString); defineNative(vm, &vm->stringMethods, "isUpper", isUpperString); defineNative(vm, &vm->stringMethods, "isLower", isLowerString); + defineNative(vm, &vm->stringMethods, "collapseSpaces", collapseSpacesString); } diff --git a/tests/strings/collapseSpaces.du b/tests/strings/collapseSpaces.du new file mode 100644 index 00000000..d3e2893a --- /dev/null +++ b/tests/strings/collapseSpaces.du @@ -0,0 +1,19 @@ +/** + * collapseSpaces.du + * + * Testing the str.collapseSpaces() method + * + * .collapseSpaces() returns a string with extraneous spaces removed. + */ +from UnitTest import UnitTest; + +class TestStringCollapseSpaces < UnitTest { + testStringCollapseSpaces() { + const testString = "This is a huge string of a lot of spaces."; + const expected = "This is a huge string of a lot of spaces."; + const res = testString.collapseSpaces(); + this.assertEquals(res, expected); + } +} + +TestStringCollapseSpaces().run(); \ No newline at end of file diff --git a/tests/strings/concat.du b/tests/strings/concat.du index c25a5f76..9518698c 100644 --- a/tests/strings/concat.du +++ b/tests/strings/concat.du @@ -17,4 +17,4 @@ class TestStringConcat < UnitTest { } } -TestStringConcat().run(); \ No newline at end of file +TestStringConcat().run(); From 2e2b6aaaa8a1a3ca1ef136bd1a3029301ac49bc4 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Sat, 13 Jan 2024 20:23:06 -0700 Subject: [PATCH 02/11] add wrap string method Signed-off-by: Brian Downs --- docs/docs/strings.md | 10 +++++++++- src/vm/datatypes/strings.c | 27 +++++++++++++++++++++++++++ tests/strings/import.du | 2 ++ tests/strings/wrap.du | 21 +++++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/strings/wrap.du diff --git a/docs/docs/strings.md b/docs/docs/strings.md index ba8d4c3f..15991572 100644 --- a/docs/docs/strings.md +++ b/docs/docs/strings.md @@ -311,10 +311,18 @@ Returns the number of words in the given string. Empty strings are considered fa "This is an even longer sentence".wordCount(); // 6 ``` -### string.collapseSpaces() -> Number +### string.collapseSpaces() -> String Returns a string with extraneous whitespace removed. ```cs "This is a huge string of a lot of spaces.".collapseSpaces(); // "This is a huge string of a lot of spaces." ``` + +### string.wrap() -> String + +Returns a new string with new lines inserted at the given length. + +```cs +"This is a really really long string that will need to be broken up for whatever reason the caller has determined. Not out business as to why, but we can provide the functionality for them to do so.".wrap(80); +``` diff --git a/src/vm/datatypes/strings.c b/src/vm/datatypes/strings.c index c730a700..7c7f17f4 100644 --- a/src/vm/datatypes/strings.c +++ b/src/vm/datatypes/strings.c @@ -687,6 +687,32 @@ static Value collapseSpacesString(DictuVM *vm, int argCount, Value *args) { return OBJ_VAL(copyString(vm, string, strlen(string) - 1)); } +static Value wrapString(DictuVM *vm, int argCount, Value *args) { + if (argCount != 1) { + runtimeError(vm, "wrap() takes 1 argument (%d given)", argCount); + return EMPTY_VAL; + } + + char *string = AS_CSTRING(args[0]); + int len = AS_NUMBER(args[1]); + + int last_space = 0; + int counter = 0; + + for (int current = 0; string[current] != '\0'; current++, counter++) { + if (isspace(string[current])) { + last_space = current; + } + + if (counter >= len) { + string[last_space] = '\n'; + counter = 0; + } + } + + return OBJ_VAL(copyString(vm, string, strlen(string) - 1)); +} + void declareStringMethods(DictuVM *vm) { defineNative(vm, &vm->stringMethods, "len", lenString); defineNative(vm, &vm->stringMethods, "toNumber", toNumberString); @@ -711,5 +737,6 @@ void declareStringMethods(DictuVM *vm) { defineNative(vm, &vm->stringMethods, "isUpper", isUpperString); defineNative(vm, &vm->stringMethods, "isLower", isLowerString); defineNative(vm, &vm->stringMethods, "collapseSpaces", collapseSpacesString); + defineNative(vm, &vm->stringMethods, "wrap", wrapString); } diff --git a/tests/strings/import.du b/tests/strings/import.du index 709f74fe..766fc8ec 100644 --- a/tests/strings/import.du +++ b/tests/strings/import.du @@ -29,3 +29,5 @@ import "title.du"; import "isUpper.du"; import "isLower.du"; import "wordCount.du"; +import "collapseSpaces.du"; +import "wrap.du"; diff --git a/tests/strings/wrap.du b/tests/strings/wrap.du new file mode 100644 index 00000000..442b0465 --- /dev/null +++ b/tests/strings/wrap.du @@ -0,0 +1,21 @@ +/** + * wrap.du + * + * Testing the str.wrap() method + * + * .wrap() returns a new string with new lines inserted at the given length. + */ +from UnitTest import UnitTest; + +class TestStringWrap < UnitTest { + const maxLen = 80; + + testStringWrap() { + const testString = "This is a really really long string that will need to be broken up for whatever reason the caller has determined. Not out business as to why, but we can provide the functionality for them to do so."; + const res = testString.wrap(this.maxLen); + const idx = res.find("\n"); + this.assertTruthy(res.find("\n") <= this.maxLen); + } +} + +TestStringWrap().run(); From e3b29bd39f4ccc87a27d4c1f2476f09a1b087282 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Sat, 13 Jan 2024 20:46:37 -0700 Subject: [PATCH 03/11] newlines Signed-off-by: Brian Downs --- src/vm/datatypes/sets.c | 1 - src/vm/datatypes/strings.c | 1 - 2 files changed, 2 deletions(-) diff --git a/src/vm/datatypes/sets.c b/src/vm/datatypes/sets.c index 0b3a3f08..f8907688 100644 --- a/src/vm/datatypes/sets.c +++ b/src/vm/datatypes/sets.c @@ -122,4 +122,3 @@ void declareSetMethods(DictuVM *vm) { defineNative(vm, &vm->setMethods, "containsAll", containsAllSet); defineNative(vm, &vm->setMethods, "toBool", boolNative); // Defined in util } - diff --git a/src/vm/datatypes/strings.c b/src/vm/datatypes/strings.c index 7c7f17f4..d656505d 100644 --- a/src/vm/datatypes/strings.c +++ b/src/vm/datatypes/strings.c @@ -738,5 +738,4 @@ void declareStringMethods(DictuVM *vm) { defineNative(vm, &vm->stringMethods, "isLower", isLowerString); defineNative(vm, &vm->stringMethods, "collapseSpaces", collapseSpacesString); defineNative(vm, &vm->stringMethods, "wrap", wrapString); - } From b08a365c343374a3b170d50a0191074dfbca77c1 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Sat, 13 Jan 2024 20:50:36 -0700 Subject: [PATCH 04/11] update code style Signed-off-by: Brian Downs --- src/vm/datatypes/strings.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/vm/datatypes/strings.c b/src/vm/datatypes/strings.c index d656505d..55519249 100644 --- a/src/vm/datatypes/strings.c +++ b/src/vm/datatypes/strings.c @@ -696,17 +696,17 @@ static Value wrapString(DictuVM *vm, int argCount, Value *args) { char *string = AS_CSTRING(args[0]); int len = AS_NUMBER(args[1]); - int last_space = 0; - int counter = 0; + int last = 0; + int count = 0; - for (int current = 0; string[current] != '\0'; current++, counter++) { - if (isspace(string[current])) { - last_space = current; + for (int cur = 0; string[cur] != '\0'; cur++, count++) { + if (isspace(string[cur])) { + last = cur; } - if (counter >= len) { - string[last_space] = '\n'; - counter = 0; + if (count >= len) { + string[last] = '\n'; + count = 0; } } From 809b187dc7be064aeb06dd888a03fdaf1bfc8ce4 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Sat, 13 Jan 2024 20:58:27 -0700 Subject: [PATCH 05/11] add new line Signed-off-by: Brian Downs --- tests/strings/collapseSpaces.du | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/strings/collapseSpaces.du b/tests/strings/collapseSpaces.du index d3e2893a..c448c444 100644 --- a/tests/strings/collapseSpaces.du +++ b/tests/strings/collapseSpaces.du @@ -16,4 +16,4 @@ class TestStringCollapseSpaces < UnitTest { } } -TestStringCollapseSpaces().run(); \ No newline at end of file +TestStringCollapseSpaces().run(); From 76d28e93c7420f95c10de76bf87c82a106cd6be4 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Sat, 20 Jan 2024 20:15:04 -0700 Subject: [PATCH 06/11] redo implementations Signed-off-by: Brian Downs --- src/vm/datatypes/strings.c | 31 +++++++++++++++++++------------ tests/strings/collapseSpaces.du | 1 + 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/vm/datatypes/strings.c b/src/vm/datatypes/strings.c index 55519249..0202ec04 100644 --- a/src/vm/datatypes/strings.c +++ b/src/vm/datatypes/strings.c @@ -674,17 +674,20 @@ static Value collapseSpacesString(DictuVM *vm, int argCount, Value *args) { return EMPTY_VAL; } - char *string = AS_CSTRING(args[0]); + ObjString *string = AS_STRING(args[0]); + char *temp = ALLOCATE(vm, char, string->length + 1); + strcpy(temp, string->chars); int i, j; - for (i = j = 0; string[i]; ++i) { - if(!isspace(string[i]) || (i > 0 && !isspace(string[i-1]))) { - string[j++] = string[i]; + for (i = j = 0; temp[i]; ++i) { + if(!isspace(temp[i]) || (i > 0 && !isspace(temp[i-1]))) { + temp[j++] = temp[i]; } } - string[j+1] = '\0'; + temp[j+1] = '\0'; + temp = SHRINK_ARRAY(vm, temp, char, string->length + 1, strlen(temp)); - return OBJ_VAL(copyString(vm, string, strlen(string) - 1)); + return OBJ_VAL(takeString(vm, temp, strlen(temp)-1)); } static Value wrapString(DictuVM *vm, int argCount, Value *args) { @@ -693,24 +696,28 @@ static Value wrapString(DictuVM *vm, int argCount, Value *args) { return EMPTY_VAL; } - char *string = AS_CSTRING(args[0]); + ObjString *string = AS_STRING(args[0]); + char *temp = ALLOCATE(vm, char, string->length + 1); + int len = AS_NUMBER(args[1]); int last = 0; int count = 0; - for (int cur = 0; string[cur] != '\0'; cur++, count++) { - if (isspace(string[cur])) { + for (int cur = 0; string->chars[cur] != '\0'; cur++, count++) { + temp[cur] = string->chars[cur]; + + if (isspace(temp[cur])) { last = cur; - } + } if (count >= len) { - string[last] = '\n'; + temp[last] = '\n'; count = 0; } } - return OBJ_VAL(copyString(vm, string, strlen(string) - 1)); + return OBJ_VAL(takeString(vm, temp, strlen(temp))); } void declareStringMethods(DictuVM *vm) { diff --git a/tests/strings/collapseSpaces.du b/tests/strings/collapseSpaces.du index c448c444..1ef92a43 100644 --- a/tests/strings/collapseSpaces.du +++ b/tests/strings/collapseSpaces.du @@ -12,6 +12,7 @@ class TestStringCollapseSpaces < UnitTest { const testString = "This is a huge string of a lot of spaces."; const expected = "This is a huge string of a lot of spaces."; const res = testString.collapseSpaces(); + print(res); this.assertEquals(res, expected); } } From 5181778da40c23a7fe85311ffb232c19721b0746 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Sat, 20 Jan 2024 20:17:04 -0700 Subject: [PATCH 07/11] update docs Signed-off-by: Brian Downs --- docs/docs/strings.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/strings.md b/docs/docs/strings.md index 15991572..8a3f6eba 100644 --- a/docs/docs/strings.md +++ b/docs/docs/strings.md @@ -319,7 +319,7 @@ Returns a string with extraneous whitespace removed. "This is a huge string of a lot of spaces.".collapseSpaces(); // "This is a huge string of a lot of spaces." ``` -### string.wrap() -> String +### string.wrap(Number) -> String Returns a new string with new lines inserted at the given length. From 818f19c729f79717044f8134a4e8cbcc0524c5f0 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Sun, 21 Jan 2024 19:25:38 -0700 Subject: [PATCH 08/11] add addtional test Signed-off-by: Brian Downs --- tests/strings/collapseSpaces.du | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/strings/collapseSpaces.du b/tests/strings/collapseSpaces.du index 1ef92a43..d885ee3f 100644 --- a/tests/strings/collapseSpaces.du +++ b/tests/strings/collapseSpaces.du @@ -12,8 +12,8 @@ class TestStringCollapseSpaces < UnitTest { const testString = "This is a huge string of a lot of spaces."; const expected = "This is a huge string of a lot of spaces."; const res = testString.collapseSpaces(); - print(res); this.assertEquals(res, expected); + this.assertNotEquals(testString, expected); } } From c01149d0b99c14d0460efd140824bcbf7960f11c Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Sun, 21 Jan 2024 20:22:13 -0700 Subject: [PATCH 09/11] update docs Signed-off-by: Brian Downs --- docs/docs/strings.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/strings.md b/docs/docs/strings.md index 8a3f6eba..bc946f67 100644 --- a/docs/docs/strings.md +++ b/docs/docs/strings.md @@ -324,5 +324,5 @@ Returns a string with extraneous whitespace removed. Returns a new string with new lines inserted at the given length. ```cs -"This is a really really long string that will need to be broken up for whatever reason the caller has determined. Not out business as to why, but we can provide the functionality for them to do so.".wrap(80); +"This is a really really long string that will need to be broken up for whatever reason the caller has determined. Not our business as to why, but we can provide the functionality for them to do so.".wrap(80); ``` From 47fa9a95091f413a6ee74c3715f6770962fff27d Mon Sep 17 00:00:00 2001 From: Jason_000 Date: Mon, 22 Jan 2024 17:49:26 +0000 Subject: [PATCH 10/11] Reuse length variable and only resize if we need to --- src/vm/datatypes/strings.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/vm/datatypes/strings.c b/src/vm/datatypes/strings.c index 0202ec04..32860c81 100644 --- a/src/vm/datatypes/strings.c +++ b/src/vm/datatypes/strings.c @@ -680,14 +680,18 @@ static Value collapseSpacesString(DictuVM *vm, int argCount, Value *args) { int i, j; for (i = j = 0; temp[i]; ++i) { - if(!isspace(temp[i]) || (i > 0 && !isspace(temp[i-1]))) { + if (!isspace(temp[i]) || (i > 0 && !isspace(temp[i-1]))) { temp[j++] = temp[i]; } } + temp[j+1] = '\0'; - temp = SHRINK_ARRAY(vm, temp, char, string->length + 1, strlen(temp)); - return OBJ_VAL(takeString(vm, temp, strlen(temp)-1)); + if (i != j) { + temp = SHRINK_ARRAY(vm, temp, char, string->length + 1, j); + } + + return OBJ_VAL(takeString(vm, temp, j)); } static Value wrapString(DictuVM *vm, int argCount, Value *args) { From 7f997ccda24bac3f9d6c963d293aa641e2fd7e77 Mon Sep 17 00:00:00 2001 From: Jason_000 Date: Mon, 22 Jan 2024 17:55:56 +0000 Subject: [PATCH 11/11] Correct memory tracking --- 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 32860c81..e58ca3e6 100644 --- a/src/vm/datatypes/strings.c +++ b/src/vm/datatypes/strings.c @@ -688,7 +688,7 @@ static Value collapseSpacesString(DictuVM *vm, int argCount, Value *args) { temp[j+1] = '\0'; if (i != j) { - temp = SHRINK_ARRAY(vm, temp, char, string->length + 1, j); + temp = SHRINK_ARRAY(vm, temp, char, string->length + 1, j + 1); } return OBJ_VAL(takeString(vm, temp, j));