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

Support string concatenation. #6576

Merged
merged 9 commits into from
May 8, 2024
19 changes: 19 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import ch.njol.skript.lang.function.Parameter;
import ch.njol.skript.lang.function.SimpleJavaFunction;
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.registrations.DefaultClasses;
import ch.njol.skript.util.Color;
import ch.njol.skript.util.ColorRGB;
Expand Down Expand Up @@ -568,6 +569,24 @@ public Boolean[] executeSimple(Object[][] params) {
}).description("Returns true if the input is NaN (not a number).")
.examples("isNaN(0) # false", "isNaN(0/0) # true", "isNaN(sqrt(-1)) # true")
.since("2.8.0");

Functions.registerFunction(new SimpleJavaFunction<String>("concat", new Parameter[]{
new Parameter<>("texts", DefaultClasses.OBJECT, false, null)
},DefaultClasses.STRING, true) {
Moderocky marked this conversation as resolved.
Show resolved Hide resolved
@Override
public String[] executeSimple(Object[][] params) {
StringBuilder builder = new StringBuilder();
for (Object object : params[0]) {
builder.append(Classes.toString(object));
}
return new String[] {builder.toString()};
}
}).description("Joins the provided texts (and other things) into a single text.")
.examples(
"concat(\"hello \", \"there\") # hello there",
"concat(\"foo \", 100, \" bar\") # foo 100 bar"
).since("2.8.0");
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
Moderocky marked this conversation as resolved.
Show resolved Hide resolved

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ public class DefaultOperations {
Arithmetics.registerOperation(Operator.SUBTRACTION, Date.class, Timespan.class, Date::minus);
Arithmetics.registerDifference(Date.class, Timespan.class, Date::difference);

// String + String
Moderocky marked this conversation as resolved.
Show resolved Hide resolved
Arithmetics.registerOperation(Operator.ADDITION, String.class, String.class, String::concat);

}

}
29 changes: 29 additions & 0 deletions src/test/skript/tests/misc/string concatenation.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

test "string concatenation":

# string + string
assert "hello " + "there" is "hello there" with "string + string concat failed"
assert "foo" + "bar" is "foobar" with "string + string concat failed"
assert "foo" + " " + "bar" is "foo bar" with "string + string + string concat failed"
assert "hello" + " " + "there" is "hello there" with "string + string + string concat failed"

# ? + string
set {_var} to "hello"
set {_var} to {_var} + " there"
assert {_var} is "hello there" with "var + string concat failed"

# ? + string
set {_var1} to "hello "
set {_var2} to "there"
assert {_var1} + {_var2} is "hello there" with "var + var concat failed"

# variable-string + string
set {_var} to "hello"
assert "%{_var}% " + "there" is "hello there" with "var-string + string concat failed"

# string + number = <none>
# parser prevents us adding "foo" + 1 or comparing "foo" + 1 with a string
# we test the edge case where somebody slips past us!
set {_var} to 1
set {_var} to "foo" + {_var}
assert {_var} doesn't exist with "string + number concat succeeded? %{_var}%"
Moderocky marked this conversation as resolved.
Show resolved Hide resolved
31 changes: 31 additions & 0 deletions src/test/skript/tests/syntaxes/functions/concat.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

test "concat() function":

# string + string
assert concat("hello ", "there") is "hello there" with "string + string concat() failed"
assert concat("foo", "bar") is "foobar" with "string + string concat() failed"
assert concat("foo", " ", "bar") is "foo bar" with "string + string + string concat() failed"
assert concat("hello", " ", "there") is "hello there" with "string + string + string concat() failed"
assert concat("a", "b", "c", "d", "e") is "abcde" with "5 strings concat() failed"

# ? + string
set {_var} to "hello"
set {_var} to concat({_var}, " there")
assert {_var} is "hello there" with "var + string concat() failed"

# ? + string
set {_var1} to "hello "
set {_var2} to "there"
assert concat({_var1}, {_var2}) is "hello there" with "var + var concat() failed"

# variable-string + string
set {_var} to "hello"
assert concat("%{_var}% ", "there") is "hello there" with "var-string + string concat() failed"

# string + non-string
# unlike the maths expression we CAN concat objects here!
set {_var} to 1
set {_var} to concat("foo", {_var})
assert {_var} is "foo1" with "string + number concat() failed: %{_var}%"
Moderocky marked this conversation as resolved.
Show resolved Hide resolved
assert concat("a", 1, "b", 2) is "a1b2" with "strings + numbers concat() failed"
assert concat("my nice new ", stone sword) is "my nice new stone sword" with "string + item concat() failed"