Skip to content

Commit 7c5f670

Browse files
author
Gilles Debunne
committed
More minor refactoring in SpannableStringBuilder
No change in the functionnalities. Change-Id: I009acc75b4f70e65a810cdc67505bc0b13e627b2
1 parent 8b461b6 commit 7c5f670

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

core/java/android/text/SpannableStringBuilder.java

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -127,28 +127,24 @@ public int length() {
127127
}
128128

129129
private void resizeFor(int size) {
130-
int newlen = ArrayUtils.idealCharArraySize(size + 1);
131-
char[] newtext = new char[newlen];
130+
final int oldLength = mText.length;
131+
final int newLength = ArrayUtils.idealCharArraySize(size + 1);
132+
final int after = oldLength - (mGapStart + mGapLength);
132133

133-
int after = mText.length - (mGapStart + mGapLength);
134+
char[] newText = new char[newLength];
135+
System.arraycopy(mText, 0, newText, 0, mGapStart);
136+
System.arraycopy(mText, oldLength - after, newText, newLength - after, after);
137+
mText = newText;
134138

135-
System.arraycopy(mText, 0, newtext, 0, mGapStart);
136-
System.arraycopy(mText, mText.length - after,
137-
newtext, newlen - after, after);
139+
final int delta = newLength - oldLength;
140+
mGapLength += delta;
141+
if (mGapLength < 1)
142+
new Exception("mGapLength < 1").printStackTrace();
138143

139144
for (int i = 0; i < mSpanCount; i++) {
140-
if (mSpanStarts[i] > mGapStart)
141-
mSpanStarts[i] += newlen - mText.length;
142-
if (mSpanEnds[i] > mGapStart)
143-
mSpanEnds[i] += newlen - mText.length;
145+
if (mSpanStarts[i] > mGapStart) mSpanStarts[i] += delta;
146+
if (mSpanEnds[i] > mGapStart) mSpanEnds[i] += delta;
144147
}
145-
146-
int oldlen = mText.length;
147-
mText = newtext;
148-
mGapLength += mText.length - oldlen;
149-
150-
if (mGapLength < 1)
151-
new Exception("mGapLength < 1").printStackTrace();
152148
}
153149

154150
private void moveGapTo(int where) {
@@ -159,14 +155,10 @@ private void moveGapTo(int where) {
159155

160156
if (where < mGapStart) {
161157
int overlap = mGapStart - where;
162-
163-
System.arraycopy(mText, where,
164-
mText, mGapStart + mGapLength - overlap, overlap);
158+
System.arraycopy(mText, where, mText, mGapStart + mGapLength - overlap, overlap);
165159
} else /* where > mGapStart */ {
166160
int overlap = where - mGapStart;
167-
168-
System.arraycopy(mText, where + mGapLength - overlap,
169-
mText, mGapStart, overlap);
161+
System.arraycopy(mText, where + mGapLength - overlap, mText, mGapStart, overlap);
170162
}
171163

172164
// XXX be more clever
@@ -342,18 +334,17 @@ private void change(int start, int end, CharSequence tb, int tbstart, int tbend)
342334
boolean atEnd = (mGapStart + mGapLength == mText.length);
343335

344336
for (int i = mSpanCount - 1; i >= 0; i--) {
345-
if (mSpanStarts[i] >= start &&
346-
mSpanStarts[i] < mGapStart + mGapLength) {
337+
if (mSpanStarts[i] >= start && mSpanStarts[i] < mGapStart + mGapLength) {
347338
int flag = (mSpanFlags[i] & START_MASK) >> START_SHIFT;
348339

349-
if (flag == POINT || (flag == PARAGRAPH && atEnd))
350-
mSpanStarts[i] = mGapStart + mGapLength;
351-
else
352-
mSpanStarts[i] = start;
340+
if (flag == POINT || (flag == PARAGRAPH && atEnd)) {
341+
mSpanStarts[i] = mGapStart + mGapLength;
342+
} else {
343+
mSpanStarts[i] = start;
344+
}
353345
}
354346

355-
if (mSpanEnds[i] >= start &&
356-
mSpanEnds[i] < mGapStart + mGapLength) {
347+
if (mSpanEnds[i] >= start && mSpanEnds[i] < mGapStart + mGapLength) {
357348
int flag = (mSpanFlags[i] & END_MASK);
358349

359350
if (flag == POINT || (flag == PARAGRAPH && atEnd))
@@ -362,7 +353,8 @@ private void change(int start, int end, CharSequence tb, int tbstart, int tbend)
362353
mSpanEnds[i] = start;
363354
}
364355

365-
// remove 0-length SPAN_EXCLUSIVE_EXCLUSIVE
356+
// remove 0-length SPAN_EXCLUSIVE_EXCLUSIVE, which are POINT_MARK and could
357+
// get their boundaries swapped by the above code
366358
if (mSpanEnds[i] < mSpanStarts[i]) {
367359
removeSpan(i);
368360
}
@@ -495,6 +487,11 @@ private void setSpan(boolean send, Object what, int start, int end, int flags) {
495487
}
496488
}
497489

490+
if (flags == Spanned.SPAN_EXCLUSIVE_EXCLUSIVE && start == end) {
491+
throw new IllegalArgumentException(
492+
"SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length");
493+
}
494+
498495
if (start > mGapStart) {
499496
start += mGapLength;
500497
} else if (start == mGapStart) {

0 commit comments

Comments
 (0)