From feaf18b2d781433442d4b2403afc02375ef6aa5f Mon Sep 17 00:00:00 2001 From: cketti Date: Sun, 9 Jun 2024 23:19:36 +0200 Subject: [PATCH] Add zero parameter variant of `CharSequence.codePointCount()` --- CHANGELOG.md | 1 + .../src/commonMain/kotlin/CharSequenceExtensions.kt | 7 +++++++ .../src/commonTest/kotlin/CharSequenceExtensionsTest.kt | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0128b1..928f51b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Added - `CodePoint.toUnicodeNotation()` returns the standard Unicode notation of a code point, e.g. `U+1F4E7`. +- `CharSequence.codePointCount()` variant without parameters. ## [0.8.0] - 2024-06-09 ### Changed diff --git a/kotlin-codepoints/src/commonMain/kotlin/CharSequenceExtensions.kt b/kotlin-codepoints/src/commonMain/kotlin/CharSequenceExtensions.kt index 290cdeb..f69a268 100644 --- a/kotlin-codepoints/src/commonMain/kotlin/CharSequenceExtensions.kt +++ b/kotlin-codepoints/src/commonMain/kotlin/CharSequenceExtensions.kt @@ -76,6 +76,13 @@ fun CharSequence.codePointBefore(index: Int): Int { return firstChar.code } +/** + * Returns the number of Unicode code points in this `CharSequence`. + */ +fun CharSequence.codePointCount(): Int { + return codePointCount(beginIndex = 0, endIndex = length) +} + /** * Returns the number of Unicode code points in the specified text range of this `CharSequence`. * diff --git a/kotlin-codepoints/src/commonTest/kotlin/CharSequenceExtensionsTest.kt b/kotlin-codepoints/src/commonTest/kotlin/CharSequenceExtensionsTest.kt index 8d36622..f3852dc 100644 --- a/kotlin-codepoints/src/commonTest/kotlin/CharSequenceExtensionsTest.kt +++ b/kotlin-codepoints/src/commonTest/kotlin/CharSequenceExtensionsTest.kt @@ -61,12 +61,17 @@ class CharSequenceExtensionsTest { @Test fun codePointCount() { + assertEquals(0, "".codePointCount()) assertEquals(0, "".codePointCount(beginIndex = 0, endIndex = 0)) assertEquals(0, "abc".codePointCount(beginIndex = 1, endIndex = 1)) + assertEquals(3, "abc".codePointCount()) assertEquals(3, "abc".codePointCount(beginIndex = 0, endIndex = 3)) + assertEquals(2, "a\uFFFF".codePointCount()) assertEquals(2, "a\uFFFF".codePointCount(beginIndex = 0, endIndex = 2)) + assertEquals(1, "\uD83E\uDD95".codePointCount()) assertEquals(1, "\uD83E\uDD95".codePointCount(beginIndex = 0, endIndex = 2)) + assertEquals(2, "\uD83E\uDD95\uD83E\uDD96".codePointCount()) assertEquals(2, "\uD83E\uDD95\uD83E\uDD96".codePointCount(beginIndex = 0, endIndex = 4)) assertEquals(2, "abc".codePointCount(beginIndex = 1, endIndex = 3)) @@ -80,7 +85,9 @@ class CharSequenceExtensionsTest { @Test fun codePointCount_with_unmatched_surrogates() { + assertEquals(2, "\uDD95\uD83E".codePointCount()) assertEquals(2, "\uDD95\uD83E".codePointCount(beginIndex = 0, endIndex = 2)) + assertEquals(3, "\uDD95\uD83E\uDD95\uD83E".codePointCount()) assertEquals(3, "\uDD95\uD83E\uDD95\uD83E".codePointCount(beginIndex = 0, endIndex = 4)) assertEquals(1, "\uDD95\uD83E".codePointCount(beginIndex = 1, endIndex = 2))