diff options
author | Szabolcs Berecz <szabolcs.berecz@gmail.com> | 2015-03-14 01:27:02 +0100 |
---|---|---|
committer | Szabolcs Berecz <szabolcs.berecz@gmail.com> | 2015-03-14 01:27:02 +0100 |
commit | 8e0a386508d6cb655db0a29a23fb26170b262548 (patch) | |
tree | 261442a2328bbb61ad2c00fb4a22d665d8c1a102 /src | |
parent | fa33395a25c87115c910e8d4a4124aee6134062b (diff) | |
download | scala-8e0a386508d6cb655db0a29a23fb26170b262548.tar.gz scala-8e0a386508d6cb655db0a29a23fb26170b262548.tar.bz2 scala-8e0a386508d6cb655db0a29a23fb26170b262548.zip |
SI-9219 Stream toString returns unexpected result
- Cursor was not advanced before appending the second element
when only the first two elements of the stream were known.
- When there is no cycle in the stream, the "scout" (and
"cursor") ends up pointing to a stream where tailDefined
is false. This means that cursor is either empty, or
cursor.tail is not yet evaluated. The former case is handled
properly, but in the latter case, one more element
(cursor.head) needs to be appended.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/collection/immutable/Stream.scala | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/src/library/scala/collection/immutable/Stream.scala b/src/library/scala/collection/immutable/Stream.scala index 9ed5162061..f303e79bb3 100644 --- a/src/library/scala/collection/immutable/Stream.scala +++ b/src/library/scala/collection/immutable/Stream.scala @@ -743,16 +743,18 @@ self => b append end return b } - if ((cursor ne scout) && scout.tailDefined) { + if (cursor ne scout) { cursor = scout - scout = scout.tail - // Use 2x 1x iterator trick for cycle detection; slow iterator can add strings - while ((cursor ne scout) && scout.tailDefined) { - b append sep append cursor.head - n += 1 - cursor = cursor.tail + if (scout.tailDefined) { scout = scout.tail - if (scout.tailDefined) scout = scout.tail + // Use 2x 1x iterator trick for cycle detection; slow iterator can add strings + while ((cursor ne scout) && scout.tailDefined) { + b append sep append cursor.head + n += 1 + cursor = cursor.tail + scout = scout.tail + if (scout.tailDefined) scout = scout.tail + } } } if (!scout.tailDefined) { // Not a cycle, scout hit an end @@ -761,6 +763,9 @@ self => n += 1 cursor = cursor.tail } + if (cursor.nonEmpty) { + b append sep append cursor.head + } } else { // Cycle. |