Skip to content

Commit f6b0a7c

Browse files
committed
Cleanup
1 parent d483391 commit f6b0a7c

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

kotlin-codepoints-deluxe/src/commonMain/kotlin/CharSequenceExtensions.kt

+15-11
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,22 @@ fun CharSequence.codePointIterator(startIndex: Int = 0, endIndex: Int = length):
5555
}
5656

5757
/**
58-
* Performs given [action] for each [CodePoint] in the [CharSequence].
59-
*
60-
* @see forEachCodePointIndexed
58+
* Performs the given [action] for each code point in this character sequence.
6159
*/
62-
inline fun CharSequence.forEachCodePoint(
63-
action: (codePoint: CodePoint) -> Unit,
64-
) = intForEachCodePoint { action(it.toCodePoint()) }
60+
inline fun CharSequence.forEachCodePoint(action: (codePoint: CodePoint) -> Unit) {
61+
intForEachCodePoint { codePoint ->
62+
action(codePoint.toCodePoint())
63+
}
64+
}
6565

6666
/**
67-
* Performs given [action] for each [CodePoint] in the [CharSequence].
68-
* Provides the start index for the given codepoint
67+
* Performs the given [action] for each code point in this character sequence.
68+
*
69+
* @param action The start index of the current code point is provided as the first argument to this function. The
70+
* code point as [CodePoint] instance as the second argument.
6971
*/
70-
inline fun CharSequence.forEachCodePointIndexed(
71-
action: (index: Int, codePoint: CodePoint) -> Unit,
72-
) = intForEachCodePointIndexed { index, codePoint -> action(index, codePoint.toCodePoint()) }
72+
inline fun CharSequence.forEachCodePointIndexed(action: (index: Int, codePoint: CodePoint) -> Unit) {
73+
intForEachCodePointIndexed { index, codePoint ->
74+
action(index, codePoint.toCodePoint())
75+
}
76+
}

kotlin-codepoints-deluxe/src/commonTest/kotlin/CharSequenceExtensionsTest.kt

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package de.cketti.codepoints.deluxe
22

33
import kotlin.test.assertEquals
44
import kotlin.test.Test
5-
import kotlin.test.assertFailsWith
65

76
class CharSequenceExtensionsTest {
87
@Test

kotlin-codepoints/src/commonMain/kotlin/CharSequenceExtensions.kt

+29-24
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ fun CharSequence.codePointAt(index: Int): Int {
5353
*
5454
* The `index` parameter is the regular `CharSequence` index, i.e. the number of `Char`s from the start of the character
5555
* sequence.
56-
*
56+
*
5757
* If the `Char` value at `index - 1` is in the low surrogate range and the `Char` value at `index - 2` is in the high
58-
* surrogate range, then the surrogate pair is decoded and the code point in one of the supplementary planes is
58+
* surrogate range, then the surrogate pair is decoded and the code point in one of the supplementary planes is
5959
* returned. In all other cases this method behaves like [CharSequence.get] was called with an argument of `index - 1`.
6060
*
61-
* If the value `index - 1` is out of bounds of this character sequence, this method throws an
61+
* If the value `index - 1` is out of bounds of this character sequence, this method throws an
6262
* [IndexOutOfBoundsException].
6363
*/
6464
fun CharSequence.codePointBefore(index: Int): Int {
@@ -78,11 +78,11 @@ fun CharSequence.codePointBefore(index: Int): Int {
7878

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

110110
/**
111-
* Returns the index within this `CharSequence` that is offset from the given `index` by `codePointOffset` code points.
112-
*
111+
* Returns the index within this `CharSequence` that is offset from the given `index` by `codePointOffset` code points.
112+
*
113113
* Unpaired surrogates within the text range given by `index` and `codePointOffset` count as one code point each.
114-
*
114+
*
115115
* If `index` is negative or larger than the length of this character sequence, or if `codePointOffset` is positive and
116116
* the subsequence starting with `index` has fewer than `codePointOffset` code points, or if `codePointOffset` is
117117
* 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 {
155155
}
156156

157157
/**
158-
* Performs given [action] for each codepoint in the [CharSequence]s.
159-
*
160-
* @see forEachCodePointIndexed
158+
* Performs the given [action] for each code point in this character sequence.
161159
*/
162-
inline fun CharSequence.forEachCodePoint(
163-
action: (codePoint: Int) -> Unit,
164-
) = forEachCodePointIndexed { _, codePoint -> action(codePoint) }
160+
inline fun CharSequence.forEachCodePoint(action: (codePoint: Int) -> Unit) {
161+
forEachCodePointIndexed { _, codePoint ->
162+
action(codePoint)
163+
}
164+
}
165165

166166
/**
167-
* Performs given [action] for each codepoint in the [CharSequence].
168-
* Provides the start index for the given codepoint
167+
* Performs the given [action] for each code point in this character sequence.
168+
*
169+
* @param action The start index of the current code point is provided as the first argument to this function. The code
170+
* point value as the second argument.
169171
*/
170-
inline fun CharSequence.forEachCodePointIndexed(
171-
action: (index: Int, codePoint: Int) -> Unit,
172-
) {
173-
val str = this
172+
inline fun CharSequence.forEachCodePointIndexed(action: (index: Int, codePoint: Int) -> Unit) {
174173
var index = 0
175174
val endIndex = length
176175
while (index < endIndex) {
177176
val codePointStartIndex = index
178-
val firstChar = str[index]
177+
178+
val firstChar = this[index]
179179
index++
180+
180181
if (firstChar.isHighSurrogate() && index < endIndex) {
181-
val nextChar = str[index]
182+
val nextChar = this[index]
182183
if (nextChar.isLowSurrogate()) {
183-
action(codePointStartIndex, CodePoints.toCodePoint(firstChar, nextChar))
184184
index++
185+
186+
val codePoint = CodePoints.toCodePoint(firstChar, nextChar)
187+
action(codePointStartIndex, codePoint)
188+
185189
continue
186190
}
187191
}
192+
188193
action(codePointStartIndex, firstChar.code)
189194
}
190195
}

0 commit comments

Comments
 (0)