Scoop API

scoop:scoop.charset:0.1.0

src/normalize.scoop

Selected production source · src/normalize.scoop

1package scoop.charset
3/**
4 * Returns Unicode text in canonical composition form NFC.
5 */
6public fun normalizeNfc(text: String): String {
7 @Unsafe {
8 val input = text.toByteArray()
9 val result = input.withDataPointer { data, size ->
10 @Unsafe { return@withDataPointer charsetNormalizeNfc(data, size) }
11 }
12 try {
13 if (result.code != 0) {
14 throw CharsetException("normalize NFC", "UTF-8", "UTF-8", CharsetFailureKind.Backend, None,
15 if (result.backend == 0) None else Some(result.backend),
16 "text backend could not normalize NFC")
17 }
18 val output = Array<Byte>(result.size) { index -> @Unsafe { return@Array result.data!!.load(index) } }
19 return String.fromUtf8(output)
20 } finally { charsetUnicodeFree(result.data, result.size) }
21 }
24@Extern(name = "scoop_charset_normalize_nfc")
25private fun charsetNormalizeNfc(data: Ptr<Byte>, size: Long): NativeCharsetText
27@Extern(name = "scoop_unicode_free")
28internal fun charsetUnicodeFree(data: Ptr<Byte>?, size: Long)