diff --git a/src/main/java/com/mojang/brigadier/ImmutableStringReader.java b/src/main/java/com/mojang/brigadier/ImmutableStringReader.java index e605623..6e1c808 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(); @@ -25,4 +27,6 @@ public interface ImmutableStringReader { 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 4e1392e..13b4d9a 100644 --- a/src/main/java/com/mojang/brigadier/StringReader.java +++ b/src/main/java/com/mojang/brigadier/StringReader.java @@ -5,6 +5,8 @@ package com.mojang.brigadier; 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 = '"'; @@ -81,6 +83,11 @@ public class StringReader implements ImmutableStringReader { return canRead() && peek() == c; } + @Override + public boolean isNext(Predicate predicate) { + return canRead() && predicate.test(peek()); + } + public char read() { return string.charAt(cursor++); } @@ -98,14 +105,14 @@ public class StringReader implements ImmutableStringReader { } 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); @@ -122,7 +129,7 @@ public class StringReader implements ImmutableStringReader { 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); @@ -139,7 +146,7 @@ public class StringReader implements ImmutableStringReader { 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); @@ -156,7 +163,7 @@ public class StringReader implements ImmutableStringReader { 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); @@ -181,7 +188,7 @@ public class StringReader implements ImmutableStringReader { public String readUnquotedString() { final int start = cursor; - while (canRead() && isAllowedInUnquotedString(peek())) { + while (isNext(StringReader::isAllowedInUnquotedString)) { skip(); } return string.substring(start, cursor); diff --git a/src/test/java/com/mojang/brigadier/StringReaderTest.java b/src/test/java/com/mojang/brigadier/StringReaderTest.java index bf08bb5..7ee7fce 100644 --- a/src/test/java/com/mojang/brigadier/StringReaderTest.java +++ b/src/test/java/com/mojang/brigadier/StringReaderTest.java @@ -6,6 +6,8 @@ package com.mojang.brigadier; 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; @@ -587,7 +589,7 @@ public class StringReaderTest { } @Test - public void isAt() { + public void isNext() { final StringReader reader = new StringReader("abc"); assertThat(reader.isNext('a'), is(true)); assertThat(reader.isNext('x'), is(false)); @@ -596,4 +598,15 @@ public class StringReaderTest { 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