Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A view of a CharSequence that implements subSequence() without copying the whole contents.
package au.com.langdale
package util
class CharSeqView( base: CharSequence, offset: Int, val length: Int) extends CharSequence {
def this(base: CharSequence) = this(base, 0, base.length)
def charAt(index: Int): Char = {
if( 0 <= index && index < length)
base.charAt(index + offset)
else
throw new IndexOutOfBoundsException
}
def subSequence(start: Int, end: Int): CharSequence = {
if(0 <= start && start <= end && end <= length) {
if(start == 0 && end == length)
this
else if(start == end)
new CharSeqView("", 0, 0)
else
new CharSeqView(base, start+offset, end-start)
}
else
throw new IndexOutOfBoundsException
}
override def toString: String = new java.lang.StringBuilder(this) toString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment