mirror of
https://github.com/corda/corda.git
synced 2025-06-13 12:48:18 +00:00
flesh out some classpath classes
This commit is contained in:
@ -1,28 +1,27 @@
|
||||
package java.lang;
|
||||
|
||||
public final class String {
|
||||
public final class String implements Comparable<String> {
|
||||
private Object data;
|
||||
private int offset;
|
||||
private int length;
|
||||
private int hash;
|
||||
|
||||
public String(char[] data, int offset, int length, boolean copy) {
|
||||
if (copy) {
|
||||
char[] c = new char[length];
|
||||
System.arraycopy(data, offset, c, 0, length);
|
||||
|
||||
this.data = c;
|
||||
this.length = length;
|
||||
} else {
|
||||
this.data = data;
|
||||
this.offset = offset;
|
||||
this.length = length;
|
||||
}
|
||||
this((Object) data, offset, length, copy);
|
||||
}
|
||||
|
||||
public String(byte[] data, int offset, int length, boolean copy) {
|
||||
this((Object) data, offset, length, copy);
|
||||
}
|
||||
|
||||
private String(Object data, int offset, int length, boolean copy) {
|
||||
if (copy) {
|
||||
byte[] c = new byte[length];
|
||||
Object c;
|
||||
if (data instanceof char[]) {
|
||||
c = new char[length];
|
||||
} else {
|
||||
c = new byte[length];
|
||||
}
|
||||
System.arraycopy(data, offset, c, 0, length);
|
||||
|
||||
this.data = c;
|
||||
@ -34,19 +33,107 @@ public final class String {
|
||||
}
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public static String valueOf(int v) {
|
||||
return valueOf((long) v);
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
if (hash == 0) {
|
||||
for (int i = 0; i < length; ++i) hash = (hash * 31) + charAt(i);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof String && compareTo((String) o) == 0;
|
||||
}
|
||||
|
||||
public boolean equalsIgnoreCase(String s) {
|
||||
return compareToIgnoreCase(s) == 0;
|
||||
}
|
||||
|
||||
public int compareTo(String s) {
|
||||
int d = length - s.length;
|
||||
if (d != 0) {
|
||||
return d;
|
||||
} else {
|
||||
for (int i = 0; i < length; ++i) {
|
||||
d = charAt(i) - s.charAt(i);
|
||||
if (d != 0) {
|
||||
return d;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int compareToIgnoreCase(String s) {
|
||||
int d = length - s.length;
|
||||
if (d != 0) {
|
||||
return d;
|
||||
} else {
|
||||
for (int i = 0; i < length; ++i) {
|
||||
d = Character.toLowerCase(charAt(i))
|
||||
- Character.toLowerCase(s.charAt(i));
|
||||
if (d != 0) {
|
||||
return d;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public String substring(int start) {
|
||||
return substring(start, length);
|
||||
}
|
||||
|
||||
public String substring(int start, int end) {
|
||||
if (start >= 0 && end >= start && end <= length) {
|
||||
if (start == 0 && end == length) {
|
||||
return this;
|
||||
} else if (end - start == 0) {
|
||||
return "";
|
||||
} else {
|
||||
return new String(data, offset + start, end - start, false);
|
||||
}
|
||||
} else {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean startsWith(String s) {
|
||||
if (length >= s.length) {
|
||||
return substring(0, s.length).compareTo(s) != 0;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean endsWith(String s) {
|
||||
if (length >= s.length) {
|
||||
return substring(length - s.length).compareTo(s) != 0;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public String concat(String s) {
|
||||
if (s.length() == 0) {
|
||||
return this;
|
||||
} else {
|
||||
return this + s;
|
||||
}
|
||||
}
|
||||
|
||||
public void getChars(int srcOffset, int srcLength,
|
||||
char[] dst, int dstOffset)
|
||||
{
|
||||
if (srcOffset + srcLength > length) {
|
||||
throw new ArrayIndexOutOfBoundsException();
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
||||
if (data instanceof char[]) {
|
||||
@ -60,6 +147,18 @@ public final class String {
|
||||
}
|
||||
}
|
||||
|
||||
public char charAt(int index) {
|
||||
if (index < 0 || index > length) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
||||
if (data instanceof char[]) {
|
||||
return ((char[]) data)[index + offset];
|
||||
} else {
|
||||
return (char) ((byte[]) data)[index + offset];
|
||||
}
|
||||
}
|
||||
|
||||
public static String valueOf(long v) {
|
||||
if (v == 0) {
|
||||
return valueOf('0');
|
||||
|
Reference in New Issue
Block a user