Add isAt method to ImmutableStringReader and StringReader
This commit is contained in:
parent
cf754c4ef6
commit
a048014b3f
3 changed files with 19 additions and 1 deletions
src
main/java/com/mojang/brigadier
test/java/com/mojang/brigadier
|
@ -23,4 +23,6 @@ public interface ImmutableStringReader {
|
|||
char peek();
|
||||
|
||||
char peek(int offset);
|
||||
|
||||
boolean isAt(char c);
|
||||
}
|
||||
|
|
|
@ -76,6 +76,11 @@ public class StringReader implements ImmutableStringReader {
|
|||
return string.charAt(cursor + offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAt(char c) {
|
||||
return canRead() && peek() == c;
|
||||
}
|
||||
|
||||
public char read() {
|
||||
return string.charAt(cursor++);
|
||||
}
|
||||
|
@ -249,7 +254,7 @@ public class StringReader implements ImmutableStringReader {
|
|||
}
|
||||
|
||||
public void expect(final char c) throws CommandSyntaxException {
|
||||
if (!canRead() || peek() != c) {
|
||||
if (!isAt(c)) {
|
||||
throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedSymbol().createWithContext(this, String.valueOf(c));
|
||||
}
|
||||
skip();
|
||||
|
|
|
@ -585,4 +585,15 @@ public class StringReaderTest {
|
|||
assertThat(ex.getCursor(), is(0));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAt() {
|
||||
final StringReader reader = new StringReader("abc");
|
||||
assertThat(reader.isAt('a'), is(true));
|
||||
assertThat(reader.isAt('x'), is(false));
|
||||
reader.setCursor(2);
|
||||
assertThat(reader.isAt('c'), is(true));
|
||||
reader.skip();
|
||||
assertThat(reader.isAt('c'), is(false));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue