Skip to content

Commit b0de8a0

Browse files
authored
Merge pull request #36 from cketti/no_argument_codePointCount
Add zero parameter variant of `CharSequence.codePointCount()`
2 parents 342112a + feaf18b commit b0de8a0

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Added
88
- `CodePoint.toUnicodeNotation()` returns the standard Unicode notation of a code point, e.g. `U+1F4E7`.
9+
- `CharSequence.codePointCount()` variant without parameters.
910

1011
## [0.8.0] - 2024-06-09
1112
### Changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ fun CharSequence.codePointBefore(index: Int): Int {
7676
return firstChar.code
7777
}
7878

79+
/**
80+
* Returns the number of Unicode code points in this `CharSequence`.
81+
*/
82+
fun CharSequence.codePointCount(): Int {
83+
return codePointCount(beginIndex = 0, endIndex = length)
84+
}
85+
7986
/**
8087
* Returns the number of Unicode code points in the specified text range of this `CharSequence`.
8188
*

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

+7
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,17 @@ class CharSequenceExtensionsTest {
6161

6262
@Test
6363
fun codePointCount() {
64+
assertEquals(0, "".codePointCount())
6465
assertEquals(0, "".codePointCount(beginIndex = 0, endIndex = 0))
6566
assertEquals(0, "abc".codePointCount(beginIndex = 1, endIndex = 1))
6667

68+
assertEquals(3, "abc".codePointCount())
6769
assertEquals(3, "abc".codePointCount(beginIndex = 0, endIndex = 3))
70+
assertEquals(2, "a\uFFFF".codePointCount())
6871
assertEquals(2, "a\uFFFF".codePointCount(beginIndex = 0, endIndex = 2))
72+
assertEquals(1, "\uD83E\uDD95".codePointCount())
6973
assertEquals(1, "\uD83E\uDD95".codePointCount(beginIndex = 0, endIndex = 2))
74+
assertEquals(2, "\uD83E\uDD95\uD83E\uDD96".codePointCount())
7075
assertEquals(2, "\uD83E\uDD95\uD83E\uDD96".codePointCount(beginIndex = 0, endIndex = 4))
7176

7277
assertEquals(2, "abc".codePointCount(beginIndex = 1, endIndex = 3))
@@ -80,7 +85,9 @@ class CharSequenceExtensionsTest {
8085

8186
@Test
8287
fun codePointCount_with_unmatched_surrogates() {
88+
assertEquals(2, "\uDD95\uD83E".codePointCount())
8389
assertEquals(2, "\uDD95\uD83E".codePointCount(beginIndex = 0, endIndex = 2))
90+
assertEquals(3, "\uDD95\uD83E\uDD95\uD83E".codePointCount())
8491
assertEquals(3, "\uDD95\uD83E\uDD95\uD83E".codePointCount(beginIndex = 0, endIndex = 4))
8592

8693
assertEquals(1, "\uDD95\uD83E".codePointCount(beginIndex = 1, endIndex = 2))

0 commit comments

Comments
 (0)