Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cketti committed Jun 24, 2024
1 parent d483391 commit f6b0a7c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,22 @@ fun CharSequence.codePointIterator(startIndex: Int = 0, endIndex: Int = length):
}

/**
* Performs given [action] for each [CodePoint] in the [CharSequence].
*
* @see forEachCodePointIndexed
* Performs the given [action] for each code point in this character sequence.
*/
inline fun CharSequence.forEachCodePoint(
action: (codePoint: CodePoint) -> Unit,
) = intForEachCodePoint { action(it.toCodePoint()) }
inline fun CharSequence.forEachCodePoint(action: (codePoint: CodePoint) -> Unit) {
intForEachCodePoint { codePoint ->
action(codePoint.toCodePoint())
}
}

/**
* Performs given [action] for each [CodePoint] in the [CharSequence].
* Provides the start index for the given codepoint
* Performs the given [action] for each code point in this character sequence.
*
* @param action The start index of the current code point is provided as the first argument to this function. The
* code point as [CodePoint] instance as the second argument.
*/
inline fun CharSequence.forEachCodePointIndexed(
action: (index: Int, codePoint: CodePoint) -> Unit,
) = intForEachCodePointIndexed { index, codePoint -> action(index, codePoint.toCodePoint()) }
inline fun CharSequence.forEachCodePointIndexed(action: (index: Int, codePoint: CodePoint) -> Unit) {
intForEachCodePointIndexed { index, codePoint ->
action(index, codePoint.toCodePoint())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package de.cketti.codepoints.deluxe

import kotlin.test.assertEquals
import kotlin.test.Test
import kotlin.test.assertFailsWith

class CharSequenceExtensionsTest {
@Test
Expand Down
53 changes: 29 additions & 24 deletions kotlin-codepoints/src/commonMain/kotlin/CharSequenceExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ fun CharSequence.codePointAt(index: Int): Int {
*
* The `index` parameter is the regular `CharSequence` index, i.e. the number of `Char`s from the start of the character
* sequence.
*
*
* If the `Char` value at `index - 1` is in the low surrogate range and the `Char` value at `index - 2` is in the high
* surrogate range, then the surrogate pair is decoded and the code point in one of the supplementary planes is
* surrogate range, then the surrogate pair is decoded and the code point in one of the supplementary planes is
* returned. In all other cases this method behaves like [CharSequence.get] was called with an argument of `index - 1`.
*
* If the value `index - 1` is out of bounds of this character sequence, this method throws an
* If the value `index - 1` is out of bounds of this character sequence, this method throws an
* [IndexOutOfBoundsException].
*/
fun CharSequence.codePointBefore(index: Int): Int {
Expand All @@ -78,11 +78,11 @@ fun CharSequence.codePointBefore(index: Int): Int {

/**
* Returns the number of Unicode code points in the specified text range of this `CharSequence`.
*
* The text range begins at the specified `beginIndex` and extends to the `Char` at index `endIndex - 1`. Thus, the
*
* The text range begins at the specified `beginIndex` and extends to the `Char` at index `endIndex - 1`. Thus, the
* length (in `Char`s) of the text range is `endIndex - beginIndex`. Unpaired surrogates within the text range count as
* one code point each.
*
*
* If `beginIndex` is negative, or `endIndex` is larger than the length of this string, or `beginIndex` is larger than
* `endIndex`, this method throws an [IndexOutOfBoundsException].
*/
Expand All @@ -108,10 +108,10 @@ fun CharSequence.codePointCount(beginIndex: Int, endIndex: Int): Int {
}

/**
* Returns the index within this `CharSequence` that is offset from the given `index` by `codePointOffset` code points.
*
* Returns the index within this `CharSequence` that is offset from the given `index` by `codePointOffset` code points.
*
* Unpaired surrogates within the text range given by `index` and `codePointOffset` count as one code point each.
*
*
* If `index` is negative or larger than the length of this character sequence, or if `codePointOffset` is positive and
* the subsequence starting with `index` has fewer than `codePointOffset` code points, or if `codePointOffset` is
* negative and the subsequence before index has fewer than the absolute value of `codePointOffset` code points, this
Expand Down Expand Up @@ -155,36 +155,41 @@ fun CharSequence.offsetByCodePoints(index: Int, codePointOffset: Int): Int {
}

/**
* Performs given [action] for each codepoint in the [CharSequence]s.
*
* @see forEachCodePointIndexed
* Performs the given [action] for each code point in this character sequence.
*/
inline fun CharSequence.forEachCodePoint(
action: (codePoint: Int) -> Unit,
) = forEachCodePointIndexed { _, codePoint -> action(codePoint) }
inline fun CharSequence.forEachCodePoint(action: (codePoint: Int) -> Unit) {
forEachCodePointIndexed { _, codePoint ->
action(codePoint)
}
}

/**
* Performs given [action] for each codepoint in the [CharSequence].
* Provides the start index for the given codepoint
* Performs the given [action] for each code point in this character sequence.
*
* @param action The start index of the current code point is provided as the first argument to this function. The code
* point value as the second argument.
*/
inline fun CharSequence.forEachCodePointIndexed(
action: (index: Int, codePoint: Int) -> Unit,
) {
val str = this
inline fun CharSequence.forEachCodePointIndexed(action: (index: Int, codePoint: Int) -> Unit) {
var index = 0
val endIndex = length
while (index < endIndex) {
val codePointStartIndex = index
val firstChar = str[index]

val firstChar = this[index]
index++

if (firstChar.isHighSurrogate() && index < endIndex) {
val nextChar = str[index]
val nextChar = this[index]
if (nextChar.isLowSurrogate()) {
action(codePointStartIndex, CodePoints.toCodePoint(firstChar, nextChar))
index++

val codePoint = CodePoints.toCodePoint(firstChar, nextChar)
action(codePointStartIndex, codePoint)

continue
}
}

action(codePointStartIndex, firstChar.code)
}
}

0 comments on commit f6b0a7c

Please sign in to comment.