Skip to content

Commit 67e8014

Browse files
authored
Merge pull request #25 from JakeWharton/jw.zero.2023-03-07
Ensure `codePointCount` can be called on zero-width range
2 parents 28d976e + 27b4710 commit 67e8014

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fun CharSequence.codePointCount(beginIndex: Int, endIndex: Int): Int {
9191

9292
var index = beginIndex
9393
var count = 0
94-
do {
94+
while (index < endIndex) {
9595
val firstChar = this[index]
9696
index++
9797
if (firstChar.isHighSurrogate() && index < endIndex) {
@@ -102,7 +102,7 @@ fun CharSequence.codePointCount(beginIndex: Int, endIndex: Int): Int {
102102
}
103103

104104
count++
105-
} while (index < endIndex)
105+
}
106106

107107
return count
108108
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class CharSequenceExtensionsTest {
6161

6262
@Test
6363
fun codePointCount() {
64+
assertEquals(0, "".codePointCount(beginIndex = 0, endIndex = 0))
65+
assertEquals(0, "abc".codePointCount(beginIndex = 1, endIndex = 1))
66+
6467
assertEquals(3, "abc".codePointCount(beginIndex = 0, endIndex = 3))
6568
assertEquals(2, "a\uFFFF".codePointCount(beginIndex = 0, endIndex = 2))
6669
assertEquals(1, "\uD83E\uDD95".codePointCount(beginIndex = 0, endIndex = 2))

0 commit comments

Comments
 (0)