Scoop API

scoop:scoop.charset:0.1.0

src/unicode.scoop

Selected production source · src/unicode.scoop

1package scoop.charset
3/** Returns extended-grapheme boundaries as scalar indices, using the fixed terminal Unicode data. */
4public fun graphemeBoundaries(text: String): Array<Long> = unicodeBoundaries(text, false)
6/** Returns locale-independent dictionary word boundaries as scalar indices, including whitespace and punctuation segments. */
7public fun wordBoundaries(text: String): Array<Long> = unicodeBoundaries(text, true)
9private fun unicodeBoundaries(text: String, words: Boolean): Array<Long> {
10 val input = text.toByteArray()
11 val breaks = MutableArray<Byte>(input.size) { 0.toByte() }
12 @Unsafe {
13 input
14 .withDataPointer { data, size ->
15 @Unsafe {
16 breaks
17 .withDataPointer { output, _ ->
18 @Unsafe {
19 checkCharset(
20 if (words)
21 nativeWordBreaks(data, size, output)
22 else
23 nativeGraphemeBreaks(data, size, output), 0,
24 if (words) "segment words" else "segment graphemes", "UTF-8",
25 "UTF-8"
26 )
27 }
28 }
29 }
30 }
31 }
32 val result = ArrayList<Long>()
33 var scalar = 0L
34 var offset = 0L
35 for (character in text) {
36 if (breaks[offset] != 0) result.add(scalar)
37 scalar += 1L
38 offset += character.toString().byteLength
39 }
40 result.add(scalar)
41 return result.toArray()
44/** Splits text into independent extended-grapheme strings. */
45public fun graphemes(text: String): Array<String> {
46 val boundaries = graphemeBoundaries(text)
47 return Array<String>(boundaries.size - 1L) { index ->
48 text.slice(boundaries[index], boundaries[index + 1L])
49 }
52/** Returns terminal scalar width, or -1 for a control including NUL. Ambiguous width is explicit. */
53public fun unicodeScalarWidth(character: Char, ambiguousAsWide: Boolean = false): Int {
54 @Unsafe {
55 return nativeScalarWidth(character.code, ambiguousAsWide)
56 }
59/** Returns contextual terminal width of complete text; controls including NUL return -1, empty text returns 0. */
60public fun unicodeTextWidth(text: String, ambiguousAsWide: Boolean = false): Long {
61 @Unsafe {
62 return text
63 .withUtf8Bytes { data, size ->
64 @Unsafe {
65 return@withUtf8Bytes nativeTextWidth(data.cast<Byte>(), size, ambiguousAsWide)
66 }
67 }
68 }
71/** Unicode data version shared by grapheme/word segmentation and terminal widths, independent of the host OS. */
72public fun unicodeTerminalDataVersion(): String {
73 @Unsafe {
74 val packed = nativeTerminalUnicodeVersion()
75 return f"${packed shr 16L}.${(packed shr 8L) and 255}.${packed and 255}"
76 }
79/** Tests Unicode Alphabetic or Decimal_Number without using the process locale. */
80public fun isUnicodeAlphanumeric(character: Char): Boolean {
81 @Unsafe {
82 return nativeAlphanumeric(character.code)
83 }
86/** Applies root-locale lowercase with full string context and multi-scalar mappings. */
87public fun unicodeLowercase(text: String): String = convertCase(text, 0)
89/** Applies root-locale uppercase with full string context and multi-scalar mappings. */
90public fun unicodeUppercase(text: String): String = convertCase(text, 1)
92/** Applies locale-independent Unicode full case folding for caseless matching. */
93public fun unicodeCasefold(text: String): String = convertCase(text, 2)
95private fun convertCase(text: String, mode: Int): String {
96 @Unsafe {
97 val input = text.toByteArray()
98 val result = input
99 .withDataPointer { data, size ->
100 @Unsafe {
101 return@withDataPointer nativeCase(data, size, mode)
102 }
103 }
104 try {
105 checkCharset(result.code, result.backend, "convert Unicode case", "UTF-8", "UTF-8")
106 val output = Array<Byte>(result.size) { index ->
107 @Unsafe {
108 return@Array result.data!!.load(index)
109 }
110 }
111 return String.fromUtf8(output)
112 } finally {
113 charsetUnicodeFree(result.data, result.size)
114 }
115 }
118@Extern(name = "scoop_charset_grapheme_breaks")
119private fun nativeGraphemeBreaks(data: Ptr<Byte>, size: Long, breaks: Ptr<Byte>): Int
121@Extern(name = "scoop_charset_word_breaks")
122private fun nativeWordBreaks(data: Ptr<Byte>, size: Long, breaks: Ptr<Byte>): Int
124@Extern(name = "scoop_charset_scalar_width")
125private fun nativeScalarWidth(scalar: Int, ambiguousAsWide: Boolean): Int
127@Extern(name = "scoop_charset_text_width")
128private fun nativeTextWidth(data: Ptr<Byte>, size: Long, ambiguousAsWide: Boolean): Long
130@Extern(name = "scoop_charset_terminal_unicode_version")
131private fun nativeTerminalUnicodeVersion(): Int
133@Extern(name = "scoop_charset_alphanumeric")
134private fun nativeAlphanumeric(scalar: Int): Boolean
136@Extern(name = "scoop_charset_case")
137private fun nativeCase(data: Ptr<Byte>, size: Long, mode: Int): NativeCharsetText