From be16b30f24ec85796dd6094b5cd2af1093e2ab44 Mon Sep 17 00:00:00 2001 From: PureFox48 <64583745+PureFox48@users.noreply.github.com> Date: Sat, 4 Mar 2023 10:34:37 +0000 Subject: [PATCH 1/5] Update wren_core.c --- src/vm/wren_core.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index d0a121f8c..83a19f3aa 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -1042,6 +1042,31 @@ DEF_PRIMITIVE(string_codePointAt) string->length - index)); } +DEF_PRIMITIVE(string_compareTo) +{ + if (!validateString(vm, args[1], "Argument")) return false; + + ObjString* str1 = AS_STRING(args[0]); + ObjString* str2 = AS_STRING(args[1]); + + size_t len1 = str1->length; + size_t len2 = str2->length; + + // Get minimum length for comparison. + size_t minLen = (len1 <= len2) ? len1 : len2; + int res = memcmp(str1->value, str2->value, minLen); + + // If result is non-zero, just return that. + if (res) RETURN_NUM(res); + + // If the lengths are the same, the strings must be equal + if (len1 == len2) RETURN_NUM(0); + + // Otherwise the shorter string will come first. + res = (len1 < len2) ? -1 : 1; + RETURN_NUM(res); +} + DEF_PRIMITIVE(string_contains) { if (!validateString(vm, args[1], "Argument")) return false; @@ -1417,6 +1442,7 @@ void wrenInitializeCore(WrenVM* vm) PRIMITIVE(vm->stringClass, "byteAt_(_)", string_byteAt); PRIMITIVE(vm->stringClass, "byteCount_", string_byteCount); PRIMITIVE(vm->stringClass, "codePointAt_(_)", string_codePointAt); + PRIMITIVE(vm->stringClass, "compareTo(_)", string_compareTo); PRIMITIVE(vm->stringClass, "contains(_)", string_contains); PRIMITIVE(vm->stringClass, "endsWith(_)", string_endsWith); PRIMITIVE(vm->stringClass, "indexOf(_)", string_indexOf1); From 5fd50229ffc95d0c0d242ccb7ae10a97dbe5e46c Mon Sep 17 00:00:00 2001 From: PureFox48 <64583745+PureFox48@users.noreply.github.com> Date: Sat, 4 Mar 2023 10:39:43 +0000 Subject: [PATCH 2/5] Update wren_core.wren --- src/vm/wren_core.wren | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/vm/wren_core.wren b/src/vm/wren_core.wren index f073062c2..ca5e52120 100644 --- a/src/vm/wren_core.wren +++ b/src/vm/wren_core.wren @@ -289,6 +289,11 @@ class String is Sequence { } return result } + + < (other) { compareTo(other) < 0 } + <= (other) { compareTo(other) <= 0 } + > (other) { compareTo(other) > 0 } + >= (other) { compareTo(other) >= 0 } } class StringByteSequence is Sequence { From a91b583c396bdfc0b2427edcba93970f6d896fdb Mon Sep 17 00:00:00 2001 From: PureFox48 <64583745+PureFox48@users.noreply.github.com> Date: Sat, 4 Mar 2023 10:51:26 +0000 Subject: [PATCH 3/5] Update wren_core.wren.inc --- src/vm/wren_core.wren.inc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/vm/wren_core.wren.inc b/src/vm/wren_core.wren.inc index be296cdf7..7aeb0d619 100644 --- a/src/vm/wren_core.wren.inc +++ b/src/vm/wren_core.wren.inc @@ -291,6 +291,11 @@ static const char* coreModuleSource = " }\n" " return result\n" " }\n" +"\n" +" < (other) { compareTo(other) < 0 }\n" +" <= (other) { compareTo(other) <= 0 }\n" +" > (other) { compareTo(other) > 0 }\n" +" >= (other) { compareTo(other) >= 0 }\n" "}\n" "\n" "class StringByteSequence is Sequence {\n" From 31b60a9978356465b4f6ebc68befacc9ac5e3899 Mon Sep 17 00:00:00 2001 From: PureFox48 <64583745+PureFox48@users.noreply.github.com> Date: Sat, 4 Mar 2023 11:48:43 +0000 Subject: [PATCH 4/5] Create comparisons.wren --- test/core/string/comparisons.wren | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 test/core/string/comparisons.wren diff --git a/test/core/string/comparisons.wren b/test/core/string/comparisons.wren new file mode 100644 index 000000000..395ba7bf8 --- /dev/null +++ b/test/core/string/comparisons.wren @@ -0,0 +1,16 @@ +System.print("" < "abc") // expect: true +System.print("abc" < "def") // expect: true +System.print("abc" < "abc") // expect: false +System.print("abc" <= "abc") // expect: true +System.print("ghi" > "") // expect: true +System.print("ghi" > "abc") // expect: true +System.print("ghi" > "ghi") // expect: false +System.print("ghi" >= "ghi") // expect: true + +// Non-ascii +System.print("ÀÁA" < "àáa") // expect: true +System.print("ÀÁA" < "ÀÁA") // expect: false +System.print("ÀÁA" <= "ÀÁA") // expect: true +System.print("a€E" > "a€e") // expect: false +System.print("a€E" > "a€E") // expect: false +System.print("a€E" >= "a€E") // expect: true From 870e5172eba753b05d0b336f709a4df8bcdcf07b Mon Sep 17 00:00:00 2001 From: PureFox48 <64583745+PureFox48@users.noreply.github.com> Date: Sat, 4 Mar 2023 12:36:34 +0000 Subject: [PATCH 5/5] Update string.markdown --- doc/site/modules/core/string.markdown | 32 ++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/doc/site/modules/core/string.markdown b/doc/site/modules/core/string.markdown index e1a58515d..114f583df 100644 --- a/doc/site/modules/core/string.markdown +++ b/doc/site/modules/core/string.markdown @@ -117,6 +117,20 @@ var string = "(ᵔᴥᵔ)" System.print(string.codePoints[2]) //> -1 (in the middle of "ᵔ") +### **compareTo**(other) + +Compares this string with `other` lexicographically by codepoint. + +Returns a negative value if `this` appears before `other`, zero if `this` compares equal to `other` or a positive value if `this` appears after `other` in lexicographical order. + +
+System.print("abc".compareTo("def")) //> -3
+System.print("def".compareTo("abc")) //> 3
+System.print("abc".compareTo("abc")) //> 0
+
+ +It is a runtime error if `other` is not a string. + ### **contains**(other) Checks if `other` is a substring of the string. @@ -268,7 +282,23 @@ Checks if the string is equal to `other`. ### **!=**(other) operator -Check if the string is not equal to `other`. +Checks if the string is not equal to `other`. + +### **<**(other) operator + +Checks if the string appears before `other` in lexicographical order. + +### **<=**(other) operator + +Checks if the string appears before or compares equal to `other` in lexicographical order. + +### **>**(other) operator + +Checks if the string appears after `other` in lexicographical order + +### **>=**(other) operator + +Checks if the string appears after or compares equal to `other` in lexicographical order. ### **[**index**]** operator