Adds append(long) and valueOf(long) implementation

This commit is contained in:
Alexey Andreev 2013-12-13 16:33:22 +04:00
parent 9faf252a8d
commit 1051d59f8f
4 changed files with 78 additions and 2 deletions

View File

@ -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();

View File

@ -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;

View File

@ -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

View File

@ -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);