scoop:scoop.idna:0.1.0
src/idna.scoop
Selected production source · src/idna.scoop
1
package scoop.idna3
/**4
* The validated ASCII and Unicode forms of one internationalized domain name, produced by5
* [Idna.parse].6
*/7
public class IdnaName internal constructor(8
/**9
* The validated ASCII form suitable for protocol host names.10
*/11
public val ascii: String,12
/**13
* The validated Unicode form suitable for presentation.14
*/15
public val unicode: String)17
/**18
* UTS #46 nontransitional domain-name conversion with STD3, hyphen, ContextJ,19
* bidirectional, and DNS length checks. Symbols and ContextO follow UTS #46;20
* strict IDNA2008 exclusions are not added. Label operations accept one label.21
*/22
public object Idna {23
/**24
* The version reported by the linked IDNA backend.25
*/26
public val backendVersion: String get() { @Unsafe { return String.fromCString(idnaVersion()) } }27
/**28
* The Unicode data version used by the supported IDNA profile.29
*/30
public val unicodeVersion: String get() { @Unsafe { return String.fromCString(idnaUnicodeVersion()) } }32
/**33
* Validates a domain name and returns both its ASCII and Unicode representations.34
*35
* @throws IdnaException The input fails IDNA validation or the backend operation fails.36
*/37
public fun parse(domain: String): IdnaName = parseIdna(domain)39
/**40
* Validates and converts a complete domain name to its ASCII representation. Invalid input41
* throws [IdnaException].42
*/43
public fun toAscii(domain: String): String = convertIdna(domain, 0, "IDNA to ASCII")44
/**45
* Validates and converts a complete domain name to its Unicode representation. Invalid input46
* throws [IdnaException].47
*/48
public fun toUnicode(domain: String): String = convertIdna(domain, 1, "IDNA to Unicode")49
/**50
* Validates and converts one label to ASCII; input containing a domain separator is rejected.51
*/52
public fun labelToAscii(label: String): String = convertIdna(label, 2, "IDNA label to ASCII")53
/**54
* Validates and converts one label to Unicode; input containing a domain separator is55
* rejected.56
*/57
public fun labelToUnicode(label: String): String = convertIdna(label, 3, "IDNA label to Unicode")58
/**59
* Returns the ASCII name, or None for an IDNA input-validation failure. Backend failures still60
* throw [IdnaException].61
*/62
public fun toAsciiOrNone(domain: String): String? = tryIdna(domain, 0, "IDNA to ASCII")63
/**64
* Returns the Unicode name, or None for an IDNA input-validation failure. Backend failures65
* still throw [IdnaException].66
*/67
public fun toUnicodeOrNone(domain: String): String? = tryIdna(domain, 1, "IDNA to Unicode")68
}70
/**71
* Raw Punycode conversion without domain-name validation. Use [Idna] when processing a host name.72
*/73
public object Punycode {74
/**75
* Encodes Unicode text as a Punycode payload without adding the `xn--` label prefix.76
*/77
public fun encode(text: String): String = convertPunycode(text, false, "Punycode encode")78
/**79
* Decodes a Punycode payload without a label prefix. Invalid payloads throw [IdnaException].80
*/81
public fun decode(text: String): String = convertPunycode(text, true, "Punycode decode")82
}84
private fun tryIdna(text: String, mode: Int, operation: String): String? {85
try { return Some(convertIdna(text, mode, operation)) }86
catch (error: IdnaException) {87
if (error.kind == IdnaFailureKind.Backend) throw error88
return None89
}90
}