Skip to content

Commit

Permalink
Support string concatenation. (#6576)
Browse files Browse the repository at this point in the history
* Add string concatenation.

* Add string concat function.

* Add string concat() and tests.

* Apply suggestions from code review

Co-authored-by: _tud <98935832+UnderscoreTud@users.noreply.github.com>
Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com>

* Update src/main/java/ch/njol/skript/classes/data/DefaultOperations.java

Co-authored-by: _tud <98935832+UnderscoreTud@users.noreply.github.com>

* Variables? We don't need assertion output variables where we're going! (Thanks for your PR sovde)

---------

Co-authored-by: _tud <98935832+UnderscoreTud@users.noreply.github.com>
Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com>
  • Loading branch information
3 people committed May 8, 2024
1 parent e4c0a15 commit 5928f8c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java
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) {
@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("INSERT VERSION");

}

}
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
Arithmetics.registerOperation(Operator.ADDITION, String.class, String.class, String::concat);

}

}
29 changes: 29 additions & 0 deletions src/test/skript/tests/misc/string concatenation.sk
@@ -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"
31 changes: 31 additions & 0 deletions src/test/skript/tests/syntaxes/functions/concat.sk
@@ -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"
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"

0 comments on commit 5928f8c

Please sign in to comment.