Skip to content

Instantly share code, notes, and snippets.

@avshabanov
Created April 11, 2015 03:42
Show Gist options
  • Save avshabanov/1f5d7acfd64389986bad to your computer and use it in GitHub Desktop.
Save avshabanov/1f5d7acfd64389986bad to your computer and use it in GitHub Desktop.
package com.alexshabanov.neocharseq
trait Str {
/** Appends the contents of this stream. */
fun appendTo(stream: Appendable)
fun charAt(pos: Int): Char
fun length(): Int
fun subSeq(start: Int, end: Int): Str
//
// Helper methods (auto implemented)
//
/** Returns char sequence representation, optimized for random access. */
fun forRandomAccess(): Str = this
fun indexOf(ch: Char): Int {
// implementation goes here...
val s = forRandomAccess()
var i = 0
val len = s.length()
while (i < len) {
if (s.charAt(i) == ch) {
return i
}
}
return -1
}
}
class StandardStr(val chars: Array<Char>): Str {
override fun appendTo(stream: Appendable) {
throw UnsupportedOperationException()
}
override fun charAt(pos: Int)= chars[pos]
override fun length() = chars.size
override fun subSeq(start: Int, end: Int): Str {
throw UnsupportedOperationException()
}
}
class Latin1Str(val byte: Array<Byte>): Str {
override fun appendTo(stream: Appendable) {
throw UnsupportedOperationException()
}
override fun charAt(pos: Int): Char {
throw UnsupportedOperationException()
}
override fun length(): Int {
throw UnsupportedOperationException()
}
override fun subSeq(start: Int, end: Int): Str {
throw UnsupportedOperationException()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment