Skip to content

Commit 6516c51

Browse files
committed
Remove useless objects in common implementation
1 parent 1eb7526 commit 6516c51

File tree

6 files changed

+165
-162
lines changed

6 files changed

+165
-162
lines changed

src/commonImplementation/kotlin/CodePoints.kt

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,54 @@
22

33
package de.cketti.codepoints
44

5-
import de.cketti.codepoints.internal.CommonCodePoints
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
614

715
actual object CodePoints {
816
actual inline fun isValidCodePoint(codePoint: Int): Boolean {
9-
return CommonCodePoints.isValidCodePoint(codePoint)
17+
return commonIsValidCodePoint(codePoint)
1018
}
1119

1220
actual inline fun isBmpCodePoint(codePoint: Int): Boolean {
13-
return CommonCodePoints.isBmpCodePoint(codePoint)
21+
return commonIsBmpCodePoint(codePoint)
1422
}
1523

1624
actual inline fun isSupplementaryCodePoint(codePoint: Int): Boolean {
17-
return CommonCodePoints.isSupplementaryCodePoint(codePoint)
25+
return commonIsSupplementaryCodePoint(codePoint)
1826
}
1927

2028
actual inline fun charCount(codePoint: Int): Int {
21-
return CommonCodePoints.charCount(codePoint)
29+
return commonCharCount(codePoint)
2230
}
2331

2432
actual inline fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean {
25-
return CommonCodePoints.isSurrogatePair(highSurrogate, lowSurrogate)
33+
return commonIsSurrogatePair(highSurrogate, lowSurrogate)
2634
}
2735

2836
actual fun highSurrogate(codePoint: Int): Char {
29-
return CommonCodePoints.highSurrogate(codePoint)
37+
return commonHighSurrogate(codePoint)
3038
}
3139

3240
actual fun lowSurrogate(codePoint: Int): Char {
33-
return CommonCodePoints.lowSurrogate(codePoint)
41+
return commonLowSurrogate(codePoint)
3442
}
3543

3644
actual inline fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int {
37-
return CommonCodePoints.toCodePoint(highSurrogate, lowSurrogate)
45+
return commonToCodePoint(highSurrogate, lowSurrogate)
3846
}
3947

4048
actual inline fun toChars(codePoint: Int): CharArray {
41-
return CommonCodePoints.toChars(codePoint)
49+
return commonToChars(codePoint)
4250
}
4351

4452
actual inline fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int {
45-
return CommonCodePoints.toChars(codePoint, destination, offset)
53+
return commonToChars(codePoint, destination, offset)
4654
}
4755
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package de.cketti.codepoints
22

3-
import de.cketti.codepoints.internal.CommonStringBuilderFunctions
43
import kotlin.text.StringBuilder
4+
import de.cketti.codepoints.internal.appendCodePoint as commonAppendCodePoint
55

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

src/commonImplementation/kotlin/StringExtensions.kt

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

33
package de.cketti.codepoints
44

5-
import de.cketti.codepoints.internal.CommonStringFunctions
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
69

710
actual inline fun String.codePointAt(index: Int): Int {
8-
return CommonStringFunctions.codePointAt(this, index)
11+
return commonCodePointAt(this, index)
912
}
1013

1114
actual inline fun String.codePointBefore(index: Int): Int {
12-
return CommonStringFunctions.codePointBefore(this, index)
15+
return commonCodePointBefore(this, index)
1316
}
1417

1518
actual inline fun String.codePointCount(beginIndex: Int, endIndex: Int): Int {
16-
return CommonStringFunctions.codePointCount(this, beginIndex, endIndex)
19+
return commonCodePointCount(this, beginIndex, endIndex)
1720
}
1821

1922
actual inline fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int {
20-
return CommonStringFunctions.offsetByCodePoints(this, index, codePointOffset)
23+
return commonOffsetByCodePoints(this, index, codePointOffset)
2124
}
Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,75 @@
11
package de.cketti.codepoints.internal
22

3-
object CommonCodePoints {
4-
private const val MIN_SUPPLEMENTARY_CODE_POINT = 0x10000
5-
private const val MAX_CODE_POINT = 0x10FFFF
3+
private const val MIN_SUPPLEMENTARY_CODE_POINT = 0x10000
4+
private const val MAX_CODE_POINT = 0x10FFFF
65

7-
private const val MIN_HIGH_SURROGATE = 0xD800
8-
private const val MIN_LOW_SURROGATE = 0xDC00
6+
private const val MIN_HIGH_SURROGATE = 0xD800
7+
private const val MIN_LOW_SURROGATE = 0xDC00
98

10-
private const val SURROGATE_DECODE_OFFSET =
11-
MIN_SUPPLEMENTARY_CODE_POINT - (MIN_HIGH_SURROGATE shl 10) - MIN_LOW_SURROGATE
9+
private const val SURROGATE_DECODE_OFFSET =
10+
MIN_SUPPLEMENTARY_CODE_POINT - (MIN_HIGH_SURROGATE shl 10) - MIN_LOW_SURROGATE
1211

13-
private const val HIGH_SURROGATE_ENCODE_OFFSET =
14-
(MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT ushr 10))
12+
private const val HIGH_SURROGATE_ENCODE_OFFSET =
13+
(MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT ushr 10))
1514

