implement StringBuilder.delete() more efficiently

This commit is contained in:
Joel Dice 2007-10-29 15:40:05 -06:00
parent bcd2c75f41
commit 956106f518
3 changed files with 35 additions and 22 deletions

View File

@ -149,16 +149,11 @@ public class StringBuilder {
}
public StringBuilder delete(int start, int end) {
int numChars = end - start; // Do not delete end itself
while (numChars > 0) {
deleteCharAt(start);
numChars--;
}
if (start >= end) {
return this;
}
public StringBuilder deleteCharAt(int i) {
if (i < 0 || i >= length) {
if (start < 0 || end > length) {
throw new IndexOutOfBoundsException();
}
@ -168,32 +163,40 @@ public class StringBuilder {
-- length;
Cell p = null;
for (Cell c = chain; c != null; c = c.next) {
int start = index - c.value.length();
index = start;
int e = index;
int s = index - c.value.length();
index = s;
if (i >= start) {
if (c.value.length() == 1) {
if (end >= e) {
if (start <= s) {
if (p == null) {
chain = c.next;
} else {
p.next = c.next;
}
} else if (i == start) {
c.value = c.value.substring(1);
} else if (i == start + c.value.length() - 1) {
c.value = c.value.substring(0, c.value.length() - 1);
} else {
c.value = c.value.substring(0, start - s);
break;
}
} else {
if (start <= s) {
c.value = c.value.substring(end - s, e - s);
} else {
String v = c.value;
c.value = v.substring(i - start + 1, v.length());
c.next = new Cell(v.substring(0, i - start), c.next);
}
c.value = v.substring(end - s, e - s);
c.next = new Cell(v.substring(0, start - s), c.next);
break;
}
}
}
return this;
}
public StringBuilder deleteCharAt(int i) {
return delete(i, i + 1);
}
public StringBuilder replace(int start, int end, String str) {
delete(start, end);
insert(start, str);

View File

@ -112,7 +112,7 @@ ifeq ($(mode),stress-major)
cflags += -O0 -g3 -DVM_STRESS -DVM_STRESS_MAJOR
endif
ifeq ($(mode),fast)
cflags += -O3 -DNDEBUG
cflags += -O3 -g3 -DNDEBUG
strip = strip
show-size = ls -l
endif

View File

@ -529,6 +529,16 @@ invokeNative(Thread* t, object method)
&byteArrayBody(t, methodName(t, method), 0));
}
// if (strcmp(reinterpret_cast<const char*>
// (&byteArrayBody(t, className(t, methodClass(t, method)), 0)),
// "org/eclipse/swt/internal/C") == 0
// and strcmp(reinterpret_cast<const char*>
// (&byteArrayBody(t, methodName(t, method), 0)),
// "memmove") == 0)
// {
// asm("int3");
// }
{ ENTER(t, Thread::IdleState);
result = t->m->system->call