mirror of
https://github.com/corda/corda.git
synced 2025-02-08 20:10:22 +00:00
add methods to StringBuilder and fix logic error in getChars
This commit is contained in:
parent
ae02212dd9
commit
0d7a2fa2bc
@ -129,10 +129,10 @@ public class StringBuffer implements CharSequence {
|
|||||||
sb.setCharAt(index, ch);
|
sb.setCharAt(index, ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void getChars(int srcOffset, int srcLength, char[] dst,
|
public synchronized void getChars(int srcStart, int srcEnd, char[] dst,
|
||||||
int dstOffset)
|
int dstStart)
|
||||||
{
|
{
|
||||||
sb.getChars(srcOffset, srcLength, dst, dstOffset);
|
sb.getChars(srcStart, srcEnd, dst, dstStart);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized String toString() {
|
public synchronized String toString() {
|
||||||
|
@ -249,6 +249,29 @@ public class StringBuilder implements CharSequence, Appendable {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int lastIndexOf(String s) {
|
||||||
|
return lastIndexOf(s, length - s.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
public int lastIndexOf(String s, int lastIndex) {
|
||||||
|
int slength = s.length();
|
||||||
|
if (slength == 0) return lastIndex;
|
||||||
|
|
||||||
|
for (int i = Math.min(length - slength, lastIndex); i >= 0; --i) {
|
||||||
|
int j = 0;
|
||||||
|
for (; j < slength && i + j < length; ++j) {
|
||||||
|
if (charAt(i + j) != s.charAt(j)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j == slength) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
public int length() {
|
public int length() {
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
@ -283,9 +306,8 @@ public class StringBuilder implements CharSequence, Appendable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getChars(int srcOffset, int srcLength, char[] dst, int dstOffset)
|
public void getChars(int srcStart, int srcEnd, char[] dst, int dstStart) {
|
||||||
{
|
if (srcStart < 0 || srcEnd > length) {
|
||||||
if (srcOffset < 0 || srcOffset + srcLength > length) {
|
|
||||||
throw new IndexOutOfBoundsException();
|
throw new IndexOutOfBoundsException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -297,17 +319,17 @@ public class StringBuilder implements CharSequence, Appendable {
|
|||||||
int end = index;
|
int end = index;
|
||||||
index = start;
|
index = start;
|
||||||
|
|
||||||
if (start < srcOffset) {
|
if (start < srcStart) {
|
||||||
start = srcOffset;
|
start = srcStart;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (end > srcOffset + srcLength) {
|
if (end > srcEnd) {
|
||||||
end = srcOffset + srcLength;
|
end = srcEnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (start < end) {
|
if (start < end) {
|
||||||
c.value.getChars(start - index, end - start,
|
c.value.getChars(start - index, end - index,
|
||||||
dst, dstOffset + (start - srcOffset));
|
dst, dstStart + (start - srcStart));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -328,10 +350,14 @@ public class StringBuilder implements CharSequence, Appendable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String substring(int start) {
|
||||||
|
return substring(start, length);
|
||||||
|
}
|
||||||
|
|
||||||
public String substring(int start, int end) {
|
public String substring(int start, int end) {
|
||||||
int len = end-start;
|
int len = end-start;
|
||||||
char[] buf = new char[len];
|
char[] buf = new char[len];
|
||||||
getChars(start, len, buf,0 );
|
getChars(start, end, buf, 0);
|
||||||
return new String(buf, 0, len, false);
|
return new String(buf, 0, len, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user