Skip to content

Instantly share code, notes, and snippets.

@arnolddevos
Created June 12, 2012 22:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnolddevos/2920621 to your computer and use it in GitHub Desktop.
Save arnolddevos/2920621 to your computer and use it in GitHub Desktop.
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