diff options
author | Josh Suereth <Joshua.Suereth@gmail.com> | 2012-11-20 17:54:28 -0800 |
---|---|---|
committer | Josh Suereth <Joshua.Suereth@gmail.com> | 2012-11-20 17:54:28 -0800 |
commit | 522ef796c8466d24fe7068af0bd1c8989b29b392 (patch) | |
tree | 2188f2e8f982a3aa2f1cdfcd1d64e0c7a62d6ded /src/library | |
parent | 249b3c43a125542d1c981b32626fa77ae6f0d773 (diff) | |
parent | 925c6e34635a9a8621b20c328670cbd23975327f (diff) | |
download | scala-522ef796c8466d24fe7068af0bd1c8989b29b392.tar.gz scala-522ef796c8466d24fe7068af0bd1c8989b29b392.tar.bz2 scala-522ef796c8466d24fe7068af0bd1c8989b29b392.zip |
Merge pull request #1596 from soc/SI-6632+SI-6633
SI-6632 SI-6633 Fixes issues and data corruption in ListBuffer
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/collection/mutable/ListBuffer.scala | 93 |
1 files changed, 44 insertions, 49 deletions
diff --git a/src/library/scala/collection/mutable/ListBuffer.scala b/src/library/scala/collection/mutable/ListBuffer.scala index 4715b6818a..67af4a6bd6 100644 --- a/src/library/scala/collection/mutable/ListBuffer.scala +++ b/src/library/scala/collection/mutable/ListBuffer.scala @@ -129,29 +129,27 @@ final class ListBuffer[A] * @throws Predef.IndexOutOfBoundsException if `n` is out of bounds. */ def update(n: Int, x: A) { - try { - if (exported) copy() - if (n == 0) { - val newElem = new :: (x, start.tail); - if (last0 eq start) { - last0 = newElem - } - start = newElem - } else { - var cursor = start - var i = 1 - while (i < n) { - cursor = cursor.tail - i += 1 - } - val newElem = new :: (x, cursor.tail.tail) - if (last0 eq cursor.tail) { - last0 = newElem - } - cursor.asInstanceOf[::[A]].tl = newElem + // We check the bounds early, so that we don't trigger copying. + if (n < 0 || n >= len) throw new IndexOutOfBoundsException(n.toString) + if (exported) copy() + if (n == 0) { + val newElem = new :: (x, start.tail); + if (last0 eq start) { + last0 = newElem + } + start = newElem + } else { + var cursor = start + var i = 1 + while (i < n) { + cursor = cursor.tail + i += 1 + } + val newElem = new :: (x, cursor.tail.tail) + if (last0 eq cursor.tail) { + last0 = newElem } - } catch { - case ex: Exception => throw new IndexOutOfBoundsException(n.toString()) + cursor.asInstanceOf[::[A]].tl = newElem } } @@ -212,34 +210,31 @@ final class ListBuffer[A] * @throws Predef.IndexOutOfBoundsException if `n` is out of bounds. */ def insertAll(n: Int, seq: Traversable[A]) { - try { - if (exported) copy() - var elems = seq.toList.reverse - len += elems.length - if (n == 0) { - while (!elems.isEmpty) { - val newElem = new :: (elems.head, start) - if (start.isEmpty) last0 = newElem - start = newElem - elems = elems.tail - } - } else { - var cursor = start - var i = 1 - while (i < n) { - cursor = cursor.tail - i += 1 - } - while (!elems.isEmpty) { - val newElem = new :: (elems.head, cursor.tail) - if (cursor.tail.isEmpty) last0 = newElem - cursor.asInstanceOf[::[A]].tl = newElem - elems = elems.tail - } + // We check the bounds early, so that we don't trigger copying. + if (n < 0 || n > len) throw new IndexOutOfBoundsException(n.toString) + if (exported) copy() + var elems = seq.toList.reverse + len += elems.length + if (n == 0) { + while (!elems.isEmpty) { + val newElem = new :: (elems.head, start) + if (start.isEmpty) last0 = newElem + start = newElem + elems = elems.tail + } + } else { + var cursor = start + var i = 1 + while (i < n) { + cursor = cursor.tail + i += 1 + } + while (!elems.isEmpty) { + val newElem = new :: (elems.head, cursor.tail) + if (cursor.tail.isEmpty) last0 = newElem + cursor.asInstanceOf[::[A]].tl = newElem + elems = elems.tail } - } catch { - case ex: Exception => - throw new IndexOutOfBoundsException(n.toString()) } } |