mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
Adds append(long) and valueOf(long) implementation
This commit is contained in:
parent
9faf252a8d
commit
1051d59f8f
|
@ -8,6 +8,7 @@ import org.junit.Test;
|
|||
* @author Alexey Andreev
|
||||
*/
|
||||
public class StringBuilderTests {
|
||||
@Test
|
||||
public void integerAppended() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(23);
|
||||
|
@ -35,6 +36,34 @@ public class StringBuilderTests {
|
|||
assertEquals("2147483647", sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void longAppended() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(23L);
|
||||
assertEquals("23", sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void negativeLongAppended() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(-23L);
|
||||
assertEquals("-23", sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void largeLongAppended() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(12345678901234L);
|
||||
assertEquals("12345678901234", sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void maxLongAppended() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(9223372036854775807L);
|
||||
assertEquals("9223372036854775807", sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void appendsCodePoint() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
|
|
@ -67,6 +67,43 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
|
|||
return this;
|
||||
}
|
||||
|
||||
protected TAbstractStringBuilder append(long value) {
|
||||
boolean positive = true;
|
||||
if (value < 0) {
|
||||
positive = false;
|
||||
value = -value;
|
||||
}
|
||||
if (value < 10) {
|
||||
if (!positive) {
|
||||
ensureCapacity(length + 2);
|
||||
buffer[length++] = '-';
|
||||
} else {
|
||||
ensureCapacity(length + 1);
|
||||
}
|
||||
buffer[length++] = (char)('0' + value);
|
||||
} else {
|
||||
int pos = 10;
|
||||
int sz = 1;
|
||||
while (pos < 1000000000000000000L && pos * 10 <= value) {
|
||||
pos *= 10;
|
||||
++sz;
|
||||
}
|
||||
if (!positive) {
|
||||
++sz;
|
||||
}
|
||||
ensureCapacity(length + sz);
|
||||
if (!positive) {
|
||||
buffer[length++] = '-';
|
||||
}
|
||||
while (pos > 0) {
|
||||
buffer[length++] = (char)('0' + value / pos);
|
||||
value %= pos;
|
||||
pos /= 10;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
protected TAbstractStringBuilder append(char c) {
|
||||
ensureCapacity(length + 1);
|
||||
buffer[length++] = c;
|
||||
|
|
|
@ -415,8 +415,12 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
|
|||
return new TString(new char[] { c });
|
||||
}
|
||||
|
||||
public static TString valueOf(int index) {
|
||||
return new TStringBuilder().append(index).toString0();
|
||||
public static TString valueOf(int i) {
|
||||
return new TStringBuilder().append(i).toString0();
|
||||
}
|
||||
|
||||
public static TString valueOf(long l) {
|
||||
return new TStringBuilder().append(l).toString0();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -17,6 +17,12 @@ public class TStringBuilder extends TAbstractStringBuilder {
|
|||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TStringBuilder append(long value) {
|
||||
super.append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TStringBuilder append(char c) {
|
||||
super.append(c);
|
||||
|
|
Loading…
Reference in New Issue
Block a user