@@ -125,28 +125,24 @@ public int length() {
125125 }
126126
127127 private void resizeFor (int size ) {
128- int newlen = ArrayUtils .idealCharArraySize (size + 1 );
129- char [] newtext = new char [newlen ];
128+ final int oldLength = mText .length ;
129+ final int newLength = ArrayUtils .idealCharArraySize (size + 1 );
130+ final int after = oldLength - (mGapStart + mGapLength );
130131
131- int after = mText .length - (mGapStart + mGapLength );
132+ char [] newText = new char [newLength ];
133+ System .arraycopy (mText , 0 , newText , 0 , mGapStart );
134+ System .arraycopy (mText , oldLength - after , newText , newLength - after , after );
135+ mText = newText ;
132136
133- System .arraycopy (mText , 0 , newtext , 0 , mGapStart );
134- System .arraycopy (mText , mText .length - after ,
135- newtext , newlen - after , after );
137+ final int delta = newLength - oldLength ;
138+ mGapLength += delta ;
139+ if (mGapLength < 1 )
140+ new Exception ("mGapLength < 1" ).printStackTrace ();
136141
137142 for (int i = 0 ; i < mSpanCount ; i ++) {
138- if (mSpanStarts [i ] > mGapStart )
139- mSpanStarts [i ] += newlen - mText .length ;
140- if (mSpanEnds [i ] > mGapStart )
141- mSpanEnds [i ] += newlen - mText .length ;
143+ if (mSpanStarts [i ] > mGapStart ) mSpanStarts [i ] += delta ;
144+ if (mSpanEnds [i ] > mGapStart ) mSpanEnds [i ] += delta ;
142145 }
143-
144- int oldlen = mText .length ;
145- mText = newtext ;
146- mGapLength += mText .length - oldlen ;
147-
148- if (mGapLength < 1 )
149- new Exception ("mGapLength < 1" ).printStackTrace ();
150146 }
151147
152148 private void moveGapTo (int where ) {
@@ -157,14 +153,10 @@ private void moveGapTo(int where) {
157153
158154 if (where < mGapStart ) {
159155 int overlap = mGapStart - where ;
160-
161- System .arraycopy (mText , where ,
162- mText , mGapStart + mGapLength - overlap , overlap );
156+ System .arraycopy (mText , where , mText , mGapStart + mGapLength - overlap , overlap );
163157 } else /* where > mGapStart */ {
164158 int overlap = where - mGapStart ;
165-
166- System .arraycopy (mText , where + mGapLength - overlap ,
167- mText , mGapStart , overlap );
159+ System .arraycopy (mText , where + mGapLength - overlap , mText , mGapStart , overlap );
168160 }
169161
170162 // XXX be more clever
@@ -340,18 +332,17 @@ private void change(int start, int end, CharSequence tb, int tbstart, int tbend)
340332 boolean atEnd = (mGapStart + mGapLength == mText .length );
341333
342334 for (int i = mSpanCount - 1 ; i >= 0 ; i --) {
343- if (mSpanStarts [i ] >= start &&
344- mSpanStarts [i ] < mGapStart + mGapLength ) {
335+ if (mSpanStarts [i ] >= start && mSpanStarts [i ] < mGapStart + mGapLength ) {
345336 int flag = (mSpanFlags [i ] & START_MASK ) >> START_SHIFT ;
346337
347- if (flag == POINT || (flag == PARAGRAPH && atEnd ))
348- mSpanStarts [i ] = mGapStart + mGapLength ;
349- else
350- mSpanStarts [i ] = start ;
338+ if (flag == POINT || (flag == PARAGRAPH && atEnd )) {
339+ mSpanStarts [i ] = mGapStart + mGapLength ;
340+ } else {
341+ mSpanStarts [i ] = start ;
342+ }
351343 }
352344
353- if (mSpanEnds [i ] >= start &&
354- mSpanEnds [i ] < mGapStart + mGapLength ) {
345+ if (mSpanEnds [i ] >= start && mSpanEnds [i ] < mGapStart + mGapLength ) {
355346 int flag = (mSpanFlags [i ] & END_MASK );
356347
357348 if (flag == POINT || (flag == PARAGRAPH && atEnd ))
@@ -360,7 +351,8 @@ private void change(int start, int end, CharSequence tb, int tbstart, int tbend)
360351 mSpanEnds [i ] = start ;
361352 }
362353
363- // remove 0-length SPAN_EXCLUSIVE_EXCLUSIVE
354+ // remove 0-length SPAN_EXCLUSIVE_EXCLUSIVE, which are POINT_MARK and could
355+ // get their boundaries swapped by the above code
364356 if (mSpanEnds [i ] < mSpanStarts [i ]) {
365357 removeSpan (i );
366358 }
@@ -520,6 +512,11 @@ private void setSpan(boolean send, Object what, int start, int end, int flags) {
520512 }
521513 }
522514
515+ if (flags == Spanned .SPAN_EXCLUSIVE_EXCLUSIVE && start == end ) {
516+ throw new IllegalArgumentException (
517+ "SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length" );
518+ }
519+
523520 if (start > mGapStart ) {
524521 start += mGapLength ;
525522 } else if (start == mGapStart ) {
0 commit comments