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

Allow custom predicate that replaces StringReader#isAllowedInUnquotedString #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/main/java/com/mojang/brigadier/StringReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.mojang.brigadier.exceptions.CommandSyntaxException;

import java.util.function.Predicate;

public class StringReader implements ImmutableStringReader {
private static final char SYNTAX_ESCAPE = '\\';
private static final char SYNTAX_DOUBLE_QUOTE = '"';
Expand Down Expand Up @@ -175,8 +177,12 @@ public static boolean isAllowedInUnquotedString(final char c) {
}

public String readUnquotedString() {
return readUnquotedString(StringReader::isAllowedInUnquotedString);
}

public String readUnquotedString(Predicate<Character> allowPredicate) {
final int start = cursor;
while (canRead() && isAllowedInUnquotedString(peek())) {
while (canRead() && allowPredicate.test(peek())) {
skip();
}
return string.substring(start, cursor);
Expand Down Expand Up @@ -220,6 +226,10 @@ public String readStringUntil(char terminator) throws CommandSyntaxException {
}

public String readString() throws CommandSyntaxException {
return readString(StringReader::isAllowedInUnquotedString);
}

public String readString(Predicate<Character> allowPredicate) throws CommandSyntaxException {
if (!canRead()) {
return "";
}
Expand All @@ -228,7 +238,7 @@ public String readString() throws CommandSyntaxException {
skip();
return readStringUntil(next);
}
return readUnquotedString();
return readUnquotedString(allowPredicate);
}

public boolean readBoolean() throws CommandSyntaxException {
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/mojang/brigadier/StringReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ public void readUnquotedString() throws Exception {
assertThat(reader.getRemaining(), equalTo(" world"));
}

@Test
public void readUnquotedStringCustomPredicate() throws Exception {
final StringReader reader = new StringReader("你好 世界");
assertThat(reader.readUnquotedString(c -> c >= '一' && c <= '龥'), equalTo("你好"));
assertThat(reader.getRead(), equalTo("你好"));
assertThat(reader.getRemaining(), equalTo(" 世界"));
}

@Test
public void readUnquotedString_empty() throws Exception {
final StringReader reader = new StringReader("");
Expand Down Expand Up @@ -300,6 +308,14 @@ public void readString_doubleQuotes() throws Exception {
assertThat(reader.getRemaining(), equalTo(""));
}

@Test
public void readString_customPredicate_noQuotes() throws Exception {
final StringReader reader = new StringReader("你好 世界");
assertThat(reader.readString(c -> c >= '一' && c <= '龥'), equalTo("你好"));
assertThat(reader.getRead(), equalTo("你好"));
assertThat(reader.getRemaining(), equalTo(" 世界"));
}

@Test
public void readInt() throws Exception {
final StringReader reader = new StringReader("1234567890");
Expand Down