Skip to content

Commit

Permalink
Merge pull request #36 from cketti/no_argument_codePointCount
Browse files Browse the repository at this point in the history
Add zero parameter variant of `CharSequence.codePointCount()`
  • Loading branch information
cketti authored Jun 23, 2024
2 parents 342112a + feaf18b commit b0de8a0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down

0 comments on commit b0de8a0

Please sign in to comment.