mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
classlib: add Integer/Long parse subsequence
This commit is contained in:
parent
81f78fab01
commit
73d2379185
|
@ -16,6 +16,7 @@
|
||||||
package org.teavm.classlib.java.lang;
|
package org.teavm.classlib.java.lang;
|
||||||
|
|
||||||
import static org.teavm.classlib.impl.IntegerUtil.toUnsignedLogRadixString;
|
import static org.teavm.classlib.impl.IntegerUtil.toUnsignedLogRadixString;
|
||||||
|
import java.util.Objects;
|
||||||
import org.teavm.backend.javascript.spi.InjectedBy;
|
import org.teavm.backend.javascript.spi.InjectedBy;
|
||||||
import org.teavm.interop.NoSideEffects;
|
import org.teavm.interop.NoSideEffects;
|
||||||
|
|
||||||
|
@ -63,42 +64,56 @@ public class TInteger extends TNumber implements TComparable<TInteger> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int parseInt(String s, int radix) throws TNumberFormatException {
|
public static int parseInt(String s, int radix) throws TNumberFormatException {
|
||||||
|
if (s == null) {
|
||||||
|
throw new TNumberFormatException("String is null");
|
||||||
|
}
|
||||||
|
return parseIntImpl(s, 0, s.length(), radix);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix) throws TNumberFormatException {
|
||||||
|
return parseIntImpl(Objects.requireNonNull(s), beginIndex, endIndex, radix);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int parseIntImpl(CharSequence s, int beginIndex, int endIndex, int radix)
|
||||||
|
throws TNumberFormatException {
|
||||||
|
if (beginIndex == endIndex) {
|
||||||
|
throw new TNumberFormatException("String is empty");
|
||||||
|
}
|
||||||
if (radix < TCharacter.MIN_RADIX || radix > TCharacter.MAX_RADIX) {
|
if (radix < TCharacter.MIN_RADIX || radix > TCharacter.MAX_RADIX) {
|
||||||
throw new TNumberFormatException("Illegal radix: " + radix);
|
throw new TNumberFormatException("Illegal radix: " + radix);
|
||||||
}
|
}
|
||||||
if (s == null || s.isEmpty()) {
|
|
||||||
throw new TNumberFormatException("String is null or empty");
|
|
||||||
}
|
|
||||||
boolean negative = false;
|
boolean negative = false;
|
||||||
int index = 0;
|
int index = beginIndex;
|
||||||
switch (s.charAt(0)) {
|
switch (s.charAt(index)) {
|
||||||
case '-':
|
case '-':
|
||||||
negative = true;
|
negative = true;
|
||||||
index = 1;
|
index++;
|
||||||
break;
|
break;
|
||||||
case '+':
|
case '+':
|
||||||
index = 1;
|
index++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
int value = 0;
|
int value = 0;
|
||||||
if (index == s.length()) {
|
if (index == endIndex) {
|
||||||
throw new TNumberFormatException();
|
throw new TNumberFormatException();
|
||||||
}
|
}
|
||||||
while (index < s.length()) {
|
while (index < endIndex) {
|
||||||
int digit = TCharacter.getNumericValue(s.charAt(index++));
|
int digit = TCharacter.getNumericValue(s.charAt(index++));
|
||||||
if (digit < 0) {
|
if (digit < 0) {
|
||||||
throw new TNumberFormatException("String contains invalid digits: " + s);
|
throw new TNumberFormatException("String contains invalid digits: "
|
||||||
|
+ s.subSequence(beginIndex, endIndex));
|
||||||
}
|
}
|
||||||
if (digit >= radix) {
|
if (digit >= radix) {
|
||||||
throw new TNumberFormatException("String contains digits out of radix " + radix
|
throw new TNumberFormatException("String contains digits out of radix " + radix + ": "
|
||||||
+ ": " + s);
|
+ s.subSequence(beginIndex, endIndex));
|
||||||
}
|
}
|
||||||
value = radix * value + digit;
|
value = radix * value + digit;
|
||||||
if (value < 0) {
|
if (value < 0) {
|
||||||
if (index == s.length() && value == MIN_VALUE && negative) {
|
if (index == endIndex && value == MIN_VALUE && negative) {
|
||||||
return MIN_VALUE;
|
return MIN_VALUE;
|
||||||
}
|
}
|
||||||
throw new TNumberFormatException("The value is too big for int type: " + s);
|
throw new TNumberFormatException("The value is too big for int type: "
|
||||||
|
+ s.subSequence(beginIndex, endIndex));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return negative ? -value : value;
|
return negative ? -value : value;
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
package org.teavm.classlib.java.lang;
|
package org.teavm.classlib.java.lang;
|
||||||
|
|
||||||
import static org.teavm.classlib.impl.IntegerUtil.toUnsignedLogRadixString;
|
import static org.teavm.classlib.impl.IntegerUtil.toUnsignedLogRadixString;
|
||||||
|
import java.util.Objects;
|
||||||
import org.teavm.backend.javascript.spi.GeneratedBy;
|
import org.teavm.backend.javascript.spi.GeneratedBy;
|
||||||
import org.teavm.interop.NoSideEffects;
|
import org.teavm.interop.NoSideEffects;
|
||||||
|
|
||||||
|
@ -39,39 +40,54 @@ public class TLong extends TNumber implements TComparable<TLong> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long parseLong(String s, int radix) throws TNumberFormatException {
|
public static long parseLong(String s, int radix) throws TNumberFormatException {
|
||||||
|
if (s == null) {
|
||||||
|
throw new TNumberFormatException("String is null");
|
||||||
|
}
|
||||||
|
return parseLongImpl(s, 0, s.length(), radix);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long parseLong(CharSequence s, int beginIndex, int endIndex, int radix)
|
||||||
|
throws TNumberFormatException {
|
||||||
|
return parseLongImpl(Objects.requireNonNull(s), beginIndex, endIndex, radix);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long parseLongImpl(CharSequence s, int beginIndex, int endIndex, int radix)
|
||||||
|
throws TNumberFormatException {
|
||||||
if (radix < TCharacter.MIN_RADIX || radix > TCharacter.MAX_RADIX) {
|
if (radix < TCharacter.MIN_RADIX || radix > TCharacter.MAX_RADIX) {
|
||||||
throw new TNumberFormatException("Illegal radix: " + radix);
|
throw new TNumberFormatException("Illegal radix: " + radix);
|
||||||
}
|
}
|
||||||
if (s == null || s.isEmpty()) {
|
if (beginIndex == endIndex) {
|
||||||
throw new TNumberFormatException("String is null or empty");
|
throw new TNumberFormatException("String is empty");
|
||||||
}
|
}
|
||||||
boolean negative = false;
|
boolean negative = false;
|
||||||
int index = 0;
|
int index = beginIndex;
|
||||||
switch (s.charAt(0)) {
|
switch (s.charAt(index)) {
|
||||||
case '-':
|
case '-':
|
||||||
negative = true;
|
negative = true;
|
||||||
index = 1;
|
index++;
|
||||||
break;
|
break;
|
||||||
case '+':
|
case '+':
|
||||||
index = 1;
|
index++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
long value = 0;
|
long value = 0;
|
||||||
while (index < s.length()) {
|
while (index < endIndex) {
|
||||||
int digit = TCharacter.getNumericValue(s.charAt(index++));
|
int digit = TCharacter.getNumericValue(s.charAt(index++));
|
||||||
if (digit < 0) {
|
if (digit < 0) {
|
||||||
throw new TNumberFormatException("String contains invalid digits: " + s);
|
throw new TNumberFormatException("String contains invalid digits: "
|
||||||
|
+ s.subSequence(beginIndex, endIndex));
|
||||||
}
|
}
|
||||||
if (digit >= radix) {
|
if (digit >= radix) {
|
||||||
throw new TNumberFormatException("String contains digits out of radix " + radix
|
throw new TNumberFormatException("String contains digits out of radix " + radix + ": "
|
||||||
+ ": " + s);
|
+ s.subSequence(beginIndex, endIndex));
|
||||||
}
|
}
|
||||||
value = radix * value + digit;
|
value = radix * value + digit;
|
||||||
if (value < 0) {
|
if (value < 0) {
|
||||||
if (index == s.length() && value == MIN_VALUE && negative) {
|
if (index == endIndex && value == MIN_VALUE && negative) {
|
||||||
return MIN_VALUE;
|
return MIN_VALUE;
|
||||||
}
|
}
|
||||||
throw new TNumberFormatException("The value is too big for int type: " + s);
|
throw new TNumberFormatException("The value is too big for int type: "
|
||||||
|
+ s.subSequence(beginIndex, endIndex));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return negative ? -value : value;
|
return negative ? -value : value;
|
||||||
|
|
|
@ -38,6 +38,14 @@ public class IntegerTest {
|
||||||
assertEquals(411787, Integer.parseInt("Kona", 27));
|
assertEquals(411787, Integer.parseInt("Kona", 27));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parsesIntegerInSubstring() {
|
||||||
|
assertEquals(0, Integer.parseInt("[0]", 1, 2, 10));
|
||||||
|
assertEquals(473, Integer.parseInt("[473]", 1, 4, 10));
|
||||||
|
assertEquals(42, Integer.parseInt("[+42]", 1, 4, 10));
|
||||||
|
assertEquals(-255, Integer.parseInt("[-FF]", 1, 4, 16));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parsesMinInteger() {
|
public void parsesMinInteger() {
|
||||||
assertEquals(-2147483648, Integer.parseInt("-2147483648", 10));
|
assertEquals(-2147483648, Integer.parseInt("-2147483648", 10));
|
||||||
|
@ -141,7 +149,6 @@ public class IntegerTest {
|
||||||
assertTrue(Integer.compare(Integer.MIN_VALUE, Integer.MAX_VALUE) < 0);
|
assertTrue(Integer.compare(Integer.MIN_VALUE, Integer.MAX_VALUE) < 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getFromSystemProperty() {
|
public void getFromSystemProperty() {
|
||||||
System.setProperty("test.foo", "23");
|
System.setProperty("test.foo", "23");
|
||||||
|
|
|
@ -21,9 +21,20 @@ import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.teavm.junit.SkipJVM;
|
import org.teavm.junit.SkipJVM;
|
||||||
import org.teavm.junit.TeaVMTestRunner;
|
import org.teavm.junit.TeaVMTestRunner;
|
||||||
|
import org.teavm.junit.WholeClassCompilation;
|
||||||
|
|
||||||
@RunWith(TeaVMTestRunner.class)
|
@RunWith(TeaVMTestRunner.class)
|
||||||
|
@WholeClassCompilation
|
||||||
public class LongTest {
|
public class LongTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parsesLongInSubstring() {
|
||||||
|
assertEquals(0, Long.parseLong("[0]", 1, 2, 10));
|
||||||
|
assertEquals(473, Long.parseLong("[473]", 1, 4, 10));
|
||||||
|
assertEquals(42, Long.parseLong("[+42]", 1, 4, 10));
|
||||||
|
assertEquals(-255, Long.parseLong("[-FF]", 1, 4, 16));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void compares() {
|
public void compares() {
|
||||||
assertTrue(Long.compare(10, 5) > 0);
|
assertTrue(Long.compare(10, 5) > 0);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user