Skip to content

Commit 0e7b802

Browse files
Gilles DebunneAndroid (Google) Code Review
authored andcommitted
Merge "Limit created string size in Spell Checker" into ics-mr1
2 parents ab6b816 + 653d3a2 commit 0e7b802

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

core/java/android/text/SpannableStringBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,17 @@ public String toString() {
863863
return new String(buf);
864864
}
865865

866+
/**
867+
* Return a String containing a copy of the chars in this buffer, limited to the
868+
* [start, end[ range.
869+
* @hide
870+
*/
871+
public String substring(int start, int end) {
872+
char[] buf = new char[end - start];
873+
getChars(start, end, buf, 0);
874+
return new String(buf);
875+
}
876+
866877
private TextWatcher[] sendTextWillChange(int start, int before, int after) {
867878
TextWatcher[] recip = getSpans(start, start + before, TextWatcher.class);
868879
int n = recip.length;

core/java/android/text/method/WordIterator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package android.text.method;
1919

2020
import android.text.Selection;
21+
import android.text.SpannableStringBuilder;
2122

2223
import java.text.BreakIterator;
2324
import java.util.Locale;
@@ -58,7 +59,11 @@ public void setCharSequence(CharSequence charSequence, int start, int end) {
5859
mOffsetShift = Math.max(0, start - WINDOW_WIDTH);
5960
final int windowEnd = Math.min(charSequence.length(), end + WINDOW_WIDTH);
6061

61-
mString = charSequence.toString().substring(mOffsetShift, windowEnd);
62+
if (charSequence instanceof SpannableStringBuilder) {
63+
mString = ((SpannableStringBuilder) charSequence).substring(mOffsetShift, windowEnd);
64+
} else {
65+
mString = charSequence.subSequence(mOffsetShift, windowEnd).toString();
66+
}
6267
mIterator.setText(mString);
6368
}
6469

core/java/android/widget/SpellChecker.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.content.Context;
2020
import android.text.Editable;
2121
import android.text.Selection;
22+
import android.text.SpannableStringBuilder;
2223
import android.text.Spanned;
2324
import android.text.method.WordIterator;
2425
import android.text.style.SpellCheckSpan;
@@ -239,7 +240,9 @@ private void spellCheck() {
239240

240241
// Do not check this word if the user is currently editing it
241242
if (start >= 0 && end > start && (selectionEnd < start || selectionStart > end)) {
242-
final String word = editable.subSequence(start, end).toString();
243+
final String word = (editable instanceof SpannableStringBuilder) ?
244+
((SpannableStringBuilder) editable).substring(start, end) :
245+
editable.subSequence(start, end).toString();
243246
spellCheckSpan.setSpellCheckInProgress(true);
244247
textInfos[textInfosCount++] = new TextInfo(word, mCookie, mIds[i]);
245248
}

0 commit comments

Comments
 (0)