Add isAt(Predicate<Character>) method to ImmutableStringReader and StringReader
This commit is contained in:
parent
11758168ba
commit
6d62cc1cb9
3 changed files with 31 additions and 7 deletions
|
@ -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<Character> predicate);
|
||||
}
|
||||
|
|
|
@ -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<Character> 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);
|
||||
|
|
|
@ -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<Character> 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));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue