From 02d3094e51e0dd11ab27adac93411669b9d9d751 Mon Sep 17 00:00:00 2001
From: Pieter12345
Date: Sun, 17 Dec 2023 04:57:21 +0100
Subject: [PATCH] Add string_compare function
---
.../core/functions/StringHandling.java | 61 +++++++++++++++++++
1 file changed, 61 insertions(+)
diff --git a/src/main/java/com/laytonsmith/core/functions/StringHandling.java b/src/main/java/com/laytonsmith/core/functions/StringHandling.java
index f8b9592a1..bc84231aa 100644
--- a/src/main/java/com/laytonsmith/core/functions/StringHandling.java
+++ b/src/main/java/com/laytonsmith/core/functions/StringHandling.java
@@ -2298,6 +2298,67 @@ public ExampleScript[] examples() throws ConfigCompileException {
}
+ @api
+ public static class string_compare extends AbstractFunction {
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Class extends CREThrowable>[] thrown() {
+ return new Class[]{};
+ }
+
+ @Override
+ public boolean isRestricted() {
+ return false;
+ }
+
+ @Override
+ public Boolean runAsync() {
+ return null;
+ }
+
+ @Override
+ public Mixed exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
+ return new CInt(args[0].val().compareTo(args[1].val()), t);
+ }
+
+ @Override
+ public String getName() {
+ return "string_compare";
+ }
+
+ @Override
+ public Integer[] numArgs() {
+ return new Integer[]{2};
+ }
+
+ @Override
+ public String docs() {
+ return "int {string s1, string s2} Compares two strings lexicographically."
+ + " The comparison is based on the Unicode value of each character in the strings."
+ + " Returns 0 if s1 is equal to s2, a negative value if s1 is lexographically less than s2,"
+ + " and a positive value if s1 is lexigraphically greater than s2."
+ + " The magnitude of non-zero return values is the difference between the char values at the first"
+ + " index at which a different char was found in both strings."
+ + " If all chars match but the strings differ in length, then the magnitude is this difference.";
+ }
+
+ @Override
+ public Version since() {
+ return MSVersion.V3_3_5;
+ }
+
+ @Override
+ public ExampleScript[] examples() throws ConfigCompileException {
+ return new ExampleScript[]{
+ new ExampleScript("", "string_compare('axx', 'bxx')"),
+ new ExampleScript("", "string_compare('xbx', 'xax')"),
+ new ExampleScript("", "string_compare('abc', 'abc')"),
+ new ExampleScript("", "string_compare('abc', 'abcde')")
+ };
+ }
+ }
+
@api
public static class string_multiply extends AbstractFunction {