Scoop API

scoop:scoop.charset:0.1.0

src/charset.scoop

Selected production source · src/charset.scoop

1package scoop.charset
3import scoop.text.*
5/**
6 * A supported character encoding identified by its canonical name. Equality and hashing use that
7 * name. Whole-value conversions are convenient for bounded input; [CharsetTranscoder] preserves
8 * conversion state across buffers.
9 */
10public class Charset private constructor(
11 /**
12 * The canonical encoding name after resolving accepted aliases.
13 */
14 public val name: String) : Equals<Charset>, Hash, ToString {
15 /**
16 * Compares canonical encoding names, so accepted aliases identify the same charset.
17 */
18 public override operator fun equals(other: Charset): Boolean = name == other.name
19 /**
20 * Hashes the canonical encoding name consistently with charset equality.
21 */
22 public override fun hash(): Long = name.hash()
23 /**
24 * Returns the canonical encoding name.
25 */
26 public override fun toString(): String = name
28 /**
29 * Decodes bytes using the selected BOM and byte-order policy. Malformed input throws
30 * [CharsetException].
31 */
32 public fun decode(data: List<Byte>, options: CharsetDecodeOptions = CharsetDecodeOptions()): String =
33 String.fromUtf8(transcodeCharset(data, this, Charset.Utf8, CharsetTranscodeOptions(decode = options)))
35 /**
36 * Decodes bytes while replacing malformed input; BOM and byte-order options still apply.
37 */
38 public fun decodeLossy(data: List<Byte>, options: CharsetDecodeOptions = CharsetDecodeOptions()): String =
39 String.fromUtf8(transcodeCharset(data, this, Charset.Utf8,
40 CharsetTranscodeOptions(decode = options, malformedInput = CharsetErrorAction.Replace)))
42 /**
43 * Encodes Unicode text. Characters that cannot be represented in this encoding throw
44 * [CharsetException].
45 */
46 public fun encode(text: String, options: CharsetEncodeOptions = CharsetEncodeOptions()): Array<Byte> =
47 encodeText(text, options, CharsetErrorAction.Strict)
49 /**
50 * Encodes Unicode text while replacing characters that cannot be represented in this encoding.
51 */
52 public fun encodeLossy(text: String, options: CharsetEncodeOptions = CharsetEncodeOptions()): Array<Byte> =
53 encodeText(text, options, CharsetErrorAction.Replace)
55 private fun encodeText(text: String, options: CharsetEncodeOptions, action: CharsetErrorAction): Array<Byte> =
56 transcodeCharset(text.toByteArray(), Charset.Utf8, this, CharsetTranscodeOptions(
57 decode = CharsetDecodeOptions(bom = CharsetBomPolicy.Preserve), encode = options, unmappableOutput = action))
59 /**
60 * Canonical named encodings, alias lookup, and backend information.
61 */
62 public companion object {
63 /**
64 * Resolves a supported encoding name or alias to its canonical charset. Unsupported names
65 * throw [UnsupportedCharsetException].
66 */
67 public fun forName(name: String): Charset = Charset(canonicalCharset(name))
68 /**
69 * Resolves a supported name or alias, returning None for an unsupported encoding.
70 */
71 public fun forNameOrNone(name: String): Charset? {
72 try { return Some(forName(name)) }
73 catch (_: UnsupportedCharsetException) { return None }
74 }
75 /**
76 * Unicode UTF-8 encoding.
77 */
78 public val Utf8: Charset = Charset(canonicalCharset("UTF-8"))
79 /**
80 * The seven-bit US-ASCII encoding.
81 */
82 public val Ascii: Charset = Charset(canonicalCharset("US-ASCII"))
83 /**
84 * ISO-8859-1 Latin-1 encoding.
85 */
86 public val Iso8859_1: Charset = Charset(canonicalCharset("ISO-8859-1"))
87 /**
88 * Windows-1252 Western European encoding.
89 */
90 public val Windows1252: Charset = Charset(canonicalCharset("Windows-1252"))
91 /**
92 * UTF-16 encoding whose byte order is selected by the conversion options and BOM.
93 */
94 public val Utf16: Charset = Charset(canonicalCharset("UTF-16"))
95 /**
96 * UTF-16 with little-endian code units.
97 */
98 public val Utf16LE: Charset = Charset(canonicalCharset("UTF-16LE"))
99 /**
100 * UTF-16 with big-endian code units.
101 */
102 public val Utf16BE: Charset = Charset(canonicalCharset("UTF-16BE"))
103 /**
104 * UTF-32 encoding whose byte order is selected by the conversion options and BOM.
105 */
106 public val Utf32: Charset = Charset(canonicalCharset("UTF-32"))
107 /**
108 * UTF-32 with little-endian code units.
109 */
110 public val Utf32LE: Charset = Charset(canonicalCharset("UTF-32LE"))
111 /**
112 * UTF-32 with big-endian code units.
113 */
114 public val Utf32BE: Charset = Charset(canonicalCharset("UTF-32BE"))
115 /**
116 * The GB18030 Chinese encoding.
117 */
118 public val Gb18030: Charset = Charset(canonicalCharset("GB18030"))
119 /**
120 * The GBK Chinese encoding.
121 */
122 public val Gbk: Charset = Charset(canonicalCharset("GBK"))
123 /**
124 * The Big5 Traditional Chinese encoding.
125 */
126 public val Big5: Charset = Charset(canonicalCharset("Big5"))
127 /**
128 * The Shift_JIS Japanese encoding.
129 */
130 public val ShiftJis: Charset = Charset(canonicalCharset("Shift_JIS"))
131 /**
132 * The EUC-JP Japanese encoding.
133 */
134 public val EucJp: Charset = Charset(canonicalCharset("EUC-JP"))
135 /**
136 * The stateful ISO-2022-JP Japanese encoding.
137 */
138 public val Iso2022Jp: Charset = Charset(canonicalCharset("ISO-2022-JP"))
140 /**
141 * Returns the encoding names advertised by the linked conversion backend.
142 */
143 public val availableNames: Array<String> get() {
144 val names = ArrayList<String>()
145 @Unsafe {
146 for (name in charsetText(charsetNames(), "list charsets").split('\n')) {
147 if (name.charLength > 0L) names.add(name)
148 }
149 }
150 return names.toArray()
151 }
152 /**
153 * The version reported by the linked character-conversion backend.
154 */
155 public val backendVersion: String get() {
156 @Unsafe {
157 return String.fromCString(charsetVersion())
158 }
159 }
160 }