From f6b0a7c7d70d196e2617cfb39b6b26d31e552a21 Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 24 Jun 2024 20:13:29 +0200 Subject: [PATCH] Cleanup --- .../kotlin/CharSequenceExtensions.kt | 26 +++++---- .../kotlin/CharSequenceExtensionsTest.kt | 1 - .../kotlin/CharSequenceExtensions.kt | 53 ++++++++++--------- 3 files changed, 44 insertions(+), 36 deletions(-) diff --git a/kotlin-codepoints-deluxe/src/commonMain/kotlin/CharSequenceExtensions.kt b/kotlin-codepoints-deluxe/src/commonMain/kotlin/CharSequenceExtensions.kt index f73e8cb..dfe1a7a 100644 --- a/kotlin-codepoints-deluxe/src/commonMain/kotlin/CharSequenceExtensions.kt +++ b/kotlin-codepoints-deluxe/src/commonMain/kotlin/CharSequenceExtensions.kt @@ -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()) } \ No newline at end of file +inline fun CharSequence.forEachCodePointIndexed(action: (index: Int, codePoint: CodePoint) -> Unit) { + intForEachCodePointIndexed { index, codePoint -> + action(index, codePoint.toCodePoint()) + } +} diff --git a/kotlin-codepoints-deluxe/src/commonTest/kotlin/CharSequenceExtensionsTest.kt b/kotlin-codepoints-deluxe/src/commonTest/kotlin/CharSequenceExtensionsTest.kt index 5768358..65ab863 100644 --- a/kotlin-codepoints-deluxe/src/commonTest/kotlin/CharSequenceExtensionsTest.kt +++ b/kotlin-codepoints-deluxe/src/commonTest/kotlin/CharSequenceExtensionsTest.kt @@ -2,7 +2,6 @@ package de.cketti.codepoints.deluxe import kotlin.test.assertEquals import kotlin.test.Test -import kotlin.test.assertFailsWith class CharSequenceExtensionsTest { @Test diff --git a/kotlin-codepoints/src/commonMain/kotlin/CharSequenceExtensions.kt b/kotlin-codepoints/src/commonMain/kotlin/CharSequenceExtensions.kt index a945ee6..c9bb48b 100644 --- a/kotlin-codepoints/src/commonMain/kotlin/CharSequenceExtensions.kt +++ b/kotlin-codepoints/src/commonMain/kotlin/CharSequenceExtensions.kt @@ -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 { @@ -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]. */ @@ -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 @@ -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) } }