Implements som java.lang.String methods

This commit is contained in:
Alexey Andreev 2013-12-05 17:46:57 +04:00
parent 4767ab07cb
commit d05b9434d9
4 changed files with 112 additions and 0 deletions

View File

@ -164,4 +164,30 @@ public class StringTests {
public void replacesCharacter() {
assertEquals("abbdbbd", "abcdbcd".replace('c', 'b'));
}
@Test
public void containsWorks() {
assertTrue("abcd".contains("bc"));
}
@Test
public void sequenceReplaced() {
assertEquals("ba", "aaa".replace("aa", "b"));
}
@Test
public void trimWorks() {
assertEquals("ab", " ab ".trim());
assertEquals("ab", "ab".trim());
assertEquals("", " ".trim());
}
@Test
public void convertedToCharArray() {
char[] array = "123".toCharArray();
assertEquals(3, array.length);
assertEquals('1', array[0]);
assertEquals('2', array[1]);
assertEquals('3', array[2]);
}
}

View File

@ -110,6 +110,21 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
return buffer[index];
}
public TAbstractStringBuilder append(TCharSequence s, int start, int end) {
if (start > end || end > s.length() || start < 0) {
throw new TIndexOutOfBoundsException();
}
ensureCapacity(end - start);
for (int i = start; i < end; ++i) {
buffer[length++] = s.charAt(i);
}
return this;
}
public TAbstractStringBuilder append(TCharSequence s) {
return append(s, 0, s.length());
}
@Override
public TCharSequence subSequence(int start, int end) {
// TODO: implement

View File

@ -2,6 +2,7 @@ package org.teavm.classlib.java.lang;
import org.teavm.classlib.java.lang.io.TSerializable;
import org.teavm.javascript.ni.GeneratedBy;
import org.teavm.javascript.ni.Rename;
/**
*
@ -328,10 +329,68 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
return new TString(buffer);
}
public boolean contains(TCharSequence s) {
outer:
for (int i = 0; i < length(); ++i) {
for (int j = 0; j < s.length(); ++j) {
if (charAt(i + j) != s.charAt(j)) {
continue outer;
}
}
return true;
}
return false;
}
public TString replace(TCharSequence target, TCharSequence replacement) {
TStringBuilder sb = new TStringBuilder();
int sz = length() - target.length();
int i = 0;
outer:
for (; i < sz; ++i) {
for (int j = 0; j < target.length(); ++j) {
if (charAt(i + j) != target.charAt(j)) {
sb.append(charAt(i));
continue outer;
}
}
sb.append(replacement);
i += target.length() - 1;
}
sb.append(substring(i));
return TString.wrap(sb.toString());
}
public TString trim() {
int lower = 0;
int upper = length() - 1;
while (lower <= upper && charAt(lower) <= ' ') {
++lower;
}
while (lower <= upper && charAt(upper) <= ' ') {
--upper;
}
return substring(lower, upper + 1);
}
public static TString valueOf(int index) {
return new TStringBuilder().append(index).toString0();
}
@Override
@Rename("toString")
public TString toString0() {
return this;
}
public char[] toCharArray() {
char[] array = new char[characters.length];
for (int i = 0; i < array.length; ++i) {
array[i] = characters[i];
}
return array;
}
@Override
public boolean equals(TObject other) {
if (this == other) {

View File

@ -28,4 +28,16 @@ public class TStringBuilder extends TAbstractStringBuilder {
super.appendCodePoint(codePoint);
return this;
}
@Override
public TStringBuilder append(TCharSequence s, int start, int end) {
super.append(s, start, end);
return this;
}
@Override
public TStringBuilder append(TCharSequence s) {
super.append(s);
return this;
}
}