miha-x64
06/06/2020, 10:18 PMinline fun CharSequence.forEachCodePoint(
start: Int = 0, end: Int = length,
action: (index: Int, codePoint: Int) -> Unit
) {
var i = start
var codePoint: Int
while (i < end) {
codePoint = Character.codePointAt(this, i);
action(i, codePoint)
i += Character.charCount(codePoint)
}
}
jw
06/07/2020, 12:35 AMMark
07/09/2020, 9:01 AMfun CharSequence?.asCodePointSequence(): Sequence<Int> {
return if (this.isNullOrEmpty()) {
emptySequence()
} else {
var nextOffset = 0
sequence {
while (nextOffset < length) {
val result = Character.codePointAt(this@asCodePointSequence, nextOffset)
nextOffset += Character.charCount(result)
yield(result)
}
}
}
}
miha-x64
07/13/2020, 8:13 PM.codePoints(): IntStream
but it's not really useful. I'd better use IntIterator
.Mark
07/14/2020, 2:27 AMIntIterator
vs Sequence<Int>
?miha-x64
07/14/2020, 7:17 PMnext()
method. This makes easy, for example, to wrap it and make iterator of words: you just call next()
until space or punctuation encountered.
Making the same thing to Sequence/Stream is basically the same but requires more indirection because Sequence/Stream it is not an Iterator itself but a (spl)iterator factory.
Example: https://youtrack.jetbrains.com/issue/KT-38384