Skip to content

Commit 342112a

Browse files
authored
Merge pull request #37 from cketti/toString
Change `CodePoint.toString()` to return the string representation
2 parents 20b40c0 + 9ec6fa9 commit 342112a

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## [Unreleased]
4+
### Changed
5+
- `CodePoint.toString()` now returns the string representation of a code point.
6+
7+
### Added
8+
- `CodePoint.toUnicodeNotation()` returns the standard Unicode notation of a code point, e.g. `U+1F4E7`.
9+
310
## [0.8.0] - 2024-06-09
411
### Changed
512
- Updated to Kotlin 2.0.0

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ to get and idea of what's available.
6464
val text = "🦕&🦖"
6565

6666
for (codePoint in text.codePointSequence()) {
67-
print("code point: $codePoint, char count: ${codePoint.charCount}")
67+
print("code point: ${codePoint.toUnicodeNotation()}, char count: ${codePoint.charCount}")
6868

6969
if (codePoint.isBasic) {
7070
println(" - boring!")

kotlin-codepoints-deluxe/src/commonMain/kotlin/CodePoint.kt

+11-2
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,23 @@ value class CodePoint internal constructor(val value: Int) {
102102
}
103103

104104
/**
105-
* Returns a string representation of this code point.
105+
* Returns the standard Unicode notation of this code point.
106106
*
107107
* "U+" followed by the code point value in hexadecimal (using upper case letters), which is prepended with leading
108108
* zeros to a minimum of four digits.
109109
*/
110-
override fun toString(): String {
110+
fun toUnicodeNotation(): String {
111111
return "U+${value.toString(16).uppercase().padStart(4, '0')}"
112112
}
113+
114+
/**
115+
* Returns the string representation of this code point.
116+
*
117+
* The returned string consists of the sequence of characters returned by [toChars].
118+
*/
119+
override fun toString(): String {
120+
return toChars().concatToString()
121+
}
113122
}
114123

115124
/**

kotlin-codepoints-deluxe/src/commonTest/kotlin/CodePointTest.kt

+15
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,19 @@ class CodePointTest {
165165
}
166166
assertContentEquals(charArrayOf('z', 'z'), chars)
167167
}
168+
169+
@Test
170+
fun toUnicodeNotation() {
171+
assertEquals("U+0000", 0.toCodePoint().toUnicodeNotation())
172+
assertEquals("U+0061", 'a'.toCodePoint().toUnicodeNotation())
173+
assertEquals("U+0FFF", 0xFFF.toCodePoint().toUnicodeNotation())
174+
assertEquals("U+FFFF", 0xFFFF.toCodePoint().toUnicodeNotation())
175+
assertEquals("U+1F995", 0x1F995.toCodePoint().toUnicodeNotation())
176+
}
177+
178+
@Test
179+
fun toString_test() {
180+
assertEquals("a", 'a'.toCodePoint().toString())
181+
assertEquals("\uD83E\uDD95", 0x1F995.toCodePoint().toString())
182+
}
168183
}

0 commit comments

Comments
 (0)