tags:

views:

83

answers:

1

Hi Guys,

The getLines() method of scala.io.Source strips \r and \n from the lines it returns. I instead want to keep those characters in the returned Strings.

getLines' comment says:

returns lines (NOT including newline character(s)) [...] if you need more refined behavior you can subclass Source#LineIterator

I've tried to subclass LineIterator, but I'm having trouble (and possibly plain misunderstanding!).

I want something like:

class FullLineIterator() extends LineIterator {   
    // Don't strip \r?\n
    override def getc() = iter.hasNext && {
        val ch = iter.next
        if (ch == '\n') { 
            sb append ch
            false
        } else if (ch == '\r') {            
            sb append ch

            if (iter.hasNext && iter.head == '\n') {
                iter.next
                sb append iter.head
            }

            false
        } else {
            sb append ch
            true
        }
    }
}

But I hit a couple of major issues:
1) Type LineIterator is not found, even though I have import scala.io.Source._
2) I'm fairly sure that I can't access sb, since it's private[this] inside LineIterator.

Has anyone ever wanted to do this? It's a fairly strange thing to want to do, but I do want to do it :).

I've peeked at scalax and have googled for a fair while, with no luck.

Any suggestions will be gratefully received. Thanks.

+2  A: 

Since you probably subclass scala.io.Source itself why not override getLines() method and return your class not overriding LineIterator, but providing exactly the functionality you need? The comment is misleding however, no doubt.

venechka
Yes, I think I'll have to take the approach you suggest. Thanks.
owst