@@ -53,12 +53,12 @@ fun CharSequence.codePointAt(index: Int): Int {
53
53
*
54
54
* The `index` parameter is the regular `CharSequence` index, i.e. the number of `Char`s from the start of the character
55
55
* sequence.
56
- *
56
+ *
57
57
* 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
59
59
* returned. In all other cases this method behaves like [CharSequence.get] was called with an argument of `index - 1`.
60
60
*
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
62
62
* [IndexOutOfBoundsException].
63
63
*/
64
64
fun CharSequence.codePointBefore (index : Int ): Int {
@@ -78,11 +78,11 @@ fun CharSequence.codePointBefore(index: Int): Int {
78
78
79
79
/* *
80
80
* 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
83
83
* length (in `Char`s) of the text range is `endIndex - beginIndex`. Unpaired surrogates within the text range count as
84
84
* one code point each.
85
- *
85
+ *
86
86
* If `beginIndex` is negative, or `endIndex` is larger than the length of this string, or `beginIndex` is larger than
87
87
* `endIndex`, this method throws an [IndexOutOfBoundsException].
88
88
*/
@@ -108,10 +108,10 @@ fun CharSequence.codePointCount(beginIndex: Int, endIndex: Int): Int {
108
108
}
109
109
110
110
/* *
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
+ *
113
113
* Unpaired surrogates within the text range given by `index` and `codePointOffset` count as one code point each.
114
- *
114
+ *
115
115
* If `index` is negative or larger than the length of this character sequence, or if `codePointOffset` is positive and
116
116
* the subsequence starting with `index` has fewer than `codePointOffset` code points, or if `codePointOffset` is
117
117
* 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 {
155
155
}
156
156
157
157
/* *
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.
161
159
*/
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
+ }
165
165
166
166
/* *
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.
169
171
*/
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 ) {
174
173
var index = 0
175
174
val endIndex = length
176
175
while (index < endIndex) {
177
176
val codePointStartIndex = index
178
- val firstChar = str[index]
177
+
178
+ val firstChar = this [index]
179
179
index++
180
+
180
181
if (firstChar.isHighSurrogate() && index < endIndex) {
181
- val nextChar = str [index]
182
+ val nextChar = this [index]
182
183
if (nextChar.isLowSurrogate()) {
183
- action(codePointStartIndex, CodePoints .toCodePoint(firstChar, nextChar))
184
184
index++
185
+
186
+ val codePoint = CodePoints .toCodePoint(firstChar, nextChar)
187
+ action(codePointStartIndex, codePoint)
188
+
185
189
continue
186
190
}
187
191
}
192
+
188
193
action(codePointStartIndex, firstChar.code)
189
194
}
190
195
}
0 commit comments