New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Regression: Source.getLines broken in 2.9.0.1 #4662
Comments
Imported From: https://issues.scala-lang.org/browse/SI-4662?orig=1 |
@dcsobral said: def reader() = new InputStreamReader(inputStream, codec.decoder)
def bufferedReader() = new BufferedReader(reader(), bufferSize)
override val iter = {
val reader = bufferedReader()
Iterator continually (codec wrap reader.read()) takeWhile (_ != -1) map (_.toChar)
}
class BufferedLineIterator extends Iterator[String] {
val bufReader = BufferedSource.this.bufferedReader() The definition |
@dcsobral said: |
@dcsobral said: I think the proper solution would be for On the other hand, it turns out this bug isn't all that bad. It only happens if you do something to the iterator before using it -- such as REPL showing whether the iterator is empty or not. |
@dcsobral said: |
Dmitry Antonyuk (lomeo) said (edited on Jun 4, 2011 11:28:43 AM UTC): io.Source.stdin.getLines.foreach(println) It shows first line only after we entered second line. AFAIK it happens 'cause var nextLine = bufReader.readLine So when we create I guess the correct code is something like this: class BufferedLineIterator extends Iterator[String] {
val bufReader = BufferedSource.this.bufferedReader()
var nextLine = null
var unread = true
override def hasNext() = {
if (unread) {
nextLine = bufReader.readLine
unread = false
}
nextLine != null
}
override def next(): String = {
val result = nextLine
if (unread) {
nextLine = bufReader.readLine
} else {
unread = true
}
result
}
} Should I create a new bug for this or these bugs are same? |
@dcsobral said (edited on Jun 4, 2011 1:39:22 PM UTC): The solution you provided is still suboptimal, however, because whenever you call "next", it will only return after the the following line has read. In other words, you fixed it for first/second line, but it will still happen with second/third, third/fourth, etc. For an example of the sort of problem this can give, see #4599. I think the following would fix it for override def next(): String = {
if (unread) {
nextLine = bufReader.readLine
}
unread = true
nextLine
} |
Viktor Hedefalk (hedefalk) said: @RunWith(classOf[JUnitRunner])
class SourceTest extends FlatSpec with ShouldMatchers with Logger {
import scala.io.Source._
it should "not frikkin eat it up" in {
val source = fromInputStream(this.getClass.getResourceAsStream("fileWithTwoLines"))(Codec.UTF8)
debug("source: " + source)
val lines = source.getLines()
debug("lines.hasNext: " + lines.hasNext)
debug("lines size: " + lines.size)
debug("lines: " + lines)
debug("lines.hasNext: " + lines.hasNext)
for(line <- lines) yield (debug("line: " + line))
}
} yields the following log: 16:40:54.576 [main] DEBUG test.SourceTest - source: non-empty iterator
16:40:54.583 [main] DEBUG test.SourceTest - lines.hasNext: true
16:40:54.584 [main] DEBUG test.SourceTest - lines size: 2
16:40:54.585 [main] DEBUG test.SourceTest - lines: empty iterator
16:40:54.585 [main] DEBUG test.SourceTest - lines.hasNext: false
|
@paulp said: |
Viktor Hedefalk (hedefalk) said: |
Source.getLines appears to be... entirely broken. I have a very simple test case: a plain-jain text file of three lines save in normal utf-8. Calling .getLines on the Source from this File does nothing: an empty iterator, no lines at all. However, it has no problem iterating over the chars.
This is a regression, it worked without a problem in 2.8. This paste shows exactly the problem and compares with 2.8:
http://pastie.org/2000844
The text was updated successfully, but these errors were encountered: