From 1eb75265fde5038b057da74393fc069e1d16322d Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 24 Jan 2023 16:34:30 +0100 Subject: [PATCH 1/4] Move common implementation to `internal` package --- src/commonImplementation/kotlin/CodePoints.kt | 2 ++ src/commonImplementation/kotlin/StringBuilderExtensions.kt | 1 + src/commonImplementation/kotlin/StringExtensions.kt | 2 ++ .../kotlin/{ => internal}/CommonCodePoints.kt | 2 +- .../kotlin/{ => internal}/CommonStringBuilderFunctions.kt | 4 +++- .../kotlin/{ => internal}/CommonStringFunctions.kt | 4 ++-- 6 files changed, 11 insertions(+), 4 deletions(-) rename src/commonImplementation/kotlin/{ => internal}/CommonCodePoints.kt (98%) rename src/commonImplementation/kotlin/{ => internal}/CommonStringBuilderFunctions.kt (82%) rename src/commonImplementation/kotlin/{ => internal}/CommonStringFunctions.kt (96%) diff --git a/src/commonImplementation/kotlin/CodePoints.kt b/src/commonImplementation/kotlin/CodePoints.kt index efe6e35..f8b6587 100644 --- a/src/commonImplementation/kotlin/CodePoints.kt +++ b/src/commonImplementation/kotlin/CodePoints.kt @@ -2,6 +2,8 @@ package de.cketti.codepoints +import de.cketti.codepoints.internal.CommonCodePoints + actual object CodePoints { actual inline fun isValidCodePoint(codePoint: Int): Boolean { return CommonCodePoints.isValidCodePoint(codePoint) diff --git a/src/commonImplementation/kotlin/StringBuilderExtensions.kt b/src/commonImplementation/kotlin/StringBuilderExtensions.kt index cc25633..b628fdd 100644 --- a/src/commonImplementation/kotlin/StringBuilderExtensions.kt +++ b/src/commonImplementation/kotlin/StringBuilderExtensions.kt @@ -1,5 +1,6 @@ package de.cketti.codepoints +import de.cketti.codepoints.internal.CommonStringBuilderFunctions import kotlin.text.StringBuilder actual fun StringBuilder.appendCodePoint(codePoint: Int): StringBuilder = apply { diff --git a/src/commonImplementation/kotlin/StringExtensions.kt b/src/commonImplementation/kotlin/StringExtensions.kt index eef41e2..41987dc 100644 --- a/src/commonImplementation/kotlin/StringExtensions.kt +++ b/src/commonImplementation/kotlin/StringExtensions.kt @@ -2,6 +2,8 @@ package de.cketti.codepoints +import de.cketti.codepoints.internal.CommonStringFunctions + actual inline fun String.codePointAt(index: Int): Int { return CommonStringFunctions.codePointAt(this, index) } diff --git a/src/commonImplementation/kotlin/CommonCodePoints.kt b/src/commonImplementation/kotlin/internal/CommonCodePoints.kt similarity index 98% rename from src/commonImplementation/kotlin/CommonCodePoints.kt rename to src/commonImplementation/kotlin/internal/CommonCodePoints.kt index b546c1c..dbf8e8c 100644 --- a/src/commonImplementation/kotlin/CommonCodePoints.kt +++ b/src/commonImplementation/kotlin/internal/CommonCodePoints.kt @@ -1,4 +1,4 @@ -package de.cketti.codepoints +package de.cketti.codepoints.internal object CommonCodePoints { private const val MIN_SUPPLEMENTARY_CODE_POINT = 0x10000 diff --git a/src/commonImplementation/kotlin/CommonStringBuilderFunctions.kt b/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt similarity index 82% rename from src/commonImplementation/kotlin/CommonStringBuilderFunctions.kt rename to src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt index 6ae45b0..f58c831 100644 --- a/src/commonImplementation/kotlin/CommonStringBuilderFunctions.kt +++ b/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt @@ -1,4 +1,6 @@ -package de.cketti.codepoints +package de.cketti.codepoints.internal + +import de.cketti.codepoints.CodePoints object CommonStringBuilderFunctions { fun appendCodePoint(builder: StringBuilder, codePoint: Int) { diff --git a/src/commonImplementation/kotlin/CommonStringFunctions.kt b/src/commonImplementation/kotlin/internal/CommonStringFunctions.kt similarity index 96% rename from src/commonImplementation/kotlin/CommonStringFunctions.kt rename to src/commonImplementation/kotlin/internal/CommonStringFunctions.kt index b873057..5647a8a 100644 --- a/src/commonImplementation/kotlin/CommonStringFunctions.kt +++ b/src/commonImplementation/kotlin/internal/CommonStringFunctions.kt @@ -1,6 +1,6 @@ -package de.cketti.codepoints +package de.cketti.codepoints.internal -import de.cketti.codepoints.CommonCodePoints.toCodePoint +import de.cketti.codepoints.internal.CommonCodePoints.toCodePoint object CommonStringFunctions { fun codePointAt(text: String, index: Int): Int { From 6516c51905177015b84b8379850875a1a7f20692 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 24 Jan 2023 16:40:38 +0100 Subject: [PATCH 2/4] Remove useless objects in common implementation --- src/commonImplementation/kotlin/CodePoints.kt | 30 ++-- .../kotlin/StringBuilderExtensions.kt | 4 +- .../kotlin/StringExtensions.kt | 13 +- .../kotlin/internal/CommonCodePoints.kt | 124 ++++++++------- .../internal/CommonStringBuilderFunctions.kt | 14 +- .../kotlin/internal/CommonStringFunctions.kt | 142 +++++++++--------- 6 files changed, 165 insertions(+), 162 deletions(-) diff --git a/src/commonImplementation/kotlin/CodePoints.kt b/src/commonImplementation/kotlin/CodePoints.kt index f8b6587..e413c80 100644 --- a/src/commonImplementation/kotlin/CodePoints.kt +++ b/src/commonImplementation/kotlin/CodePoints.kt @@ -2,46 +2,54 @@ package de.cketti.codepoints -import de.cketti.codepoints.internal.CommonCodePoints +import de.cketti.codepoints.internal.charCount as commonCharCount +import de.cketti.codepoints.internal.highSurrogate as commonHighSurrogate +import de.cketti.codepoints.internal.isBmpCodePoint as commonIsBmpCodePoint +import de.cketti.codepoints.internal.isSupplementaryCodePoint as commonIsSupplementaryCodePoint +import de.cketti.codepoints.internal.isSurrogatePair as commonIsSurrogatePair +import de.cketti.codepoints.internal.isValidCodePoint as commonIsValidCodePoint +import de.cketti.codepoints.internal.lowSurrogate as commonLowSurrogate +import de.cketti.codepoints.internal.toChars as commonToChars +import de.cketti.codepoints.internal.toCodePoint as commonToCodePoint actual object CodePoints { actual inline fun isValidCodePoint(codePoint: Int): Boolean { - return CommonCodePoints.isValidCodePoint(codePoint) + return commonIsValidCodePoint(codePoint) } actual inline fun isBmpCodePoint(codePoint: Int): Boolean { - return CommonCodePoints.isBmpCodePoint(codePoint) + return commonIsBmpCodePoint(codePoint) } actual inline fun isSupplementaryCodePoint(codePoint: Int): Boolean { - return CommonCodePoints.isSupplementaryCodePoint(codePoint) + return commonIsSupplementaryCodePoint(codePoint) } actual inline fun charCount(codePoint: Int): Int { - return CommonCodePoints.charCount(codePoint) + return commonCharCount(codePoint) } actual inline fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean { - return CommonCodePoints.isSurrogatePair(highSurrogate, lowSurrogate) + 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) + return commonToCodePoint(highSurrogate, lowSurrogate) } actual inline fun toChars(codePoint: Int): CharArray { - return CommonCodePoints.toChars(codePoint) + return commonToChars(codePoint) } actual inline fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int { - return CommonCodePoints.toChars(codePoint, destination, offset) + return commonToChars(codePoint, destination, offset) } } diff --git a/src/commonImplementation/kotlin/StringBuilderExtensions.kt b/src/commonImplementation/kotlin/StringBuilderExtensions.kt index b628fdd..1d0ea95 100644 --- a/src/commonImplementation/kotlin/StringBuilderExtensions.kt +++ b/src/commonImplementation/kotlin/StringBuilderExtensions.kt @@ -1,8 +1,8 @@ package de.cketti.codepoints -import de.cketti.codepoints.internal.CommonStringBuilderFunctions 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) } diff --git a/src/commonImplementation/kotlin/StringExtensions.kt b/src/commonImplementation/kotlin/StringExtensions.kt index 41987dc..7ea4aa9 100644 --- a/src/commonImplementation/kotlin/StringExtensions.kt +++ b/src/commonImplementation/kotlin/StringExtensions.kt @@ -2,20 +2,23 @@ package de.cketti.codepoints -import de.cketti.codepoints.internal.CommonStringFunctions +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 inline fun String.codePointAt(index: Int): Int { - return CommonStringFunctions.codePointAt(this, index) + return commonCodePointAt(this, index) } actual inline fun String.codePointBefore(index: Int): Int { - return CommonStringFunctions.codePointBefore(this, index) + return commonCodePointBefore(this, index) } actual inline fun String.codePointCount(beginIndex: Int, endIndex: Int): Int { - return CommonStringFunctions.codePointCount(this, beginIndex, endIndex) + return commonCodePointCount(this, beginIndex, endIndex) } actual inline fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int { - return CommonStringFunctions.offsetByCodePoints(this, index, codePointOffset) + return commonOffsetByCodePoints(this, index, codePointOffset) } diff --git a/src/commonImplementation/kotlin/internal/CommonCodePoints.kt b/src/commonImplementation/kotlin/internal/CommonCodePoints.kt index dbf8e8c..8c760e5 100644 --- a/src/commonImplementation/kotlin/internal/CommonCodePoints.kt +++ b/src/commonImplementation/kotlin/internal/CommonCodePoints.kt @@ -1,77 +1,75 @@ package de.cketti.codepoints.internal -object CommonCodePoints { - private const val MIN_SUPPLEMENTARY_CODE_POINT = 0x10000 - private const val MAX_CODE_POINT = 0x10FFFF +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 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 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)) +private const val HIGH_SURROGATE_ENCODE_OFFSET = + (MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT ushr 10)) - fun isValidCodePoint(codePoint: Int): Boolean { - return codePoint in 0..MAX_CODE_POINT - } - - fun isBmpCodePoint(codePoint: Int): Boolean { - return codePoint ushr 16 == 0 - } - - fun isSupplementaryCodePoint(codePoint: Int): Boolean { - return codePoint in MIN_SUPPLEMENTARY_CODE_POINT..MAX_CODE_POINT - } - - fun charCount(codePoint: Int): Int { - return if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) 1 else 2 - } - - fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean { - return highSurrogate.isHighSurrogate() && lowSurrogate.isLowSurrogate() - } +fun isValidCodePoint(codePoint: Int): Boolean { + return codePoint in 0..MAX_CODE_POINT +} - fun highSurrogate(codePoint: Int): Char { - return ((codePoint ushr 10) + HIGH_SURROGATE_ENCODE_OFFSET).toChar() - } - - fun lowSurrogate(codePoint: Int): Char { - return ((codePoint and 0x3FF) + MIN_LOW_SURROGATE).toChar() - } - - fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int { - return (highSurrogate.code shl 10) + lowSurrogate.code + SURROGATE_DECODE_OFFSET - } +fun isBmpCodePoint(codePoint: Int): Boolean { + return codePoint ushr 16 == 0 +} - fun toChars(codePoint: Int): CharArray { - return if (isBmpCodePoint(codePoint)) { - charArrayOf(codePoint.toChar()) - } else { - charArrayOf(highSurrogate(codePoint), lowSurrogate(codePoint)) - } +fun isSupplementaryCodePoint(codePoint: Int): Boolean { + return codePoint in MIN_SUPPLEMENTARY_CODE_POINT..MAX_CODE_POINT +} + +fun charCount(codePoint: Int): Int { + return if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) 1 else 2 +} + +fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean { + return highSurrogate.isHighSurrogate() && lowSurrogate.isLowSurrogate() +} + +fun highSurrogate(codePoint: Int): Char { + return ((codePoint ushr 10) + HIGH_SURROGATE_ENCODE_OFFSET).toChar() +} + +fun lowSurrogate(codePoint: Int): Char { + return ((codePoint and 0x3FF) + MIN_LOW_SURROGATE).toChar() +} + +fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int { + return (highSurrogate.code shl 10) + lowSurrogate.code + SURROGATE_DECODE_OFFSET +} + +fun toChars(codePoint: Int): CharArray { + return if (isBmpCodePoint(codePoint)) { + charArrayOf(codePoint.toChar()) + } else { + charArrayOf(highSurrogate(codePoint), lowSurrogate(codePoint)) } +} - 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 - } +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 +private fun CharArray.setSafe(index: Int, value: Char) { + if (index !in this.indices) { + throw IndexOutOfBoundsException("Size: $size, offset: $index") } + + this[index] = value } diff --git a/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt b/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt index f58c831..4826cc2 100644 --- a/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt +++ b/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt @@ -2,13 +2,11 @@ package de.cketti.codepoints.internal import de.cketti.codepoints.CodePoints -object CommonStringBuilderFunctions { - fun appendCodePoint(builder: StringBuilder, codePoint: Int) { - if (CodePoints.isBmpCodePoint(codePoint)) { - builder.append(codePoint.toChar()) - } else { - builder.append(CodePoints.highSurrogate(codePoint)) - builder.append(CodePoints.lowSurrogate(codePoint)) - } +fun appendCodePoint(builder: StringBuilder, codePoint: Int) { + if (CodePoints.isBmpCodePoint(codePoint)) { + builder.append(codePoint.toChar()) + } else { + builder.append(CodePoints.highSurrogate(codePoint)) + builder.append(CodePoints.lowSurrogate(codePoint)) } } diff --git a/src/commonImplementation/kotlin/internal/CommonStringFunctions.kt b/src/commonImplementation/kotlin/internal/CommonStringFunctions.kt index 5647a8a..ddf6d82 100644 --- a/src/commonImplementation/kotlin/internal/CommonStringFunctions.kt +++ b/src/commonImplementation/kotlin/internal/CommonStringFunctions.kt @@ -1,92 +1,88 @@ package de.cketti.codepoints.internal -import de.cketti.codepoints.internal.CommonCodePoints.toCodePoint +fun codePointAt(text: String, index: Int): Int { + if (index !in text.indices) throw IndexOutOfBoundsException() -object CommonStringFunctions { - fun codePointAt(text: String, index: Int): Int { - if (index !in text.indices) throw IndexOutOfBoundsException() - - val firstChar = text[index] - if (firstChar.isHighSurrogate() && index + 1 < text.length) { - val nextChar = text[index + 1] - if (nextChar.isLowSurrogate()) { - return toCodePoint(firstChar, nextChar) - } + val firstChar = text[index] + if (firstChar.isHighSurrogate() && index + 1 < text.length) { + val nextChar = text[index + 1] + if (nextChar.isLowSurrogate()) { + return toCodePoint(firstChar, nextChar) } - - return firstChar.code } - fun codePointBefore(text: String, index: Int): Int { - val startIndex = index - 1 - if (startIndex !in text.indices) throw IndexOutOfBoundsException() - - val firstChar = text[startIndex] - if (firstChar.isLowSurrogate() && startIndex - 1 >= 0) { - val previousChar = text[startIndex - 1] - if (previousChar.isHighSurrogate()) { - return toCodePoint(previousChar, firstChar) - } + return firstChar.code +} + +fun codePointBefore(text: String, index: Int): Int { + val startIndex = index - 1 + if (startIndex !in text.indices) throw IndexOutOfBoundsException() + + val firstChar = text[startIndex] + if (firstChar.isLowSurrogate() && startIndex - 1 >= 0) { + val previousChar = text[startIndex - 1] + if (previousChar.isHighSurrogate()) { + return toCodePoint(previousChar, firstChar) } - - return firstChar.code } + + return firstChar.code +} + +fun codePointCount(text: String, beginIndex: Int, endIndex: Int): Int { + if (beginIndex < 0 || endIndex > text.length || beginIndex > endIndex) throw IndexOutOfBoundsException() - fun codePointCount(text: String, beginIndex: Int, endIndex: Int): Int { - if (beginIndex < 0 || endIndex > text.length || beginIndex > endIndex) throw IndexOutOfBoundsException() - - var index = beginIndex - var count = 0 - do { - val firstChar = text[index] - index++ - if (firstChar.isHighSurrogate() && index < endIndex) { - val nextChar = text[index] - if (nextChar.isLowSurrogate()) { - index++ - } + var index = beginIndex + var count = 0 + do { + val firstChar = text[index] + index++ + if (firstChar.isHighSurrogate() && index < endIndex) { + val nextChar = text[index] + if (nextChar.isLowSurrogate()) { + index++ } - - count++ - } while (index < endIndex) - - return count - } - - fun offsetByCodePoints(text: String, index: Int, codePointOffset: Int): Int { - if (index !in 0..text.length) throw IndexOutOfBoundsException() - if (codePointOffset == 0) return index + } + + count++ + } while (index < endIndex) - if (codePointOffset > 0) { - var currentIndex = index - repeat(codePointOffset) { - if (currentIndex > text.lastIndex) throw IndexOutOfBoundsException() - val firstChar = text[currentIndex] - currentIndex++ - if (firstChar.isHighSurrogate() && currentIndex <= text.lastIndex) { - val nextChar = text[currentIndex] - if (nextChar.isLowSurrogate()) { - currentIndex++ - } + return count +} + +fun offsetByCodePoints(text: String, index: Int, codePointOffset: Int): Int { + if (index !in 0..text.length) throw IndexOutOfBoundsException() + if (codePointOffset == 0) return index + + if (codePointOffset > 0) { + var currentIndex = index + repeat(codePointOffset) { + if (currentIndex > text.lastIndex) throw IndexOutOfBoundsException() + val firstChar = text[currentIndex] + currentIndex++ + if (firstChar.isHighSurrogate() && currentIndex <= text.lastIndex) { + val nextChar = text[currentIndex] + if (nextChar.isLowSurrogate()) { + currentIndex++ } } + } - return currentIndex - } else { - var currentIndex = index - 1 - repeat(-codePointOffset) { - if (currentIndex < 0) throw IndexOutOfBoundsException() - val firstChar = text[currentIndex] - currentIndex-- - if (firstChar.isLowSurrogate() && currentIndex >= 0) { - val previousChar = text[currentIndex] - if (previousChar.isHighSurrogate()) { - currentIndex-- - } + return currentIndex + } else { + var currentIndex = index - 1 + repeat(-codePointOffset) { + if (currentIndex < 0) throw IndexOutOfBoundsException() + val firstChar = text[currentIndex] + currentIndex-- + if (firstChar.isLowSurrogate() && currentIndex >= 0) { + val previousChar = text[currentIndex] + if (previousChar.isHighSurrogate()) { + currentIndex-- } } - - return currentIndex + 1 } + + return currentIndex + 1 } } From 8eeb581131e4f075fc65cf412fc01550a00b75f2 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 24 Jan 2023 16:48:42 +0100 Subject: [PATCH 3/4] Use `internal` keyword for functions in the `internal` package --- src/commonImplementation/kotlin/CodePoints.kt | 16 +++++++-------- .../kotlin/StringExtensions.kt | 8 ++++---- .../kotlin/internal/CommonCodePoints.kt | 20 +++++++++---------- .../internal/CommonStringBuilderFunctions.kt | 2 +- .../kotlin/internal/CommonStringFunctions.kt | 8 ++++---- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/commonImplementation/kotlin/CodePoints.kt b/src/commonImplementation/kotlin/CodePoints.kt index e413c80..f6d8e4d 100644 --- a/src/commonImplementation/kotlin/CodePoints.kt +++ b/src/commonImplementation/kotlin/CodePoints.kt @@ -13,23 +13,23 @@ import de.cketti.codepoints.internal.toChars as commonToChars import de.cketti.codepoints.internal.toCodePoint as commonToCodePoint actual object CodePoints { - actual inline fun isValidCodePoint(codePoint: Int): Boolean { + actual fun isValidCodePoint(codePoint: Int): Boolean { return commonIsValidCodePoint(codePoint) } - actual inline fun isBmpCodePoint(codePoint: Int): Boolean { + actual fun isBmpCodePoint(codePoint: Int): Boolean { return commonIsBmpCodePoint(codePoint) } - actual inline fun isSupplementaryCodePoint(codePoint: Int): Boolean { + actual fun isSupplementaryCodePoint(codePoint: Int): Boolean { return commonIsSupplementaryCodePoint(codePoint) } - actual inline fun charCount(codePoint: Int): Int { + actual fun charCount(codePoint: Int): Int { return commonCharCount(codePoint) } - actual inline fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean { + actual fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean { return commonIsSurrogatePair(highSurrogate, lowSurrogate) } @@ -41,15 +41,15 @@ actual object CodePoints { return commonLowSurrogate(codePoint) } - actual inline fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int { + actual fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int { return commonToCodePoint(highSurrogate, lowSurrogate) } - actual inline fun toChars(codePoint: Int): CharArray { + actual fun toChars(codePoint: Int): CharArray { return commonToChars(codePoint) } - actual inline fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int { + actual fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int { return commonToChars(codePoint, destination, offset) } } diff --git a/src/commonImplementation/kotlin/StringExtensions.kt b/src/commonImplementation/kotlin/StringExtensions.kt index 7ea4aa9..b14af41 100644 --- a/src/commonImplementation/kotlin/StringExtensions.kt +++ b/src/commonImplementation/kotlin/StringExtensions.kt @@ -7,18 +7,18 @@ 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 inline fun String.codePointAt(index: Int): Int { +actual fun String.codePointAt(index: Int): Int { return commonCodePointAt(this, index) } -actual inline fun String.codePointBefore(index: Int): Int { +actual fun String.codePointBefore(index: Int): Int { return commonCodePointBefore(this, index) } -actual inline fun String.codePointCount(beginIndex: Int, endIndex: Int): Int { +actual fun String.codePointCount(beginIndex: Int, endIndex: Int): Int { return commonCodePointCount(this, beginIndex, endIndex) } -actual inline fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int { +actual fun String.offsetByCodePoints(index: Int, codePointOffset: Int): Int { return commonOffsetByCodePoints(this, index, codePointOffset) } diff --git a/src/commonImplementation/kotlin/internal/CommonCodePoints.kt b/src/commonImplementation/kotlin/internal/CommonCodePoints.kt index 8c760e5..a0b8fd6 100644 --- a/src/commonImplementation/kotlin/internal/CommonCodePoints.kt +++ b/src/commonImplementation/kotlin/internal/CommonCodePoints.kt @@ -12,39 +12,39 @@ private const val SURROGATE_DECODE_OFFSET = private const val HIGH_SURROGATE_ENCODE_OFFSET = (MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT ushr 10)) -fun isValidCodePoint(codePoint: Int): Boolean { +internal fun isValidCodePoint(codePoint: Int): Boolean { return codePoint in 0..MAX_CODE_POINT } -fun isBmpCodePoint(codePoint: Int): Boolean { +internal fun isBmpCodePoint(codePoint: Int): Boolean { return codePoint ushr 16 == 0 } -fun isSupplementaryCodePoint(codePoint: Int): Boolean { +internal fun isSupplementaryCodePoint(codePoint: Int): Boolean { return codePoint in MIN_SUPPLEMENTARY_CODE_POINT..MAX_CODE_POINT } -fun charCount(codePoint: Int): Int { +internal fun charCount(codePoint: Int): Int { return if (codePoint < MIN_SUPPLEMENTARY_CODE_POINT) 1 else 2 } -fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean { +internal fun isSurrogatePair(highSurrogate: Char, lowSurrogate: Char): Boolean { return highSurrogate.isHighSurrogate() && lowSurrogate.isLowSurrogate() } -fun highSurrogate(codePoint: Int): Char { +internal fun highSurrogate(codePoint: Int): Char { return ((codePoint ushr 10) + HIGH_SURROGATE_ENCODE_OFFSET).toChar() } -fun lowSurrogate(codePoint: Int): Char { +internal fun lowSurrogate(codePoint: Int): Char { return ((codePoint and 0x3FF) + MIN_LOW_SURROGATE).toChar() } -fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int { +internal fun toCodePoint(highSurrogate: Char, lowSurrogate: Char): Int { return (highSurrogate.code shl 10) + lowSurrogate.code + SURROGATE_DECODE_OFFSET } -fun toChars(codePoint: Int): CharArray { +internal fun toChars(codePoint: Int): CharArray { return if (isBmpCodePoint(codePoint)) { charArrayOf(codePoint.toChar()) } else { @@ -52,7 +52,7 @@ fun toChars(codePoint: Int): CharArray { } } -fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int { +internal fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int { if (isBmpCodePoint(codePoint)) { destination.setSafe(offset, codePoint.toChar()) return 1 diff --git a/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt b/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt index 4826cc2..dc90cc6 100644 --- a/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt +++ b/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt @@ -2,7 +2,7 @@ package de.cketti.codepoints.internal import de.cketti.codepoints.CodePoints -fun appendCodePoint(builder: StringBuilder, codePoint: Int) { +internal fun appendCodePoint(builder: StringBuilder, codePoint: Int) { if (CodePoints.isBmpCodePoint(codePoint)) { builder.append(codePoint.toChar()) } else { diff --git a/src/commonImplementation/kotlin/internal/CommonStringFunctions.kt b/src/commonImplementation/kotlin/internal/CommonStringFunctions.kt index ddf6d82..127569e 100644 --- a/src/commonImplementation/kotlin/internal/CommonStringFunctions.kt +++ b/src/commonImplementation/kotlin/internal/CommonStringFunctions.kt @@ -1,6 +1,6 @@ package de.cketti.codepoints.internal -fun codePointAt(text: String, index: Int): Int { +internal fun codePointAt(text: String, index: Int): Int { if (index !in text.indices) throw IndexOutOfBoundsException() val firstChar = text[index] @@ -14,7 +14,7 @@ fun codePointAt(text: String, index: Int): Int { return firstChar.code } -fun codePointBefore(text: String, index: Int): Int { +internal fun codePointBefore(text: String, index: Int): Int { val startIndex = index - 1 if (startIndex !in text.indices) throw IndexOutOfBoundsException() @@ -29,7 +29,7 @@ fun codePointBefore(text: String, index: Int): Int { return firstChar.code } -fun codePointCount(text: String, beginIndex: Int, endIndex: Int): Int { +internal fun codePointCount(text: String, beginIndex: Int, endIndex: Int): Int { if (beginIndex < 0 || endIndex > text.length || beginIndex > endIndex) throw IndexOutOfBoundsException() var index = beginIndex @@ -50,7 +50,7 @@ fun codePointCount(text: String, beginIndex: Int, endIndex: Int): Int { return count } -fun offsetByCodePoints(text: String, index: Int, codePointOffset: Int): Int { +internal fun offsetByCodePoints(text: String, index: Int, codePointOffset: Int): Int { if (index !in 0..text.length) throw IndexOutOfBoundsException() if (codePointOffset == 0) return index From d38e2b1d84e23fa19a515420354f7fbf7e71053c Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 24 Jan 2023 16:58:50 +0100 Subject: [PATCH 4/4] Directly use common implementation in `CommonStringBuilderFunctions.kt` --- .../kotlin/internal/CommonStringBuilderFunctions.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt b/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt index dc90cc6..cc5955a 100644 --- a/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt +++ b/src/commonImplementation/kotlin/internal/CommonStringBuilderFunctions.kt @@ -1,12 +1,10 @@ package de.cketti.codepoints.internal -import de.cketti.codepoints.CodePoints - internal fun appendCodePoint(builder: StringBuilder, codePoint: Int) { - if (CodePoints.isBmpCodePoint(codePoint)) { + if (isBmpCodePoint(codePoint)) { builder.append(codePoint.toChar()) } else { - builder.append(CodePoints.highSurrogate(codePoint)) - builder.append(CodePoints.lowSurrogate(codePoint)) + builder.append(highSurrogate(codePoint)) + builder.append(lowSurrogate(codePoint)) } }