Skip to content

Instantly share code, notes, and snippets.

Created June 12, 2012 22:50
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A view of a CharSequence that implements subSequence() without copying the whole contents.
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)
throw new IndexOutOfBoundsException
def subSequence(start: Int, end: Int): CharSequence = {
if(0 <= start && start <= end && end <= length) {
if(start == 0 && end == length)
else if(start == end)
new CharSeqView("", 0, 0)
new CharSeqView(base, start+offset, end-start)
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