scoop:scoop.charset:0.1.0
src/unicode.scoop
Selected production source · src/unicode.scoop
1
package scoop.charset3
/** Returns extended-grapheme boundaries as scalar indices, using the fixed terminal Unicode data. */4
public fun graphemeBoundaries(text: String): Array<Long> = unicodeBoundaries(text, false)6
/** Returns locale-independent dictionary word boundaries as scalar indices, including whitespace and punctuation segments. */7
public fun wordBoundaries(text: String): Array<Long> = unicodeBoundaries(text, true)9
private 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
input14
.withDataPointer { data, size ->15
@Unsafe {16
breaks17
.withDataPointer { output, _ ->18
@Unsafe {19
checkCharset(20
if (words)21
nativeWordBreaks(data, size, output)22
else23
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 = 0L34
var offset = 0L35
for (character in text) {36
if (breaks[offset] != 0) result.add(scalar)37
scalar += 1L38
offset += character.toString().byteLength39
}40
result.add(scalar)41
return result.toArray()42
}44
/** Splits text into independent extended-grapheme strings. */45
public 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
}50
}52
/** Returns terminal scalar width, or -1 for a control including NUL. Ambiguous width is explicit. */53
public fun unicodeScalarWidth(character: Char, ambiguousAsWide: Boolean = false): Int {54
@Unsafe {55
return nativeScalarWidth(character.code, ambiguousAsWide)56
}57
}59
/** Returns contextual terminal width of complete text; controls including NUL return -1, empty text returns 0. */60
public fun unicodeTextWidth(text: String, ambiguousAsWide: Boolean = false): Long {61
@Unsafe {62
return text63
.withUtf8Bytes { data, size ->64
@Unsafe {65
return@withUtf8Bytes nativeTextWidth(data.cast<Byte>(), size, ambiguousAsWide)66
}67
}68
}69
}71
/** Unicode data version shared by grapheme/word segmentation and terminal widths, independent of the host OS. */72
public fun unicodeTerminalDataVersion(): String {73
@Unsafe {74
val packed = nativeTerminalUnicodeVersion()75
return f"${packed shr 16L}.${(packed shr 8L) and 255}.${packed and 255}"76
}77
}79
/** Tests Unicode Alphabetic or Decimal_Number without using the process locale. */80
public fun isUnicodeAlphanumeric(character: Char): Boolean {81
@Unsafe {82
return nativeAlphanumeric(character.code)83
}84
}86
/** Applies root-locale lowercase with full string context and multi-scalar mappings. */87
public fun unicodeLowercase(text: String): String = convertCase(text, 0)89
/** Applies root-locale uppercase with full string context and multi-scalar mappings. */90
public fun unicodeUppercase(text: String): String = convertCase(text, 1)92
/** Applies locale-independent Unicode full case folding for caseless matching. */93
public fun unicodeCasefold(text: String): String = convertCase(text, 2)95
private fun convertCase(text: String, mode: Int): String {96
@Unsafe {97
val input = text.toByteArray()98
val result = input99
.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
}116
}118
@Extern(name = "scoop_charset_grapheme_breaks")119
private fun nativeGraphemeBreaks(data: Ptr<Byte>, size: Long, breaks: Ptr<Byte>): Int121
@Extern(name = "scoop_charset_word_breaks")122
private fun nativeWordBreaks(data: Ptr<Byte>, size: Long, breaks: Ptr<Byte>): Int124
@Extern(name = "scoop_charset_scalar_width")125
private fun nativeScalarWidth(scalar: Int, ambiguousAsWide: Boolean): Int127
@Extern(name = "scoop_charset_text_width")128
private fun nativeTextWidth(data: Ptr<Byte>, size: Long, ambiguousAsWide: Boolean): Long130
@Extern(name = "scoop_charset_terminal_unicode_version")131
private fun nativeTerminalUnicodeVersion(): Int133
@Extern(name = "scoop_charset_alphanumeric")134
private fun nativeAlphanumeric(scalar: Int): Boolean136
@Extern(name = "scoop_charset_case")137
private fun nativeCase(data: Ptr<Byte>, size: Long, mode: Int): NativeCharsetText