Added readFloat to StringReader

This commit is contained in:
Nathan Adams 2017-11-16 14:28:21 +01:00
parent 1b32233044
commit 647f859b63
3 changed files with 83 additions and 2 deletions

View file

@ -16,6 +16,8 @@ public class StringReader implements ImmutableStringReader {
public static final SimpleCommandExceptionType ERROR_EXPECTED_INT = new SimpleCommandExceptionType("parsing.int.expected", "Expected integer");
public static final ParameterizedCommandExceptionType ERROR_INVALID_DOUBLE = new ParameterizedCommandExceptionType("parsing.double.invalid", "Invalid double '${value}'", "value");
public static final SimpleCommandExceptionType ERROR_EXPECTED_DOUBLE = new SimpleCommandExceptionType("parsing.double.expected", "Expected double");
public static final ParameterizedCommandExceptionType ERROR_INVALID_FLOAT = new ParameterizedCommandExceptionType("parsing.float.invalid", "Invalid float '${value}'", "value");
public static final SimpleCommandExceptionType ERROR_EXPECTED_FLOAT = new SimpleCommandExceptionType("parsing.float.expected", "Expected float");
public static final SimpleCommandExceptionType ERROR_EXPECTED_BOOL = new SimpleCommandExceptionType("parsing.bool.expected", "Expected bool");
public static final ParameterizedCommandExceptionType ERROR_EXPECTED_SYMBOL = new ParameterizedCommandExceptionType("parsing.expected", "Expected '${symbol}'", "symbol");
@ -93,7 +95,7 @@ public class StringReader implements ImmutableStringReader {
cursor++;
}
private static boolean isAllowedNumber(final char c) {
public static boolean isAllowedNumber(final char c) {
return c >= '0' && c <= '9' || c == '.' || c == '-';
}
@ -137,6 +139,23 @@ public class StringReader implements ImmutableStringReader {
}
}
public float readFloat() throws CommandSyntaxException {
final int start = cursor;
while (canRead() && isAllowedNumber(peek())) {
skip();
}
final String number = string.substring(start, cursor);
if (number.isEmpty()) {
throw ERROR_EXPECTED_FLOAT.createWithContext(this);
}
try {
return Float.parseFloat(number);
} catch (final NumberFormatException ex) {
cursor = start;
throw ERROR_INVALID_FLOAT.createWithContext(this, number);
}
}
public static boolean isAllowedInUnquotedString(final char c) {
return c >= '0' && c <= '9'
|| c >= 'A' && c <= 'Z'

View file

@ -45,7 +45,7 @@ public class FloatArgumentType implements ArgumentType<Float> {
@Override
public <S> Float parse(final StringReader reader, final CommandContextBuilder<S> contextBuilder) throws CommandSyntaxException {
final int start = reader.getCursor();
final float result = (float) reader.readDouble();
final float result = reader.readFloat();
if (result < minimum) {
reader.setCursor(start);
throw ERROR_TOO_SMALL.createWithContext(reader, result, minimum);

View file

@ -361,6 +361,68 @@ public class StringReaderTest {
assertThat(reader.getRemaining(), equalTo("foo bar"));
}
@Test
public void readFloat() throws Exception {
final StringReader reader = new StringReader("123");
assertThat(reader.readFloat(), is(123.0f));
assertThat(reader.getRead(), equalTo("123"));
assertThat(reader.getRemaining(), equalTo(""));
}
@Test
public void readFloat_withDecimal() throws Exception {
final StringReader reader = new StringReader("12.34");
assertThat(reader.readFloat(), is(12.34f));
assertThat(reader.getRead(), equalTo("12.34"));
assertThat(reader.getRemaining(), equalTo(""));
}
@Test
public void readFloat_negative() throws Exception {
final StringReader reader = new StringReader("-123");
assertThat(reader.readFloat(), is(-123.0f));
assertThat(reader.getRead(), equalTo("-123"));
assertThat(reader.getRemaining(), equalTo(""));
}
@Test
public void readFloat_invalid() throws Exception {
try {
new StringReader("12.34.56").readFloat();
} catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_INVALID_FLOAT));
assertThat(ex.getData(), equalTo(ImmutableMap.of("value", "12.34.56")));
assertThat(ex.getCursor(), is(0));
}
}
@Test
public void readFloat_none() throws Exception {
try {
new StringReader("").readFloat();
} catch (final CommandSyntaxException ex) {
assertThat(ex.getType(), is(StringReader.ERROR_EXPECTED_FLOAT));
assertThat(ex.getData(), equalTo(Collections.emptyMap()));
assertThat(ex.getCursor(), is(0));
}
}
@Test
public void readFloat_withRemaining() throws Exception {
final StringReader reader = new StringReader("12.34 foo bar");
assertThat(reader.readFloat(), is(12.34f));
assertThat(reader.getRead(), equalTo("12.34"));
assertThat(reader.getRemaining(), equalTo(" foo bar"));
}
@Test
public void readFloat_withRemainingImmediate() throws Exception {
final StringReader reader = new StringReader("12.34foo bar");
assertThat(reader.readFloat(), is(12.34f));
assertThat(reader.getRead(), equalTo("12.34"));
assertThat(reader.getRemaining(), equalTo("foo bar"));
}
@Test
public void expect_correct() throws Exception {
final StringReader reader = new StringReader("abc");