Skip to content

Commit df119f7

Browse files
authored
Merge pull request #7 from cketti/refactor
Reshuffle code
2 parents f38b5e6 + d38e2b1 commit df119f7

File tree

9 files changed

+216
-208
lines changed

9 files changed

+216
-208
lines changed

src/commonImplementation/kotlin/CodePoints.kt

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,54 @@
22

33
package de.cketti.codepoints
44

5+
import de.cketti.codepoints.internal.charCount as commonCharCount
6+
import de.cketti.codepoints.internal.highSurrogate as commonHighSurrogate
7+
import de.cketti.codepoints.internal.isBmpCodePoint as commonIsBmpCodePoint
8+
import de.cketti.codepoints.internal.isSupplementaryCodePoint as commonIsSupplementaryCodePoint
9+
import de.cketti.codepoints.internal.isSurrogatePair as commonIsSurrogatePair
10+
import de.cketti.codepoints.internal.isValidCodePoint as commonIsValidCodePoint
11+
import de.cketti.codepoints.internal.lowSurrogate as commonLowSurrogate
12+
import de.cketti.codepoints.internal.toChars as commonToChars
13+
import de.cketti.codepoints.internal.toCodePoint as commonToCodePoint
14+
515
actual object CodePoints {
6-
actual inline fun isValidCodePoint(codePoint: Int): Boolean {
7-
return CommonCodePoints.isValidCodePoint(codePoint)
16+
actual fun isValidCodePoint(codePoint: Int): Boolean {
17+
return commonIsValidCodePoint(codePoint)
818
}
919

10-
actual inline fun isBmpCodePoint(codePoint: Int): Boolean {
11-
return CommonCodePoints.isBmpCodePoint(codePoint)
20+
actual fun isBmpCodePoint(codePoint: Int): Boolean {
21+
return commonIsBmpCodePoint(codePoint)
1222
}
1323

14-
actual inline fun isSupplementaryCodePoint(codePoint: Int): Boolean {
15-
return CommonCodePoints.isSupplementaryCodePoint(codePoint)
24+
actual fun isSupplementaryCodePoint(codePoint: Int): Boolean {
25+
return commonIsSupplementaryCodePoint(codePoint)
1626
}
1727

18-
actual inline fun charCount(codePoint: Int): Int {
19-
return CommonCodePoints.charCount(codePoint)
28+
actual fun charCount(codePoint: Int): Int {
29+
return commonCharCount(codePoint)
2030
}
2131

22-
actual inline fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean {
23-
return CommonCodePoints.isSurrogatePair(highSurrogate, lowSurrogate)
32+
actual fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean {
33+
return commonIsSurrogatePair(highSurrogate, lowSurrogate)
2434
}
2535

2636
actual fun highSurrogate(codePoint: Int): Char {
27-
return CommonCodePoints.highSurrogate(codePoint)
37+
return commonHighSurrogate(codePoint)
2838
}
2939

3040
actual fun lowSurrogate(codePoint: Int): Char {
31-
return CommonCodePoints.lowSurrogate(codePoint)
41+
return commonLowSurrogate(codePoint)
3242
}
3343

34-
actual inline fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int {
35-
return CommonCodePoints.toCodePoint(highSurrogate, lowSurrogate)
44+
actual fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int {
45+
return commonToCodePoint(highSurrogate, lowSurrogate)
3646
}
3747

38-
actual inline fun toChars(codePoint: Int): CharArray {
39-
return CommonCodePoints.toChars(codePoint)
48+
actual fun toChars(codePoint: Int): CharArray {
49+
return commonToChars(codePoint)
4050
}
4151

42-
actual inline fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int {
43-
return CommonCodePoints.toChars(codePoint, destination, offset)
52+
actual fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int {
53+
return commonToChars(codePoint, destination, offset)
4454
}
4555
}

src/commonImplementation/kotlin/CommonCodePoints.kt

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/commonImplementation/kotlin/CommonStringBuilderFunctions.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/commonImplementation/kotlin/CommonStringFunctions.kt

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package de.cketti.codepoints
22

33
import kotlin.text.StringBuilder
4+
import de.cketti.codepoints.internal.appendCodePoint as commonAppendCodePoint
45

56
actual fun StringBuilder.appendCodePoint(codePoint: Int): StringBuilder = apply {
6-
CommonStringBuilderFunctions.appendCodePoint(this, codePoint)
7+
commonAppendCodePoint(this, codePoint)
78
}

src/commonImplementation/kotlin/StringExtensions.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
package de.cketti.codepoints
44

5-
actual inline fun String.codePointAt(index: Int): Int {
6-
return CommonStringFunctions.codePointAt(this, index)
5+
import de.cketti.codepoints.internal.codePointAt as commonCodePointAt
6+
import de.cketti.codepoints.internal.codePointBefore as commonCodePointBefore
7+
import de.cketti.codepoints.internal.codePointCount as commonCodePointCount
8+
import de.cketti.codepoints.internal.offsetByCodePoints as commonOffsetByCodePoints
9+
10+
actual fun String.codePointAt(index: Int): Int {
11+
return commonCodePointAt(this, index)
712
}
813

9-
actual inline fun String.codePointBefore(index: Int): Int {
10-
return CommonStringFunctions.codePointBefore(this, index)
14+
actual fun String.codePointBefore(index: Int): Int {
15+
return commonCodePointBefore(this, index)
1116
}
1217

13-
actual inline fun String.codePointCount(beginIndex: Int, endIndex: Int): Int {
14-
return CommonStringFunctions.codePointCount(this, beginIndex, endIndex)
18+
actual fun String.codePointCount(beginIndex: Int, endIndex: Int): Int {
19+
return commonCodePointCount(this, beginIndex, endIndex)
1520
}
1621

17-
actual inline fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int {
18-
return CommonStringFunctions.offsetByCodePoints(this, index, codePointOffset)
22+
actual fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int {
23+
return commonOffsetByCodePoints(this, index, codePointOffset)
1924
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package de.cketti.codepoints.internal
2+
3+
private const val MIN_SUPPLEMENTARY_CODE_POINT = 0x10000
4+
private const val MAX_CODE_POINT = 0x10FFFF
5+
6+
private const val MIN_HIGH_SURROGATE = 0xD800
7+
private const val MIN_LOW_SURROGATE = 0xDC00
8+
9+
private const val SURROGATE_DECODE_OFFSET =
10+
MIN_SUPPLEMENTARY_CODE_POINT - (MIN_HIGH_SURROGATE shl 10) - MIN_LOW_SURROGATE
11+
12+
private const val HIGH_SURROGATE_ENCODE_OFFSET =
13+
(MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT ushr 10))
14+
15+
internal fun isValidCodePoint(codePoint: Int): Boolean {
16+
return codePoint in 0..MAX_CODE_POINT
17+
}
18+
19+
internal fun isBmpCodePoint(codePoint: Int): Boolean {
20+
return codePoint ushr 16 == 0
21+
}
22+
23+
internal fun isSupplementaryCodePoint(codePoint: Int): Boolean {
24+
return codePoint in MIN_SUPPLEMENTARY_CODE_POINT..MAX_CODE_POINT
25+
}
26+
27+
internal fun charCount(codePoint: Int): Int {
28+
return if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) 1 else 2
29+
}
30+
31+
internal fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean {
32+
return highSurrogate.isHighSurrogate() && lowSurrogate.isLowSurrogate()
33+
}
34+
35+
internal fun highSurrogate(codePoint: Int): Char {
36+
return ((codePoint ushr 10) + HIGH_SURROGATE_ENCODE_OFFSET).toChar()
37+
}
38+
39+
internal fun lowSurrogate(codePoint: Int): Char {
40+
return ((codePoint and 0x3FF) + MIN_LOW_SURROGATE).toChar()
41+
}
42+
43+
internal fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int {
44+
return (highSurrogate.code shl 10) + lowSurrogate.code + SURROGATE_DECODE_OFFSET
45+
}
46+
47+
internal fun toChars(codePoint: Int): CharArray {
48+
return if (isBmpCodePoint(codePoint)) {
49+
charArrayOf(codePoint.toChar())
50+
} else {
51+
charArrayOf(highSurrogate(codePoint), lowSurrogate(codePoint))
52+
}
53+
}
54+
55+
internal fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int {
56+
if (isBmpCodePoint(codePoint)) {
57+
destination.setSafe(offset, codePoint.toChar())
58+
return 1
59+
} else {
60+
// When writing the low surrogate succeeds but writing the high surrogate fails (offset = -1), the
61+
// destination will be modified even though the method throws. This feels wrong, but matches the behavior
62+
// of the Java stdlib implementation.
63+
destination.setSafe(offset + 1, lowSurrogate(codePoint))
64+
destination.setSafe(offset, highSurrogate(codePoint))
65+
return 2
66+
}
67+
}
68+
69+
private fun CharArray.setSafe(index: Int, value: Char) {
70+
if (index !in this.indices) {
71+
throw IndexOutOfBoundsException("Size: $size, offset: $index")
72+
}
73+
74+
this[index] = value
75+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package de.cketti.codepoints.internal
2+
3+
internal fun appendCodePoint(builder: StringBuilder, codePoint: Int) {
4+
if (isBmpCodePoint(codePoint)) {
5+
builder.append(codePoint.toChar())
6+
} else {
7+
builder.append(highSurrogate(codePoint))
8+
builder.append(lowSurrogate(codePoint))
9+
}
10+
}

0 commit comments

Comments
 (0)