A view of a CharSequence that implements subSequence() without copying the whole contents.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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