@@ -121,7 +121,7 @@ function assemble()
121
121
122
122
let divText = document . getElementById ( "divOutputText" )
123
123
divText . innerHTML = output
124
- divText . style . whiteSpace = ( isError || format <= 1 ) ? "pre" : "no-wrap"
124
+ divText . style . whiteSpace = "no-wrap"
125
125
}
126
126
127
127
@@ -130,7 +130,7 @@ function makeRustString(str)
130
130
//console.log("makeRustString")
131
131
//console.log(str)
132
132
133
- let bytes = new TextEncoder ( "utf-8" ) . encode ( str )
133
+ let bytes = window . TextEncoder ? new TextEncoder ( "utf-8" ) . encode ( str ) : stringToUtf8ByteArray ( str )
134
134
//console.log(bytes)
135
135
136
136
let ptr = g_wasm . instance . exports . wasm_string_new ( bytes . length )
@@ -157,7 +157,7 @@ function readRustString(ptr)
157
157
158
158
//console.log(bytes)
159
159
160
- let str = new TextDecoder ( "utf-8" ) . decode ( new Uint8Array ( bytes ) )
160
+ let str = window . TextDecoder ? new TextDecoder ( "utf-8" ) . decode ( new Uint8Array ( bytes ) ) : utf8ByteArrayToString ( bytes )
161
161
//console.log(str)
162
162
return str
163
163
}
@@ -169,4 +169,64 @@ function dropRustString(ptr)
169
169
//console.log(ptr)
170
170
171
171
g_wasm . instance . exports . wasm_string_drop ( ptr )
172
+ }
173
+
174
+
175
+ // From https://github.com/google/closure-library/blob/e877b1eac410c0d842bcda118689759512e0e26f/closure/goog/crypt/crypt.js#L115
176
+ function stringToUtf8ByteArray ( str )
177
+ {
178
+ let out = [ ] , p = 0
179
+ for ( let i = 0 ; i < str . length ; i ++ ) {
180
+ let c = str . charCodeAt ( i )
181
+ if ( c < 128 ) {
182
+ out [ p ++ ] = c
183
+ } else if ( c < 2048 ) {
184
+ out [ p ++ ] = ( c >> 6 ) | 192
185
+ out [ p ++ ] = ( c & 63 ) | 128
186
+ } else if (
187
+ ( ( c & 0xFC00 ) == 0xD800 ) && ( i + 1 ) < str . length &&
188
+ ( ( str . charCodeAt ( i + 1 ) & 0xFC00 ) == 0xDC00 ) ) {
189
+ // Surrogate Pair
190
+ c = 0x10000 + ( ( c & 0x03FF ) << 10 ) + ( str . charCodeAt ( ++ i ) & 0x03FF )
191
+ out [ p ++ ] = ( c >> 18 ) | 240
192
+ out [ p ++ ] = ( ( c >> 12 ) & 63 ) | 128
193
+ out [ p ++ ] = ( ( c >> 6 ) & 63 ) | 128
194
+ out [ p ++ ] = ( c & 63 ) | 128
195
+ } else {
196
+ out [ p ++ ] = ( c >> 12 ) | 224
197
+ out [ p ++ ] = ( ( c >> 6 ) & 63 ) | 128
198
+ out [ p ++ ] = ( c & 63 ) | 128
199
+ }
200
+ }
201
+ return out
202
+ }
203
+
204
+
205
+ // From https://github.com/google/closure-library/blob/e877b1eac410c0d842bcda118689759512e0e26f/closure/goog/crypt/crypt.js#L149
206
+ function utf8ByteArrayToString ( bytes )
207
+ {
208
+ let out = [ ] , pos = 0 , c = 0
209
+ while ( pos < bytes . length ) {
210
+ let c1 = bytes [ pos ++ ]
211
+ if ( c1 < 128 ) {
212
+ out [ c ++ ] = String . fromCharCode ( c1 )
213
+ } else if ( c1 > 191 && c1 < 224 ) {
214
+ let c2 = bytes [ pos ++ ]
215
+ out [ c ++ ] = String . fromCharCode ( ( c1 & 31 ) << 6 | c2 & 63 )
216
+ } else if ( c1 > 239 && c1 < 365 ) {
217
+ // Surrogate Pair
218
+ let c2 = bytes [ pos ++ ]
219
+ let c3 = bytes [ pos ++ ]
220
+ let c4 = bytes [ pos ++ ]
221
+ let u = ( ( c1 & 7 ) << 18 | ( c2 & 63 ) << 12 | ( c3 & 63 ) << 6 | c4 & 63 ) - 0x10000
222
+ out [ c ++ ] = String . fromCharCode ( 0xD800 + ( u >> 10 ) )
223
+ out [ c ++ ] = String . fromCharCode ( 0xDC00 + ( u & 1023 ) )
224
+ } else {
225
+ let c2 = bytes [ pos ++ ]
226
+ let c3 = bytes [ pos ++ ]
227
+ out [ c ++ ] =
228
+ String . fromCharCode ( ( c1 & 15 ) << 12 | ( c2 & 63 ) << 6 | c3 & 63 )
229
+ }
230
+ }
231
+ return out . join ( '' )
172
232
}
0 commit comments