-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
String.intCodePoints()
native implementations.
- Loading branch information
Showing
8 changed files
with
60 additions
and
14 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
skiko/src/commonMain/kotlin/org/jetbrains/skia/CodePoint.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,51 @@ | ||
package org.jetbrains.skia | ||
|
||
// Copied from CharHelpers.skiko.kt | ||
// TODO Remove once it's available in common stdlib https://youtrack.jetbrains.com/issue/KT-23251 | ||
internal typealias CodePoint = Int | ||
|
||
// Copied from CharHelpers.skiko.kt | ||
/** | ||
* The minimum value of a supplementary code point, `\u0x10000`. | ||
*/ | ||
private const val MIN_SUPPLEMENTARY_CODE_POINT: Int = 0x10000 | ||
|
||
// Copied from CharHelpers.skiko.kt | ||
/** | ||
* Converts a surrogate pair to a unicode code point. | ||
*/ | ||
internal fun toCodePoint(high: Char, low: Char): CodePoint = | ||
(((high - Char.MIN_HIGH_SURROGATE) shl 10) or (low - Char.MIN_LOW_SURROGATE)) + MIN_SUPPLEMENTARY_CODE_POINT | ||
|
||
// Copied from CharHelpers.skiko.kt | ||
internal fun Int.charCount(): Int = if (this >= MIN_SUPPLEMENTARY_CODE_POINT) 2 else 1 | ||
|
||
internal val String.codePointsAsIntArray: IntArray | ||
get() = codePoints.toList().toIntArray() | ||
|
||
internal val String.codePoints | ||
get() = codePointsAt(0) | ||
|
||
internal fun String.codePointsAt(index: Int) = sequence { | ||
var current = index | ||
while (current < length) { | ||
val codePoint = codePointAt(current) | ||
yield(codePoint) | ||
current += codePoint.charCount() | ||
} | ||
} | ||
|
||
// Copied from CharHelpers.skiko.kt | ||
/** | ||
* Returns the character (Unicode code point) at the specified index. | ||
*/ | ||
internal fun CharSequence.codePointAt(index: Int): CodePoint { | ||
val high = this[index] | ||
if (high.isHighSurrogate() && index + 1 < this.length) { | ||
val low = this[index + 1] | ||
if (low.isLowSurrogate()) { | ||
return toCodePoint(high, low) | ||
} | ||
} | ||
return high.code | ||
} |
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 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 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 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 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 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 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