diff --git a/src/main/java/com/mojang/brigadier/ImmutableStringReader.java b/src/main/java/com/mojang/brigadier/ImmutableStringReader.java index a347f754..6e1c808d 100644 --- a/src/main/java/com/mojang/brigadier/ImmutableStringReader.java +++ b/src/main/java/com/mojang/brigadier/ImmutableStringReader.java @@ -3,6 +3,8 @@ package com.mojang.brigadier; +import java.util.function.Predicate; + public interface ImmutableStringReader { String getString(); @@ -23,4 +25,8 @@ public interface ImmutableStringReader { char peek(); char peek(int offset); + + boolean isNext(char c); + + boolean isNext(Predicate predicate); } diff --git a/src/main/java/com/mojang/brigadier/StringReader.java b/src/main/java/com/mojang/brigadier/StringReader.java index c1465df6..13b4d9a0 100644 --- a/src/main/java/com/mojang/brigadier/StringReader.java +++ b/src/main/java/com/mojang/brigadier/StringReader.java @@ -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 = '"'; @@ -76,6 +78,16 @@ public char peek(final int offset) { return string.charAt(cursor + offset); } + @Override + public boolean isNext(char c) { + return canRead() && peek() == c; + } + + @Override + public boolean isNext(Predicate predicate) { + return canRead() && predicate.test(peek()); + } + public char read() { return string.charAt(cursor++); } @@ -93,14 +105,14 @@ public static boolean isQuotedStringStart(char c) { } public void skipWhitespace() { - while (canRead() && Character.isWhitespace(peek())) { + while (isNext(Character::isWhitespace)) { skip(); } } public int readInt() throws CommandSyntaxException { final int start = cursor; - while (canRead() && isAllowedNumber(peek())) { + while (isNext(StringReader::isAllowedNumber)) { skip(); } final String number = string.substring(start, cursor); @@ -117,7 +129,7 @@ public int readInt() throws CommandSyntaxException { public long readLong() throws CommandSyntaxException { final int start = cursor; - while (canRead() && isAllowedNumber(peek())) { + while (isNext(StringReader::isAllowedNumber)) { skip(); } final String number = string.substring(start, cursor); @@ -134,7 +146,7 @@ public long readLong() throws CommandSyntaxException { public double readDouble() throws CommandSyntaxException { final int start = cursor; - while (canRead() && isAllowedNumber(peek())) { + while (isNext(StringReader::isAllowedNumber)) { skip(); } final String number = string.substring(start, cursor); @@ -151,7 +163,7 @@ public double readDouble() throws CommandSyntaxException { public float readFloat() throws CommandSyntaxException { final int start = cursor; - while (canRead() && isAllowedNumber(peek())) { + while (isNext(StringReader::isAllowedNumber)) { skip(); } final String number = string.substring(start, cursor); @@ -176,7 +188,7 @@ public static boolean isAllowedInUnquotedString(final char c) { public String readUnquotedString() { final int start = cursor; - while (canRead() && isAllowedInUnquotedString(peek())) { + while (isNext(StringReader::isAllowedInUnquotedString)) { skip(); } return string.substring(start, cursor); @@ -249,7 +261,7 @@ public boolean readBoolean() throws CommandSyntaxException { } public void expect(final char c) throws CommandSyntaxException { - if (!canRead() || peek() != c) { + if (!isNext(c)) { throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedSymbol().createWithContext(this, String.valueOf(c)); } skip(); diff --git a/src/test/java/com/mojang/brigadier/StringReaderTest.java b/src/test/java/com/mojang/brigadier/StringReaderTest.java index 23e2c14d..7ee7fce3 100644 --- a/src/test/java/com/mojang/brigadier/StringReaderTest.java +++ b/src/test/java/com/mojang/brigadier/StringReaderTest.java @@ -6,6 +6,8 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import org.junit.Test; +import java.util.function.Predicate; + import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -585,4 +587,26 @@ public void readBoolean_none() throws Exception { assertThat(ex.getCursor(), is(0)); } } + + @Test + public void isNext() { + final StringReader reader = new StringReader("abc"); + assertThat(reader.isNext('a'), is(true)); + assertThat(reader.isNext('x'), is(false)); + reader.setCursor(2); + assertThat(reader.isNext('c'), is(true)); + reader.skip(); + assertThat(reader.isNext('c'), is(false)); + } + + @Test + public void isNext_predicate() { + final StringReader reader = new StringReader("abc"); + final Predicate predicate = c -> (c == 'a' || c == 'b'); + assertThat(reader.isNext(predicate), is(true)); + reader.setCursor(1); + assertThat(reader.isNext(predicate), is(true)); + reader.setCursor(2); + assertThat(reader.isNext(predicate), is(false)); + } } \ No newline at end of file