|
1 | 1 | package de.cketti.codepoints.internal |
2 | 2 |
|
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 |
6 | 5 |
|
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 |
9 | 8 |
|
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 |
12 | 11 |
|
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)) |
15 | 14 |
|
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 | +} |
35 | 18 |
|
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 | +} |
47 | 22 |
|
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)) |
54 | 52 | } |
| 53 | +} |
55 | 54 |
|
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 |
68 | 66 | } |
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 | +} |
74 | 68 |
|
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") |
76 | 72 | } |
| 73 | + |
| 74 | + this[index] = value |
77 | 75 | } |
0 commit comments