mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
Adds some java.lang.String methods
This commit is contained in:
parent
3f1a44eccb
commit
626635daeb
|
@ -1,6 +1,6 @@
|
|||
package org.teavm.classlib.java.lang;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
|
@ -21,4 +21,60 @@ public class StringTests {
|
|||
String str = "123";
|
||||
assertEquals(3, str.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringCreatedFromChars() {
|
||||
String str = new String(new char[] { '1', '2', '3' });
|
||||
assertEquals("123", str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringsAreEqual() {
|
||||
String a = new String(new char[] { '1', '2', '3' });
|
||||
String b = new String(new char[] { '1', '2', '3' });
|
||||
assertEquals(a, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringsAreNotEqual() {
|
||||
String a = new String(new char[] { '1', '2', '3' });
|
||||
String b = new String(new char[] { '1', '4', '3' });
|
||||
assertNotEquals(a, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringCharactersRead() {
|
||||
char[] buffer = new char[4];
|
||||
"123".getChars(0, 3, buffer, 0);
|
||||
assertEquals('1', buffer[0]);
|
||||
assertEquals('2', buffer[1]);
|
||||
assertEquals('3', buffer[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringIsEqualToBuilder() {
|
||||
assertTrue("123".contentEquals(new StringBuilder().append('1').append('2').append('3')));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void comparesSameStrings() {
|
||||
String a = "123";
|
||||
String b = new String(a);
|
||||
assertTrue(a.compareTo(b) == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void comparesToPrecedingStrings() {
|
||||
assertTrue("abc".compareTo("abbc") > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void comparesToSuccessorStrings() {
|
||||
assertTrue("abc".compareTo("abdc") < 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startsWithWorks() {
|
||||
assertTrue("123".startsWith("12"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,9 +8,9 @@ import org.teavm.javascript.ni.Rename;
|
|||
*
|
||||
* @author Alexey Andreev
|
||||
*/
|
||||
class TAbstractStringBuilder extends TObject implements TSerializable {
|
||||
private char[] buffer;
|
||||
private int length;
|
||||
class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequence {
|
||||
char[] buffer;
|
||||
int length;
|
||||
|
||||
public TAbstractStringBuilder() {
|
||||
this(16);
|
||||
|
@ -85,4 +85,23 @@ class TAbstractStringBuilder extends TObject implements TSerializable {
|
|||
public TString toString0() {
|
||||
return new TString(buffer, 0, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
return length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public char charAt(int index) {
|
||||
if (index < 0 || index >= length) {
|
||||
throw new TIndexOutOfBoundsException();
|
||||
}
|
||||
return buffer[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public TCharSequence subSequence(int start, int end) {
|
||||
// TODO: implement
|
||||
throw new TUnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package org.teavm.classlib.java.lang;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev
|
||||
*/
|
||||
public interface TCharSequence {
|
||||
int length();
|
||||
|
||||
char charAt(int index);
|
||||
|
||||
TCharSequence subSequence(int start, int end);
|
||||
|
||||
TString toString0();
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package org.teavm.classlib.java.lang;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev
|
||||
*/
|
||||
public interface TComparable<T extends TComparable<T>> {
|
||||
int compareTo(T other);
|
||||
}
|
|
@ -7,7 +7,7 @@ import org.teavm.javascript.ni.GeneratedBy;
|
|||
*
|
||||
* @author Alexey Andreev <konsoletyper@gmail.com>
|
||||
*/
|
||||
public class TString extends TObject implements TSerializable {
|
||||
public class TString extends TObject implements TSerializable, TComparable<TString> {
|
||||
private char[] characters;
|
||||
private transient int hashCode;
|
||||
|
||||
|
@ -33,6 +33,10 @@ public class TString extends TObject implements TSerializable {
|
|||
}
|
||||
}
|
||||
|
||||
public TString(TStringBuilder sb) {
|
||||
this(sb.buffer, 0, sb.length);
|
||||
}
|
||||
|
||||
public char charAt(int index) {
|
||||
if (index < 0 || index >= characters.length) {
|
||||
throw new StringIndexOutOfBoundsException(null);
|
||||
|
@ -44,6 +48,67 @@ public class TString extends TObject implements TSerializable {
|
|||
return characters.length;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return characters.length == 0;
|
||||
}
|
||||
|
||||
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
|
||||
if (srcBegin < 0 || srcBegin > srcEnd || srcEnd > length() || dstBegin < 0 ||
|
||||
dstBegin + (srcEnd - srcBegin) > dst.length) {
|
||||
throw new TIndexOutOfBoundsException();
|
||||
}
|
||||
while (srcBegin < srcEnd) {
|
||||
dst[dstBegin++] = charAt(srcBegin++);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean contentEquals(TCharSequence charSeq) {
|
||||
if (this == charSeq) {
|
||||
return true;
|
||||
}
|
||||
if (characters.length != charSeq.length()) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < characters.length; ++i) {
|
||||
if (characters[i] != charSeq.charAt(i)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(TString anotherString) {
|
||||
if (this == anotherString) {
|
||||
return 0;
|
||||
}
|
||||
int l = TMath.min(length(), anotherString.length());
|
||||
for (int i = 0; i < l; ++i) {
|
||||
char a = charAt(i);
|
||||
char b = anotherString.charAt(i);
|
||||
if (a - b != 0) {
|
||||
return a - b;
|
||||
}
|
||||
}
|
||||
return length() - anotherString.length();
|
||||
}
|
||||
|
||||
public boolean startsWith(TString prefix, int toffset) {
|
||||
if (toffset + prefix.length() > length()) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < prefix.length(); ++i) {
|
||||
if (prefix.charAt(i) != charAt(toffset++)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean startsWith(TString prefix) {
|
||||
return startsWith(prefix, 0);
|
||||
}
|
||||
|
||||
public static TString valueOf(int index) {
|
||||
return new TStringBuilder().append(index).toString0();
|
||||
}
|
||||
|
|
|
@ -16,4 +16,10 @@ public class TStringBuilder extends TAbstractStringBuilder {
|
|||
super.append(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TStringBuilder append(char c) {
|
||||
super.append(c);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package org.teavm.classlib.java.lang;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev
|
||||
*/
|
||||
public class TUnsupportedOperationException extends TRuntimeException {
|
||||
private static final long serialVersionUID = 3820374864664609707L;
|
||||
|
||||
public TUnsupportedOperationException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TUnsupportedOperationException(TString message, TThrowable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public TUnsupportedOperationException(TString message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public TUnsupportedOperationException(TThrowable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user