-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from cketti/refactor
Reshuffle code
- Loading branch information
Showing
9 changed files
with
216 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
src/commonImplementation/kotlin/CommonStringBuilderFunctions.kt
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package de.cketti.codepoints | ||
|
||
import kotlin.text.StringBuilder | ||
import de.cketti.codepoints.internal.appendCodePoint as commonAppendCodePoint | ||
|
||
actual fun StringBuilder.appendCodePoint(codePoint: Int): StringBuilder = apply { | ||
CommonStringBuilderFunctions.appendCodePoint(this, codePoint) | ||
commonAppendCodePoint(this, codePoint) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/commonImplementation/kotlin/internal/CommonCodePoints.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package de.cketti.codepoints.internal | ||
|
||
private const val MIN_SUPPLEMENTARY_CODE_POINT = 0x10000 | ||
private const val MAX_CODE_POINT = 0x10FFFF | ||
|
||
private const val MIN_HIGH_SURROGATE = 0xD800 | ||
private const val MIN_LOW_SURROGATE = 0xDC00 | ||
|
||
private const val SURROGATE_DECODE_OFFSET = | ||
MIN_SUPPLEMENTARY_CODE_POINT - (MIN_HIGH_SURROGATE shl 10) - MIN_LOW_SURROGATE | ||
|
||
private const val HIGH_SURROGATE_ENCODE_OFFSET = | ||
(MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT ushr 10)) | ||
|
||
internal fun isValidCodePoint(codePoint: Int): Boolean { | ||
return codePoint in 0..MAX_CODE_POINT | ||
} | ||
|
||
internal fun isBmpCodePoint(codePoint: Int): Boolean { | ||
return codePoint ushr 16 == 0 | ||
} | ||
|
||
internal fun isSupplementaryCodePoint(codePoint: Int): Boolean { | ||
return codePoint in MIN_SUPPLEMENTARY_CODE_POINT..MAX_CODE_POINT | ||
} | ||
|
||
internal fun charCount(codePoint: Int): Int { | ||
return if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) 1 else 2 | ||
} | ||
|
||
internal fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean { | ||
return highSurrogate.isHighSurrogate() && lowSurrogate.isLowSurrogate() | ||
} | ||
|
||
internal fun highSurrogate(codePoint: Int): Char { | ||
return ((codePoint ushr 10) + HIGH_SURROGATE_ENCODE_OFFSET).toChar() | ||
} | ||
|
||
internal fun lowSurrogate(codePoint: Int): Char { | ||
return ((codePoint and 0x3FF) + MIN_LOW_SURROGATE).toChar() | ||
} | ||
|
||
internal fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int { | ||
return (highSurrogate.code shl 10) + lowSurrogate.code + SURROGATE_DECODE_OFFSET | ||
} | ||
|
||
internal fun toChars(codePoint: Int): CharArray { | ||
return if (isBmpCodePoint(codePoint)) { | ||
charArrayOf(codePoint.toChar()) | ||
} else { | ||
charArrayOf(highSurrogate(codePoint), lowSurrogate(codePoint)) | ||
} | ||
} | ||
|
||
internal fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int { | ||
if (isBmpCodePoint(codePoint)) { | ||
destination.setSafe(offset, codePoint.toChar()) | ||
return 1 | ||
} else { | ||
// When writing the low surrogate succeeds but writing the high surrogate fails (offset = -1), the | ||
// destination will be modified even though the method throws. This feels wrong, but matches the behavior | ||
// of the Java stdlib implementation. | ||
destination.setSafe(offset + 1, lowSurrogate(codePoint)) | ||
destination.setSafe(offset, highSurrogate(codePoint)) | ||
return 2 | ||
} | ||
} | ||
|
||
private fun CharArray.setSafe(index: Int, value: Char) { | ||
if (index !in this.indices) { | ||
throw IndexOutOfBoundsException("Size: $size, offset: $index") | ||
} | ||
|
||
this[index] = value | ||
} |
10 changes: 10 additions & 0 deletions
10
src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package de.cketti.codepoints.internal | ||
|
||
internal fun appendCodePoint(builder: StringBuilder, codePoint: Int) { | ||
if (isBmpCodePoint(codePoint)) { | ||
builder.append(codePoint.toChar()) | ||
} else { | ||
builder.append(highSurrogate(codePoint)) | ||
builder.append(lowSurrogate(codePoint)) | ||
} | ||
} |
Oops, something went wrong.