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 * @author Alexey Andreev
*/ */
public class StringBuilderTests { public class StringBuilderTests {
@Test
public void integerAppended() { public void integerAppended() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(23); sb.append(23);
@ -35,6 +36,34 @@ public class StringBuilderTests {
assertEquals("2147483647", sb.toString()); 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 @Test
public void appendsCodePoint() { public void appendsCodePoint() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();

View File

@ -67,6 +67,43 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
return this; 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) { protected TAbstractStringBuilder append(char c) {
ensureCapacity(length + 1); ensureCapacity(length + 1);
buffer[length++] = c; buffer[length++] = c;

View File

@ -415,8 +415,12 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
return new TString(new char[] { c }); return new TString(new char[] { c });
} }
public static TString valueOf(int index) { public static TString valueOf(int i) {
return new TStringBuilder().append(index).toString0(); return new TStringBuilder().append(i).toString0();
}
public static TString valueOf(long l) {
return new TStringBuilder().append(l).toString0();
} }
@Override @Override

View File

@ -17,6 +17,12 @@ public class TStringBuilder extends TAbstractStringBuilder {
return this; return this;
} }
@Override
public TStringBuilder append(long value) {
super.append(value);
return this;
}
@Override @Override
public TStringBuilder append(char c) { public TStringBuilder append(char c) {
super.append(c); super.append(c);