Debian Codegraph @

scheme/ikarus.unicode-conversion.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 (library (ikarus transcoders)
18 (export string->utf8 utf8->string string->utf16 string->utf32
19 utf16->string utf32->string string->bytevector
20 bytevector->string)
21 (import (except (ikarus) string->utf8 utf8->string string->utf16
22 utf16->string string->utf32 utf32->string
23 string->bytevector bytevector->string)
24 (ikarus system $strings)
25 (ikarus system $bytevectors)
26 (ikarus system $fx)
27 (ikarus system $chars))
28
29 ;;; From http://en.wikipedia.org/wiki/UTF-8
30 ;;; hexadecimal binary scalar value UTF8
31 ;;; 000000-00007F 00000000_00000000_0zzzzzzz 0zzzzzzz
32 ;;; 000080-0007FF 00000000_00000yyy_yyzzzzzz 110yyyyy 10zzzzzz
33 ;;; 000800-00FFFF 00000000_xxxxyyyy_yyzzzzzz 1110xxxx 10yyyyyy 10zzzzzz
34 ;;; 010000-10FFFF 000wwwxx_xxxxyyyy_yyzzzzzz 11110www 10xxxxxx 10yyyyyy 10zzzzzz
35
36 ;;; valid ranges: [000000 - 00D7FF] \union [00E000 - 10FFFF]
37 ;;; invalid hole: [00D800 - 00DFFF]
38
39 ;;; handling-modes: ignore, replace, raise
40 ;;; ignore: skips over the offending bytes
41 ;;; replace: places a U+FFFD in place of the malformed bytes
42 ;;; raise: raises an die
43
44 ;;; It appears that utf-8 data can start with a #xEF #xBB #xBF BOM!
45
46 (define integer->char/invalid
47 (lambda (n)
48 (cond
49 [(not (fixnum? n)) #\xFFFD]
50 [($fx<= n #xD7FF) ($fixnum->char n)]
51 [($fx< n #xE000) #\xFFFD]
52 [($fx<= n #x10FFFF) ($fixnum->char n)]
53 [else #\xFFFD])))
54
55 (define string->utf8
56 (lambda (str)
57 (define (utf8-string-size str)
58 (let f ([str str] [i 0] [j ($string-length str)] [n 0])
59 (cond
60 [($fx= i j) n]
61 [else
62 (let ([c ($string-ref str i)])
63 (let ([b ($char->fixnum c)])
64 (f str ($fxadd1 i) j
65 ($fx+ n
66 (cond
67 [($fx<= b #x7F) 1]
68 [($fx<= b #x7FF) 2]
69 [($fx<= b #xFFFF) 3]
70 [else 4])))))])))
71 (define (fill-utf8-bytevector bv str)
72 (let f ([bv bv] [str str] [i 0] [j 0] [n ($string-length str)])
73 (cond
74 [($fx= i n) bv]
75 [else
76 (let ([c ($string-ref str i)])
77 (let ([b ($char->fixnum c)])
78 (cond
79 [($fx<= b #x7F)
80 ($bytevector-set! bv j b)
81 (f bv str ($fxadd1 i) ($fxadd1 j) n)]
82 [($fx<= b #x7FF)
83 ($bytevector-set! bv j
84 ($fxlogor #b11000000 ($fxsra b 6)))
85 ($bytevector-set! bv ($fx+ j 1)
86 ($fxlogor #b10000000 ($fxlogand b #b111111)))
87 (f bv str ($fxadd1 i) ($fx+ j 2) n)]
88 [($fx<= b #xFFFF)
89 ($bytevector-set! bv j
90 ($fxlogor #b11100000 ($fxsra b 12)))
91 ($bytevector-set! bv ($fx+ j 1)
92 ($fxlogor #b10000000 ($fxlogand ($fxsra b 6) #b111111)))
93 ($bytevector-set! bv ($fx+ j 2)
94 ($fxlogor #b10000000 ($fxlogand b #b111111)))
95 (f bv str ($fxadd1 i) ($fx+ j 3) n)]
96 [else
97 ($bytevector-set! bv j
98 ($fxlogor #b11110000 ($fxsra b 18)))
99 ($bytevector-set! bv ($fx+ j 1)
100 ($fxlogor #b10000000 ($fxlogand ($fxsra b 12) #b111111)))
101 ($bytevector-set! bv ($fx+ j 2)
102 ($fxlogor #b10000000 ($fxlogand ($fxsra b 6) #b111111)))
103 ($bytevector-set! bv ($fx+ j 3)
104 ($fxlogor #b10000000 ($fxlogand b #b111111)))
105 (f bv str ($fxadd1 i) ($fx+ j 4) n)])))])))
106 (unless (string? str)
107 (die 'string->utf8 "not a string" str))
108 (fill-utf8-bytevector
109 ($make-bytevector (utf8-string-size str))
110 str)))
111
112 (define (utf8->string x)
113 (unless (bytevector? x)
114 (die 'utf8->string "not a bytevector" x))
115 (decode-utf8-bytevector x 'replace))
116
117 (define decode-utf8-bytevector
118 (let ()
119 (define who 'decode-utf8-bytevector)
120 (define (count bv i mode)
121 (let f ([x bv] [i i] [j ($bytevector-length bv)] [n 0] [mode mode])
122 (cond
123 [($fx= i j) n]
124 [else
125 (let ([b0 ($bytevector-u8-ref x i)])
126 (cond
127 [($fx<= b0 #x7F)
128 (f x ($fxadd1 i) j ($fxadd1 n) mode)]
129 [($fx= ($fxsra b0 5) #b110)
130 (let ([i ($fxadd1 i)])
131 (cond
132 [($fx< i j)
133 (let ([b1 ($bytevector-u8-ref x i)])
134 (cond
135 [(and ($fx= ($fxsra b1 6) #b10)
136 ;;; 000080-0007FF
137 (let ([n (fxlogor (fxsll (fxlogand b0 #x1F) 6)
138 (fxlogand b1 #x3F))])
139 (and (fx>= n #x80)
140 (fx<= n #x7FF))))
141 (f x ($fxadd1 i) j ($fxadd1 n) mode)]
142 [(eq? mode 'ignore)
143 (f x i j n mode)]
144 [(eq? mode 'replace)
145 (f x i j ($fxadd1 n) mode)]
146 [else
147 (die who "invalid byte sequence at idx of bytevector"
148 b0 b1 i bv)]))]
149 [(eq? mode 'ignore) n]
150 [(eq? mode 'replace) ($fxadd1 n)]
151 [else
152 (die who "invalid byte near end of bytevector" b0)]))]
153 [($fx= ($fxsra b0 4) #b1110)
154 (cond
155 [($fx< ($fx+ i 2) j)
156 (let ([b1 ($bytevector-u8-ref x ($fx+ i 1))]
157 [b2 ($bytevector-u8-ref x ($fx+ i 2))])
158 (cond
159 [(and ($fx= ($fxsra ($fxlogor b1 b2) 6) #b10)
160 (let ([n (fx+ (fxsll (fxlogand b0 #xF) 12)
161 (fx+ (fxsll (fxlogand b1 #x3F) 6)
162 (fxlogand b2 #x3F)))])
163 ;;; REVIEW LATER ; 000800-00FFFF
164 (and (fx>= n #x0000800) (fx<= n #x00FFFF))))
165 (f x ($fx+ i 3) j ($fxadd1 n) mode)]
166 [(eq? mode 'ignore)
167 (f x ($fxadd1 i) j n mode)]
168 [(eq? mode 'replace)
169 (f x ($fxadd1 i) j ($fxadd1 n) mode)]
170 [else (die who "invalid sequence" b0 b1 b2)]))]
171 [(eq? mode 'ignore) (f x ($fxadd1 i) j n mode)]
172 [(eq? mode 'replace) (f x ($fxadd1 i) j ($fxadd1 n) mode)]
173 [else (die who "incomplete char sequence")])]
174 [($fx= ($fxsra b0 3) #b11110)
175 (cond
176 [($fx< ($fx+ i 3) j)
177 (let ([b1 ($bytevector-u8-ref x ($fx+ i 1))]
178 [b2 ($bytevector-u8-ref x ($fx+ i 2))]
179 [b3 ($bytevector-u8-ref x ($fx+ i 3))])
180 (cond
181 [(and ($fx= ($fxsra ($fxlogor b1 ($fxlogor b2 b3)) 6) #b10)
182 (let ([n
183 ($fx+ ($fxlogand b3 #b111111)
184 ($fx+ ($fxsll ($fxlogand b2 #b111111) 6)
185 ($fx+ ($fxsll ($fxlogand b1 #b111111) 12)
186 ($fxsll ($fxlogand b0 #b111) 18))))])
187 ;;; 010000-10FFFF
188 (and (fx>= n #x10000) (fx<= n #x10FFFF))))
189 (f x ($fx+ i 4) j ($fxadd1 n) mode)]
190 [(eq? mode 'ignore)
191 (f x ($fxadd1 i) j n mode)]
192 [(eq? mode 'replace)
193 (f x ($fxadd1 i) j ($fxadd1 n) mode)]
194 [else (die who "invalid sequence" b0 b1 b2 b3)]))]
195 [(eq? mode 'ignore) (f x ($fxadd1 i) j n mode)]
196 [(eq? mode 'replace) (f x ($fxadd1 i) j ($fxadd1 n) mode)]
197 [else (die who "incomplete char sequence")])]
198 [(eq? mode 'ignore) (f x ($fxadd1 i) j n mode)]
199 [(eq? mode 'replace) (f x ($fxadd1 i) j ($fxadd1 n) mode)]
200 [else (die who "invalid byte at index of bytevector" b0 i x)]))])))
201 (define (fill str bv i mode)
202 (let f ([str str] [x bv] [i i] [j ($bytevector-length bv)] [n 0] [mode mode])
203 (cond
204 [($fx= i j) str]
205 [else
206 (let ([b0 ($bytevector-u8-ref x i)])
207 (cond
208 [($fx<= b0 #x7F)
209 ($string-set! str n ($fixnum->char b0))
210 (f str x ($fxadd1 i) j ($fxadd1 n) mode)]
211 [($fx= ($fxsra b0 5) #b110)
212 (let ([i ($fxadd1 i)])
213 (cond
214 [($fx< i j)
215 (let ([b1 ($bytevector-u8-ref x i)])
216 (cond
217 [(and ($fx= ($fxsra b1 6) #b10)
218 ;;; 000080-0007FF
219 (let ([n (fxlogor (fxsll (fxlogand b0 #x1F) 6)
220 (fxlogand b1 #x3F))])
221 (and (fx>= n #x80)
222 (fx<= n #x7FF)
223 ($fixnum->char n)))) =>
224 (lambda (c)
225 ($string-set! str n c)
226 (f str x ($fxadd1 i) j ($fxadd1 n) mode))]
227 [(eq? mode 'ignore)
228 (f str x i j n mode)]
229 [(eq? mode 'replace)
230 ($string-set! str n ($fixnum->char #xFFFD))
231 (f str x i j ($fxadd1 n) mode)]
232 [else (die who "BUG")]))]
233 [(eq? mode 'ignore) str]
234 [(eq? mode 'replace)
235 ($string-set! str n ($fixnum->char #xFFFD))
236 str]
237 [else (die who "BUG")]))]
238 [($fx= ($fxsra b0 4) #b1110)
239 (cond
240 [($fx< ($fx+ i 2) j)
241 (let ([b1 ($bytevector-u8-ref x ($fx+ i 1))]
242 [b2 ($bytevector-u8-ref x ($fx+ i 2))])
243 (cond
244 [(and ($fx= ($fxsra ($fxlogor b1 b2) 6) #b10)
245 (let ([n (fx+ (fxsll (fxlogand b0 #xF) 12)
246 (fx+ (fxsll (fxlogand b1 #x3F) 6)
247 (fxlogand b2 #x3F)))])
248 ;;; REVIEW LATER ; 000800-00FFFF
249 (and (and (fx>= n #x000800) (fx<= n #x00FFFF))
250 ($fixnum->char n)))) =>
251 (lambda (c)
252 ($string-set! str n c)
253 (f str x ($fx+ i 3) j ($fxadd1 n) mode))]
254 [(eq? mode 'ignore)
255 (f str x ($fxadd1 i) j n mode)]
256 [(eq? mode 'replace)
257 ($string-set! str n ($fixnum->char #xFFFD))
258 (f str x ($fxadd1 i) j ($fxadd1 n) mode)]
259 [else (die who "BUG")]))]
260 [(eq? mode 'ignore) (f str x ($fxadd1 i) j n mode)]
261 [(eq? mode 'replace)
262 ($string-set! str n ($fixnum->char #xFFFD))
263 (f str x ($fxadd1 i) j ($fxadd1 n) mode)]
264 [else (die who "BUG")])]
265 [($fx= ($fxsra b0 3) #b11110)
266 (cond
267 [($fx< ($fx+ i 3) j)
268 (let ([b1 ($bytevector-u8-ref x ($fx+ i 1))]
269 [b2 ($bytevector-u8-ref x ($fx+ i 2))]
270 [b3 ($bytevector-u8-ref x ($fx+ i 3))])
271 (cond
272 [(and ($fx= ($fxsra ($fxlogor b1 ($fxlogor b2 b3)) 6) #b10)
273 (let ([n
274 ($fx+ ($fxlogand b3 #b111111)
275 ($fx+ ($fxsll ($fxlogand b2 #b111111) 6)
276 ($fx+ ($fxsll ($fxlogand b1 #b111111) 12)
277 ($fxsll ($fxlogand b0 #b111) 18))))])
278 ;;; 010000-10FFFF
279 (and (fx>= n #x10000)
280 (fx<= n #x10FFFF)
281 ($fixnum->char n)))) =>
282 (lambda (c)
283 ($string-set! str n c)
284 (f str x ($fx+ i 4) j ($fxadd1 n) mode))]
285 [(eq? mode 'ignore)
286 (f str x ($fxadd1 i) j n mode)]
287 [(eq? mode 'replace)
288 ($string-set! str n ($fixnum->char #xFFFD))
289 (f str x ($fxadd1 i) j ($fxadd1 n) mode)]
290 [else (die who "BUG")]))]
291 [(eq? mode 'ignore) (f str x ($fxadd1 i) j n mode)]
292 [(eq? mode 'replace)
293 ($string-set! str n ($fixnum->char #xFFFD))
294 (f str x ($fxadd1 i) j ($fxadd1 n) mode)]
295 [else (die who "BUG")])]
296 [(eq? mode 'ignore) (f str x ($fxadd1 i) j n mode)]
297 [(eq? mode 'replace)
298 ($string-set! str n ($fixnum->char #xFFFD))
299 (f str x ($fxadd1 i) j ($fxadd1 n) mode)]
300 [else (die who "BUG")]))])))
301 (define (has-bom? bv)
302 (and (fx> (bytevector-length bv) 3)
303 (fx= (bytevector-u8-ref bv 0) #xEF)
304 (fx= (bytevector-u8-ref bv 1) #xBB)
305 (fx= (bytevector-u8-ref bv 2) #xBF)))
306 (define (convert bv mode)
307 (cond
308 [(has-bom? bv)
309 (fill ($make-string (count bv 3 mode)) bv 3 mode)]
310 [else
311 (fill ($make-string (count bv 0 mode)) bv 0 mode)]))
312 (case-lambda
313 [(bv) (convert bv 'raise)]
314 [(bv handling-mode)
315 (unless (memq handling-mode '(ignore replace raise))
316 (die 'decode-utf8-bytevector
317 "not a valid handling mode"
318 handling-mode))
319 (convert bv handling-mode)])))
320
321
322 ;;; From: http://tools.ietf.org/html/rfc2781
323 ;;;
324 ;;; 2.1 Encoding UTF-16
325 ;;;
326 ;;; Encoding of a single character from an ISO 10646 character value
327 ;;; to UTF-16 proceeds as follows. Let U be the character number, no
328 ;;; greater than 0x10FFFF.
329 ;;;
330 ;;; 1) If U < 0x10000, encode U as a 16-bit unsigned integer and terminate.
331 ;;;
332 ;;; 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
333 ;;; U' must be less than or equal to 0xFFFFF. That is, U' can be
334 ;;; represented in 20 bits.
335 ;;;
336 ;;; 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
337 ;;; 0xDC00, respectively. These integers each have 10 bits free to
338 ;;; encode the character value, for a total of 20 bits.
339 ;;;
340 ;;; 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
341 ;;; bits of W1 and the 10 low-order bits of U' to the 10 low-order
342 ;;; bits of W2. Terminate.
343 ;;;
344 ;;; Graphically, steps 2 through 4 look like:
345 ;;; U' = yyyyyyyyyyxxxxxxxxxx
346 ;;; W1 = 110110yyyyyyyyyy
347 ;;; W2 = 110111xxxxxxxxxx
348 ;;;
349 ;;; Decoding of a single character from UTF-16 to an ISO 10646 character
350 ;;; value proceeds as follows. Let W1 be the next 16-bit integer in the
351 ;;; sequence of integers representing the text. Let W2 be the (eventual)
352 ;;; next integer following W1.
353 ;;;
354 ;;; 1) If W1 < 0xD800 or W1 > 0xDFFF, the character value U is the value
355 ;;; of W1. Terminate.
356 ;;;
357 ;;; 2) Determine if W1 is between 0xD800 and 0xDBFF. If not, the sequence
358 ;;; is in error and no valid character can be obtained using W1.
359 ;;; Terminate.
360 ;;;
361 ;;; 3) If there is no W2 (that is, the sequence ends with W1), or if W2
362 ;;; is not between 0xDC00 and 0xDFFF, the sequence is in error.
363 ;;; Terminate.
364 ;;;
365 ;;; 4) Construct a 20-bit unsigned integer U', taking the 10 low-order
366 ;;; bits of W1 as its 10 high-order bits and the 10 low-order bits of
367 ;;; W2 as its 10 low-order bits.
368 ;;; 5) Add 0x10000 to U' to obtain the character value U.
369 ;;; Terminate.
370
371
372
373 (module (string->utf16)
374 (define ($string->utf16 str endianness)
375 (define (count-surr* str len i n)
376 (cond
377 [(fx= i len) n]
378 [else
379 (let ([c (string-ref str i)])
380 (cond
381 [(char<? c #\x10000)
382 (count-surr* str len (fx+ i 1) n)]
383 [else
384 (count-surr* str len (fx+ i 1) (fx+ n 1))]))]))
385 (define (bvfill str bv i j len endianness)
386 (cond
387 [(fx= i len) bv]
388 [else
389 (let ([n (char->integer (string-ref str i))])
390 (cond
391 [(fx< n #x10000)
392 (bytevector-u16-set! bv j n endianness)
393 (bvfill str bv (fx+ i 1) (fx+ j 2) len endianness)]
394 [else
395 (let ([u^ (fx- n #x10000)])
396 (bytevector-u16-set! bv j
397 (fxlogor (fxsll #b110110 10) (fxsra u^ 10))
398 endianness)
399 (bytevector-u16-set! bv (fx+ j 2)
400 (fxlogor (fxsll #b110111 10) (fxlogand u^ #x3FF))
401 endianness))
402 (bvfill str bv (fx+ i 1) (fx+ j 4) len endianness)]))]))
403 (let ([len ($string-length str)])
404 (let ([n (count-surr* str len 0 0)])
405 ;;; FIXME: maybe special case for n=0 later
406 (let ([bv (make-bytevector (fxsll (fx+ len n) 1))])
407 (bvfill str bv 0 0 len endianness)))))
408 (define string->utf16
409 (case-lambda
410 [(str)
411 (unless (string? str)
412 (die 'string->utf16 "not a string" str))
413 ($string->utf16 str 'big)]
414 [(str endianness)
415 (unless (string? str)
416 (die 'string->utf16 "not a string" str))
417 (unless (memv endianness '(big little))
418 (die 'string->utf16 "invalid endianness" endianness))
419 ($string->utf16 str endianness)])))
420
421 (module (utf16->string)
422 (define who 'utf16->string)
423 (define (count-size bv endianness i len n)
424 (cond
425 [(fx= i len)
426 (if (fx= len (bytevector-length bv))
427 n
428 (+ n 1))]
429 [else
430 (let ([w1 (bytevector-u16-ref bv i endianness)])
431 (cond
432 [(or (fx< w1 #xD800) (fx> w1 #xDFFF))
433 (count-size bv endianness (+ i 2) len (+ n 1))]
434 [(not (fx<= #xD800 w1 #xDBFF)) ;;; error sequence
435 (count-size bv endianness (+ i 2) len (+ n 1))]
436 [(<= (+ i 4) (bytevector-length bv))
437 (let ([w2 (bytevector-u16-ref bv (+ i 2) endianness)])
438 (cond
439 [(not (<= #xDC00 w2 #xDFFF))
440 ;;; do we skip w2 also?
441 ;;; I won't. Just w1 is an error
442 (count-size bv endianness (+ i 2) len (+ n 1))]
443 [else
444 ;;; 4-byte sequence is ok
445 (count-size bv endianness (+ i 4) len (+ n 1))]))]
446 [else
447 ;;; error again
448 (count-size bv endianness (+ i 2) len (+ n 1))]))]))
449 (define (fill bv endianness str i len n)
450 (cond
451 [(fx= i len)
452 (unless (fx= len (bytevector-length bv))
453 (string-set! str n #\xFFFD))
454 str]
455 [else
456 (let ([w1 (bytevector-u16-ref bv i endianness)])
457 (cond
458 [(or (fx< w1 #xD800) (fx> w1 #xDFFF))
459 (string-set! str n (integer->char/invalid w1))
460 (fill bv endianness str (+ i 2) len (+ n 1))]
461 [(not (fx<= #xD800 w1 #xDBFF)) ;;; error sequence
462 (string-set! str n #\xFFFD)
463 (fill bv endianness str (+ i 2) len (+ n 1))]
464 [(<= (+ i 4) (bytevector-length bv))
465 (let ([w2 (bytevector-u16-ref bv (+ i 2) endianness)])
466 (cond
467 [(not (<= #xDC00 w2 #xDFFF))
468 ;;; do we skip w2 also?
469 ;;; I won't. Just w1 is an error
470 (string-set! str n #\xFFFD)
471 (fill bv endianness str (+ i 2) len (+ n 1))]
472 [else
473 (string-set! str n
474 (integer->char/invalid
475 (+ #x10000
476 (fxlogor (fxsll (fxlogand w1 #x3FF) 10)
477 (fxlogand w2 #x3FF)))))
478 (fill bv endianness str (+ i 4) len (+ n 1))]))]
479 [else
480 ;;; error again
481 (string-set! str n #\xFFFD)
482 (fill bv endianness str (+ i 2) len (+ n 1))]))]))
483 (define (decode bv endianness start)
484 (let ([len (fxand (bytevector-length bv) -2)])
485 (let ([n (count-size bv endianness start len 0)])
486 (let ([str (make-string n)])
487 (fill bv endianness str start len 0)))))
488 (define ($utf16->string bv endianness em?)
489 (define (bom-present bv)
490 (and (fx>= (bytevector-length bv) 2)
491 (let ([n (bytevector-u16-ref bv 0 'big)])
492 (cond
493 [(fx= n #xFEFF) 'big]
494 [(fx= n #xFFFE) 'little]
495 [else #f]))))
496 (unless (bytevector? bv)
497 (die who "not a bytevector" bv))
498 (unless (memv endianness '(big little))
499 (die who "invalid endianness" endianness))
500 (cond
501 [em? (decode bv endianness 0)]
502 [(bom-present bv) =>
503 (lambda (endianness)
504 (decode bv endianness 2))]
505 [else
506 (decode bv endianness 0)]))
507 (define utf16->string
508 (case-lambda
509 [(bv endianness)
510 ($utf16->string bv endianness #f)]
511 [(bv endianness em?)
512 ($utf16->string bv endianness em?)])))
513
514
515
516 (module (string->utf32)
517 (define who 'string->utf32)
518 (define (vfill str bv i len endianness)
519 (cond
520 [(fx= i len) bv]
521 [else
522 (bytevector-u32-set! bv (fxsll i 2)
523 (char->integer (string-ref str i))
524 endianness)
525 (vfill str bv (fx+ i 1) len endianness)]))
526 (define ($string->utf32 str endianness)
527 (let ([len (string-length str)])
528 (vfill str (make-bytevector (fxsll len 2)) 0 len endianness)))
529 (define string->utf32
530 (case-lambda
531 [(str)
532 (unless (string? str)
533 (die who "not a string" str))
534 ($string->utf32 str 'big)]
535 [(str endianness)
536 (unless (string? str)
537 (die who "not a string" str))
538 (unless (memq endianness '(little big))
539 (die who "invalid endianness" endianness))
540 ($string->utf32 str endianness)])))
541
542
543
544 (module (utf32->string)
545 (define who 'utf32->string)
546 (define (fill bv endianness str i j n)
547 (cond
548 [(fx= i j)
549 (unless (fx= n (string-length str))
550 (string-set! str n #\xFFFD))
551 str]
552 [else
553 (string-set! str n
554 (integer->char/invalid
555 (bytevector-u32-ref bv i endianness)))
556 (fill bv endianness str (fx+ i 4) j (fx+ n 1))]))
557 (define (decode bv endianness start)
558 (let ([bvlen (bytevector-length bv)])
559 (let ([strlen (fxsra (fx+ (fx- bvlen start) 3) 2)])
560 (fill bv endianness (make-string strlen)
561 start (fxand bvlen -4)
562 0))))
563 (define ($utf32->string bv endianness em?)
564 (define (bom-present bv)
565 (and (fx>= (bytevector-length bv) 4)
566 (let ([n (bytevector-u32-ref bv 0 'big)])
567 (cond
568 [(= n #x0000FEFF) 'big]
569 [(= n #xFFFE0000) 'little]
570 [else #f]))))
571 (unless (bytevector? bv)
572 (die who "not a bytevector" bv))
573 (unless (memv endianness '(big little))
574 (die who "invalid endianness" endianness))
575 (cond
576 [em? (decode bv endianness 0)]
577 [(bom-present bv) =>
578 (lambda (endianness)
579 (decode bv endianness 4))]
580 [else
581 (decode bv endianness 0)]))
582 (define utf32->string
583 (case-lambda
584 [(bv endianness)
585 ($utf32->string bv endianness #f)]
586 [(bv endianness em?)
587 ($utf32->string bv endianness em?)])))
588
589
590 (define (bytevector->string bv t)
591 (define who 'bytevector->string)
592 (unless (bytevector? bv)
593 (die who "not a bytevector" bv))
594 (unless (transcoder? t)
595 (die who "not a transcoder" t))
596 (call-with-port (open-bytevector-input-port bv t)
597 (lambda (tcip)
598 (let ([r (get-string-all tcip)])
599 (if (eof-object? r) "" r)))))
600
601 (define (string->bytevector str t)
602 (define who 'string->bytevector)
603 (unless (string? str)
604 (die who "not a string" str))
605 (unless (transcoder? t)
606 (die who "not a transcoder" t))
607 (call-with-bytevector-output-port
608 (lambda (tcop)
609 (put-string tcop str))
610 t))
611 )