blob: 2050cbc8ce81b6939409579a64dcbda384d44ebf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
(.module:
[lux #*
["@" target]
["." host]
[abstract
[codec (#+ Codec)]]
[control
["." try (#+ Try)]]
[data
[binary (#+ Binary)]]
[type
abstract]])
## https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html
(abstract: #export Encoding
Text
(template [<name> <encoding>]
[(def: #export <name> Encoding (:abstraction <encoding>))]
[ascii "ASCII"]
[ibm_37 "IBM037"]
[ibm_273 "IBM273"]
[ibm_277 "IBM277"]
[ibm_278 "IBM278"]
[ibm_280 "IBM280"]
[ibm_284 "IBM284"]
[ibm_285 "IBM285"]
[ibm_290 "IBM290"]
[ibm_297 "IBM297"]
[ibm_300 "IBM300"]
[ibm_420 "IBM420"]
[ibm_424 "IBM424"]
[ibm_437 "IBM437"]
[ibm_500 "IBM500"]
[ibm_737 "IBM737"]
[ibm_775 "IBM775"]
[ibm_833 "IBM833"]
[ibm_834 "IBM834"]
[ibm_838 "IBM-Thai"]
[ibm_850 "IBM850"]
[ibm_852 "IBM852"]
[ibm_855 "IBM855"]
[ibm_856 "IBM856"]
[ibm_857 "IBM857"]
[ibm_858 "IBM00858"]
[ibm_860 "IBM860"]
[ibm_861 "IBM861"]
[ibm_862 "IBM862"]
[ibm_863 "IBM863"]
[ibm_864 "IBM864"]
[ibm_865 "IBM865"]
[ibm_866 "IBM866"]
[ibm_868 "IBM868"]
[ibm_869 "IBM869"]
[ibm_870 "IBM870"]
[ibm_871 "IBM871"]
[ibm_874 "IBM874"]
[ibm_875 "IBM875"]
[ibm_918 "IBM918"]
[ibm_921 "IBM921"]
[ibm_922 "IBM922"]
[ibm_930 "IBM930"]
[ibm_933 "IBM933"]
[ibm_935 "IBM935"]
[ibm_937 "IBM937"]
[ibm_939 "IBM939"]
[ibm_942 "IBM942"]
[ibm_942c "IBM942C"]
[ibm_943 "IBM943"]
[ibm_943c "IBM943C"]
[ibm_948 "IBM948"]
[ibm_949 "IBM949"]
[ibm_949c "IBM949C"]
[ibm_950 "IBM950"]
[ibm_964 "IBM964"]
[ibm_970 "IBM970"]
[ibm_1006 "IBM1006"]
[ibm_1025 "IBM1025"]
[ibm_1026 "IBM1026"]
[ibm_1046 "IBM1046"]
[ibm_1047 "IBM1047"]
[ibm_1097 "IBM1097"]
[ibm_1098 "IBM1098"]
[ibm_1112 "IBM1112"]
[ibm_1122 "IBM1122"]
[ibm_1123 "IBM1123"]
[ibm_1124 "IBM1124"]
[ibm_1140 "IBM01140"]
[ibm_1141 "IBM01141"]
[ibm_1142 "IBM01142"]
[ibm_1143 "IBM01143"]
[ibm_1144 "IBM01144"]
[ibm_1145 "IBM01145"]
[ibm_1146 "IBM01146"]
[ibm_1147 "IBM01147"]
[ibm_1148 "IBM01148"]
[ibm_1149 "IBM01149"]
[ibm_1166 "IBM1166"]
[ibm_1364 "IBM1364"]
[ibm_1381 "IBM1381"]
[ibm_1383 "IBM1383"]
[ibm_33722 "IBM33722"]
[iso_2022_cn "ISO-2022-CN"]
[iso2022_cn_cns "ISO2022-CN-CNS"]
[iso2022_cn_gb "ISO2022-CN-GB"]
[iso_2022_jp "ISO-2022-JP"]
[iso_2022_jp_2 "ISO-2022-JP-2"]
[iso_2022_kr "ISO-2022-KR"]
[iso_8859_1 "ISO-8859-1"]
[iso_8859_2 "ISO-8859-2"]
[iso_8859_3 "ISO-8859-3"]
[iso_8859_4 "ISO-8859-4"]
[iso_8859_5 "ISO-8859-5"]
[iso_8859_6 "ISO-8859-6"]
[iso_8859_7 "ISO-8859-7"]
[iso_8859_8 "ISO-8859-8"]
[iso_8859_9 "ISO-8859-9"]
[iso_8859_11 "iso-8859-11"]
[iso_8859_13 "ISO-8859-13"]
[iso_8859_15 "ISO-8859-15"]
[mac_arabic "MacArabic"]
[mac_central_europe "MacCentralEurope"]
[mac_croatian "MacCroatian"]
[mac_cyrillic "MacCyrillic"]
[mac_dingbat "MacDingbat"]
[mac_greek "MacGreek"]
[mac_hebrew "MacHebrew"]
[mac_iceland "MacIceland"]
[mac_roman "MacRoman"]
[mac_romania "MacRomania"]
[mac_symbol "MacSymbol"]
[mac_thai "MacThai"]
[mac_turkish "MacTurkish"]
[mac_ukraine "MacUkraine"]
[utf_8 "UTF-8"]
[utf_16 "UTF-16"]
[utf_32 "UTF-32"]
[windows_31j "windows-31j"]
[windows_874 "windows-874"]
[windows_949 "windows-949"]
[windows_950 "windows-950"]
[windows_1250 "windows-1250"]
[windows_1252 "windows-1252"]
[windows_1251 "windows-1251"]
[windows_1253 "windows-1253"]
[windows_1254 "windows-1254"]
[windows_1255 "windows-1255"]
[windows_1256 "windows-1256"]
[windows_1257 "windows-1257"]
[windows_1258 "windows-1258"]
[windows_iso2022jp "windows-iso2022jp"]
[windows_50220 "windows-50220"]
[windows_50221 "windows-50221"]
[cesu_8 "CESU-8"]
[koi8_r "KOI8-R"]
[koi8_u "KOI8-U"]
)
(def: #export name
(-> Encoding Text)
(|>> :representation))
)
(with_expansions [<jvm> (as_is (host.import: java/lang/String
["#::."
(new [[byte] java/lang/String])
(getBytes [java/lang/String] [byte])]))]
(for {@.old (as_is <jvm>)
@.jvm (as_is <jvm>)
@.js
(as_is (host.import: Uint8Array)
## On Node
(host.import: Buffer
(#static from #as from|encode [host.String host.String] Buffer)
(#static from #as from|decode [Uint8Array] Buffer)
(toString [host.String] host.String))
## On the browser
(host.import: TextEncoder
(new [host.String])
(encode [host.String] Uint8Array))
(host.import: TextDecoder
(new [host.String])
(decode [Uint8Array] host.String)))}
(as_is)))
(def: (to_utf8 value)
(-> Text Binary)
(for {@.old
(java/lang/String::getBytes (..name ..utf_8)
## TODO: Remove coercion below.
## The coercion below may seem
## gratuitous, but removing it
## causes a grave compilation problem.
(:coerce java/lang/String value))
@.jvm
(java/lang/String::getBytes (..name ..utf_8) value)
@.js
(cond host.on_nashorn?
(:coerce Binary ("js object do" "getBytes" value ["utf8"]))
host.on_node_js?
(|> (Buffer::from|encode [value "utf8"])
## This coercion is valid as per NodeJS's documentation:
## https://nodejs.org/api/buffer.html#buffer_buffers_and_typedarrays
(:coerce Uint8Array))
## On the browser
(|> (TextEncoder::new [(..name ..utf_8)])
(TextEncoder::encode [value]))
)
@.python
(:coerce Binary ("python apply" (:assume ("python constant" "bytearray")) value "utf-8"))}))
(def: (from_utf8 value)
(-> Binary (Try Text))
(with_expansions [<jvm> (#try.Success (java/lang/String::new value (..name ..utf_8)))]
(for {@.old <jvm>
@.jvm <jvm>
@.js
(cond host.on_nashorn?
(|> ("js object new" ("js constant" "java.lang.String") [value "utf8"])
(:coerce Text)
#try.Success)
host.on_node_js?
(|> (Buffer::from|decode [value])
(Buffer::toString ["utf8"])
#try.Success)
## On the browser
(|> (TextDecoder::new [(..name ..utf_8)])
(TextDecoder::decode [value])
#try.Success))
@.python
(host.try (:coerce Text ("python object do" "decode" (:assume value) "utf-8")))})))
(structure: #export utf8
(Codec Binary Text)
(def: encode ..to_utf8)
(def: decode ..from_utf8))
|