Skip to content

Commit

Permalink
Revert "Fix quote parsing (SkriptLang#3762)"
Browse files Browse the repository at this point in the history
This reverts commit 842bb58
  • Loading branch information
bilektugrul committed Dec 22, 2022
1 parent df0dbcd commit 7aa9a8b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 70 deletions.
6 changes: 1 addition & 5 deletions src/main/java/ch/njol/skript/lang/SkriptParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1155,15 +1155,11 @@ static int countUnescaped(final String pattern, final char c, final int start, f
* @return Index of the end quote
*/
private static int nextQuote(final String s, final int from) {
boolean inExpression = false;
for (int i = from; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '"' && !inExpression) {
if (s.charAt(i) == '"') {
if (i == s.length() - 1 || s.charAt(i + 1) != '"')
return i;
i++;
} else if (c == '%') {
inExpression = !inExpression;
}
}
return -1;
Expand Down
47 changes: 8 additions & 39 deletions src/main/java/ch/njol/skript/lang/VariableString.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ public static VariableString newInstance(String s) {

/**
* Tests whether a string is correctly quoted, i.e. only has doubled double quotes in it.
* Singular double quotes are only allowed between percentage signs.
*
* @param s The string
* @param withQuotes Whether s must be surrounded by double quotes or not
Expand All @@ -143,22 +142,12 @@ public static boolean isQuotedCorrectly(String s, boolean withQuotes) {
if (withQuotes && (!s.startsWith("\"") || !s.endsWith("\"") || s.length() < 2))
return false;
boolean quote = false;
boolean percentage = false;
for (int i = withQuotes ? 1 : 0; i < (withQuotes ? s.length() - 1 : s.length()); i++) {
if (percentage) {
if (s.charAt(i) == '%')
percentage = false;

continue;
}

if (quote && s.charAt(i) != '"')
return false;

if (s.charAt(i) == '"') {
if (s.charAt(i) != '"') {
if (quote)
return false;
} else {
quote = !quote;
} else if (s.charAt(i) == '%') {
percentage = true;
}
}
return !quote;
Expand Down Expand Up @@ -187,7 +176,7 @@ public static String unquote(String s, boolean surroundingQuotes) {
*/
@Nullable
public static VariableString newInstance(String orig, StringMode mode) {
if (mode != StringMode.VARIABLE_NAME && !isQuotedCorrectly(orig, false))
if (!isQuotedCorrectly(orig, false))
return null;
int n = StringUtils.count(orig, '%');
if (n % 2 != 0) {
Expand All @@ -196,26 +185,7 @@ public static VariableString newInstance(String orig, StringMode mode) {
}

// We must not parse color codes yet, as JSON support would be broken :(
String s;
if (mode != StringMode.VARIABLE_NAME) {
// Replace every double " character with a single ", except for those in expressions (between %)
StringBuilder stringBuilder = new StringBuilder();

boolean expression = false;
for (int i = 0; i < orig.length(); i++) {
char c = orig.charAt(i);
stringBuilder.append(c);

if (c == '%')
expression = !expression;

if (!expression && c == '"')
i++;
}
s = stringBuilder.toString();
} else {
s = orig;
}
String s = orig.replace("\"\"", "\"");

List<Object> string = new ArrayList<>(n / 2 + 2); // List of strings and expressions

Expand Down Expand Up @@ -289,8 +259,7 @@ public static VariableString newInstance(String orig, StringMode mode) {
Object[] sa = string.toArray();
if (string.size() == 1 && string.get(0) instanceof Expression &&
((Expression<?>) string.get(0)).getReturnType() == String.class &&
((Expression<?>) string.get(0)).isSingle() &&
mode == StringMode.MESSAGE) {
((Expression<?>) string.get(0)).isSingle()) {
String expr = ((Expression<?>) string.get(0)).toString(null, false);
Skript.warning(expr + " is already a text, so you should not put it in one (e.g. " + expr + " instead of " + "\"%" + expr.replace("\"", "\"\"") + "%\")");
}
Expand Down Expand Up @@ -638,7 +607,7 @@ public void change(Event e, @Nullable Object[] delta, ChangeMode mode) throws Un

@Override
public boolean getAnd() {
return true;
return false;
}

@Override
Expand Down

This file was deleted.

9 changes: 2 additions & 7 deletions src/test/skript/tests/syntaxes/expressions/ExprEntities.sk
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
test "entities in chunk":
spawn 10 sheep at spawn of world "world"
wait 1 tick
assert size of all entities in chunk at spawn of world "world" >= 10 with "Size of all entities in spawn chunk is not > 10: %size of all entities in chunk at spawn of world "world"%"

loop all entities in chunk at spawn of world "world":
add loop-entity to {_e::*}
assert size of {_e::*} >= 10 with "Size of all entities in spawn chunk is not > 10 (iterating): %size of {_e::*}%"

assert size of all entities in chunk at spawn of world "world" >= 10 with "Size of all entities in spawn chunk is not > 10: %size of all entities in chunk at spawn of world ""world""%"
delete all entities in chunk at spawn of world "world"
assert size of all entities in chunk at spawn of world "world" = 0 with "Size of all entities in spawn chunk != 0: %size of all entities in chunk at spawn of world "world"%"
assert size of all entities in chunk at spawn of world "world" = 0 with "Size of all entities in spawn chunk != 0: %size of all entities in chunk at spawn of world ""world""%"

0 comments on commit 7aa9a8b

Please sign in to comment.