16-
fun isValidCodePoint(codePoint: Int): Boolean {
17-
return codePoint in 0..MAX_CODE_POINT
18-
}
19-
20-
fun isBmpCodePoint(codePoint: Int): Boolean {
21-
return codePoint ushr 16 == 0
22-
}
23-
24-
fun isSupplementaryCodePoint(codePoint: Int): Boolean {
25-
return codePoint in MIN_SUPPLEMENTARY_CODE_POINT..MAX_CODE_POINT
26-
}
27-
28-
fun charCount(codePoint: Int): Int {
29-
return if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) 1 else 2
30-
}
31-
32-
fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean {
33-
return highSurrogate.isHighSurrogate() && lowSurrogate.isLowSurrogate()
34-
}
15+
fun isValidCodePoint(codePoint: Int): Boolean {
16+
return codePoint in 0..MAX_CODE_POINT
17+
}
3518

36-
fun highSurrogate(codePoint: Int): Char {
37-
return ((codePoint ushr 10) + HIGH_SURROGATE_ENCODE_OFFSET).toChar()
38-
}
39-
40-
fun lowSurrogate(codePoint: Int): Char {
41-
return ((codePoint and 0x3FF) + MIN_LOW_SURROGATE).toChar()
42-
}
43-
44-
fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int {
45-
return (highSurrogate.code shl 10) + lowSurrogate.code + SURROGATE_DECODE_OFFSET
46-
}
19+
fun isBmpCodePoint(codePoint: Int): Boolean {
20+
return codePoint ushr 16 == 0
21+
}
4722

48-
fun toChars(codePoint: Int): CharArray {
49-
return if (isBmpCodePoint(codePoint)) {
50-
charArrayOf(codePoint.toChar())
51-
} else {
52-
charArrayOf(highSurrogate(codePoint), lowSurrogate(codePoint))
53-
}
23+
fun isSupplementaryCodePoint(codePoint: Int): Boolean {
24+
return codePoint in MIN_SUPPLEMENTARY_CODE_POINT..MAX_CODE_POINT
25+
}
26+
27+
fun charCount(codePoint: Int): Int {
28+
return if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) 1 else 2
29+
}
30+
31+
fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean {
32+
return highSurrogate.isHighSurrogate() && lowSurrogate.isLowSurrogate()
33+
}
34+
35+
fun highSurrogate(codePoint: Int): Char {
36+
return ((codePoint ushr 10) + HIGH_SURROGATE_ENCODE_OFFSET).toChar()
37+
}
38+
39+
fun lowSurrogate(codePoint: Int): Char {
40+
return ((codePoint and 0x3FF) + MIN_LOW_SURROGATE).toChar()
41+
}
42+
43+
fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int {
44+
return (highSurrogate.code shl 10) + lowSurrogate.code + SURROGATE_DECODE_OFFSET
45+
}
46+
47+
fun toChars(codePoint: Int): CharArray {
48+
return if (isBmpCodePoint(codePoint)) {
49+
charArrayOf(codePoint.toChar())
50+
} else {
51+
charArrayOf(highSurrogate(codePoint), lowSurrogate(codePoint))
5452
}
53+
}
5554

56-
fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int {
57-
if (isBmpCodePoint(codePoint)) {
58-
destination.setSafe(offset, codePoint.toChar())
59-
return 1
60-
} else {
61-
// When writing the low surrogate succeeds but writing the high surrogate fails (offset = -1), the
62-
// destination will be modified even though the method throws. This feels wrong, but matches the behavior
63-
// of the Java stdlib implementation.
64-
destination.setSafe(offset + 1, lowSurrogate(codePoint))
65-
destination.setSafe(offset, highSurrogate(codePoint))
66-
return 2
67-
}
55+
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
6866
}
69-
70-
private fun CharArray.setSafe(index: Int, value: Char) {
71-
if (index !in this.indices) {
72-
throw IndexOutOfBoundsException("Size: $size, offset: $index")
73-
}
67+
}
7468

75-
this[index] = value
69+
private fun CharArray.setSafe(index: Int, value: Char) {
70+
if (index !in this.indices) {
71+
throw IndexOutOfBoundsException("Size: $size, offset: $index")
7672
}
73+
74+
this[index] = value
7775
}

src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package de.cketti.codepoints.internal
22

33
import de.cketti.codepoints.CodePoints
44

5-
object CommonStringBuilderFunctions {
6-
fun appendCodePoint(builder: StringBuilder, codePoint: Int) {
7-
if (CodePoints.isBmpCodePoint(codePoint)) {
8-
builder.append(codePoint.toChar())
9-
} else {
10-
builder.append(CodePoints.highSurrogate(codePoint))
11-
builder.append(CodePoints.lowSurrogate(codePoint))
12-
}
5+
fun appendCodePoint(builder: StringBuilder, codePoint: Int) {
6+
if (CodePoints.isBmpCodePoint(codePoint)) {
7+
builder.append(codePoint.toChar())
8+
} else {
9+
builder.append(CodePoints.highSurrogate(codePoint))
10+
builder.append(CodePoints.lowSurrogate(codePoint))
1311
}
1412
}

0 commit comments

Comments
 (0)