Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
- Move common implementation to its own package
- Remove useless objects in common implementation
- Remove inline keyword from most methods
  • Loading branch information
cketti committed Jan 21, 2023
1 parent 14ed5cf commit fca12e7
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 193 deletions.
52 changes: 32 additions & 20 deletions src/commonImplementation/kotlin/CodePoints.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,60 @@

package de.cketti.codepoints

import de.cketti.codepoints.internal.isValidCodePoint as commonIsValidCodePoint
import de.cketti.codepoints.internal.isBmpCodePoint as commonIsBmpCodePoint
import de.cketti.codepoints.internal.isSupplementaryCodePoint as commonIsSupplementaryCodePoint
import de.cketti.codepoints.internal.charCount as commonCharCount
import de.cketti.codepoints.internal.isSurrogate as commonIsSurrogate
import de.cketti.codepoints.internal.isHighSurrogate as commonIsHighSurrogate
import de.cketti.codepoints.internal.isLowSurrogate as commonIsLowSurrogate
import de.cketti.codepoints.internal.isSurrogatePair as commonIsSurrogatePair
import de.cketti.codepoints.internal.highSurrogate as commonHighSurrogate
import de.cketti.codepoints.internal.lowSurrogate as commonLowSurrogate
import de.cketti.codepoints.internal.toCodePoint as commonToCodePoint

actual object CodePoints {
actual inline fun isValidCodePoint(codePoint: Int): Boolean {
return CommonCodePoints.isValidCodePoint(codePoint)
actual fun isValidCodePoint(codePoint: Int): Boolean {
return commonIsValidCodePoint(codePoint)
}

actual inline fun isBmpCodePoint(codePoint: Int): Boolean {
return CommonCodePoints.isBmpCodePoint(codePoint)
actual fun isBmpCodePoint(codePoint: Int): Boolean {
return commonIsBmpCodePoint(codePoint)
}

actual inline fun isSupplementaryCodePoint(codePoint: Int): Boolean {
return CommonCodePoints.isSupplementaryCodePoint(codePoint)
actual fun isSupplementaryCodePoint(codePoint: Int): Boolean {
return commonIsSupplementaryCodePoint(codePoint)
}

actual inline fun charCount(codePoint: Int): Int {
return CommonCodePoints.charCount(codePoint)
actual fun charCount(codePoint: Int): Int {
return commonCharCount(codePoint)
}

actual inline fun isSurrogate(char: Char): Boolean {
return CommonCodePoints.isSurrogate(char)
actual fun isSurrogate(char: Char): Boolean {
return commonIsSurrogate(char)
}

actual inline fun isHighSurrogate(char: Char): Boolean {
return CommonCodePoints.isHighSurrogate(char)
actual fun isHighSurrogate(char: Char): Boolean {
return commonIsHighSurrogate(char)
}

actual inline fun isLowSurrogate(char: Char): Boolean {
return CommonCodePoints.isLowSurrogate(char)
actual fun isLowSurrogate(char: Char): Boolean {
return commonIsLowSurrogate(char)
}

actual inline fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean {
return CommonCodePoints.isSurrogatePair(highSurrogate, lowSurrogate)
actual fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean {
return commonIsSurrogatePair(highSurrogate, lowSurrogate)
}

actual fun highSurrogate(codePoint: Int): Char {
return CommonCodePoints.highSurrogate(codePoint)
return commonHighSurrogate(codePoint)
}

actual fun lowSurrogate(codePoint: Int): Char {
return CommonCodePoints.lowSurrogate(codePoint)
return commonLowSurrogate(codePoint)
}

actual inline fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int {
return CommonCodePoints.toCodePoint(highSurrogate, lowSurrogate)
actual fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int {
return commonToCodePoint(highSurrogate, lowSurrogate)
}
}
61 changes: 0 additions & 61 deletions src/commonImplementation/kotlin/CommonCodePoints.kt

This file was deleted.

94 changes: 0 additions & 94 deletions src/commonImplementation/kotlin/CommonStringFunctions.kt

This file was deleted.

21 changes: 13 additions & 8 deletions src/commonImplementation/kotlin/StringExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

package de.cketti.codepoints

actual inline fun String.codePointAt(index: Int): Int {
return CommonStringFunctions.codePointAt(this, index)
import de.cketti.codepoints.internal.codePointAt as commonCodePointAt
import de.cketti.codepoints.internal.codePointBefore as commonCodePointBefore
import de.cketti.codepoints.internal.codePointCount as commonCodePointCount
import de.cketti.codepoints.internal.offsetByCodePoints as commonOffsetByCodePoints

actual fun String.codePointAt(index: Int): Int {
return commonCodePointAt(this, index)
}

actual inline fun String.codePointBefore(index: Int): Int {
return CommonStringFunctions.codePointBefore(this, index)
actual fun String.codePointBefore(index: Int): Int {
return commonCodePointBefore(this, index)
}

actual inline fun String.codePointCount(beginIndex: Int, endIndex: Int): Int {
return CommonStringFunctions.codePointCount(this, beginIndex, endIndex)
actual fun String.codePointCount(beginIndex: Int, endIndex: Int): Int {
return commonCodePointCount(this, beginIndex, endIndex)
}

actual inline fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int {
return CommonStringFunctions.offsetByCodePoints(this, index, codePointOffset)
actual fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int {
return commonOffsetByCodePoints(this, index, codePointOffset)
}
59 changes: 59 additions & 0 deletions src/commonImplementation/kotlin/internal/CommonCodePoints.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 MAX_HIGH_SURROGATE = 0xDBFF
private const val MIN_LOW_SURROGATE = 0xDC00
private const val MAX_LOW_SURROGATE = 0xDFFF

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 isSurrogate(char: Char): Boolean {
return char.code in MIN_HIGH_SURROGATE..MAX_LOW_SURROGATE
}

internal fun isHighSurrogate(char: Char): Boolean {
return char.code in MIN_HIGH_SURROGATE..MAX_HIGH_SURROGATE
}

internal fun isLowSurrogate(char: Char): Boolean {
return char.code in MIN_LOW_SURROGATE..MAX_LOW_SURROGATE
}

internal fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean {
return isHighSurrogate(highSurrogate) && isLowSurrogate(lowSurrogate)
}

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
}
Loading

0 comments on commit fca12e7

Please sign in to comment.