* fixed incorrect handling of unicode chars in output string ports.

This commit is contained in:
Abdulaziz Ghuloum 2007-05-19 22:36:52 -04:00
parent a1879ccc57
commit 7b8b50a6aa
3 changed files with 35 additions and 11 deletions

Binary file not shown.

View File

@ -50,12 +50,12 @@
(define concat (define concat
(lambda (bv i ls) (lambda (bv i ls)
(let ([n (sum i ls)]) (let ([n (sum i ls)])
(let ([outstr (make-string n)]) (let ([outbv ($make-bytevector n)])
(let f ([n (copy outstr bv i n)] [ls ls]) (let f ([n (copy outbv bv i n)] [ls ls])
(if (null? ls) (if (null? ls)
outstr (utf8-bytevector->string outbv)
(let ([a ($car ls)]) (let ([a ($car ls)])
(f (copy outstr a ($bytevector-length a) n) ($cdr ls))))))))) (f (copy outbv a ($bytevector-length a) n) ($cdr ls)))))))))
(define sum (define sum
(lambda (ac ls) (lambda (ac ls)
(cond (cond
@ -87,8 +87,7 @@
[($fx= si 0) di] [($fx= si 0) di]
[else [else
(let ([di ($fxsub1 di)] [si ($fxsub1 si)]) (let ([di ($fxsub1 di)] [si ($fxsub1 si)])
(string-set! dst di ($bytevector-set! dst di ($bytevector-u8-ref src si))
(integer->char ($bytevector-u8-ref src si)))
(f di si))])))) (f di si))]))))
(define bv-copy (define bv-copy

View File

@ -1,11 +1,11 @@
(library (ikarus writer) (library (ikarus writer)
(export write display format printf print-error error-handler (export write display format printf print-error error-handler
error) error)
(import (import
(ikarus system $chars) (ikarus system $chars)
(ikarus system $strings) (ikarus system $strings)
(ikarus system $vectors)
(ikarus system $fx) (ikarus system $fx)
(ikarus system $pairs) (ikarus system $pairs)
(ikarus system $symbols) (ikarus system $symbols)
@ -13,10 +13,24 @@
(except (ikarus) write display format printf print-error (except (ikarus) write display format printf print-error
error-handler error)) error-handler error))
;;; (define char-table ; first nonprintable chars
;;; '#("nul" "soh" "stx" "etx" "eot" "enq" "ack" "bel" "bs" "tab" "newline" (include "unicode/unicode-constituents.ss")
;;; "vt" "ff" "return" "so" "si" "dle" "dc1" "dc2" "dc3" "dc4" "nak"
;;; "syn" "etb" "can" "em" "sub" "esc" "fs" "gs" "rs" "us" "space")) (define (binary-search-on? n v)
(let ([k ($fx- ($vector-length v) 1)])
(let f ([i 0] [k k] [n n] [v v])
(cond
[($fx= i k) ($fx= ($fxlogand i 1) 1)]
[else
(let ([j ($fxsra ($fx+ i ($fx+ k 1)) 1)])
(cond
[($fx<= ($vector-ref v j) n) (f j k n v)]
[else (f i ($fx- j 1) n v)]))]))))
(define (unicode-printable-char? c)
(binary-search-on?
($char->fixnum c)
unicode-constituents-vector))
(define char-table ; first nonprintable chars (define char-table ; first nonprintable chars
'#("nul" "x1" "x2" "x3" "x4" "x5" "x6" "alarm" '#("nul" "x1" "x2" "x3" "x4" "x5" "x6" "alarm"
@ -55,6 +69,9 @@
[(fx= i 127) [(fx= i 127)
(write-char #\\ p) (write-char #\\ p)
(write-char* "delete" p)] (write-char* "delete" p)]
[(unicode-printable-char? x)
(write-char #\\ p)
(write-char x p)]
[else [else
(write-char #\\ p) (write-char #\\ p)
(write-char #\x p) (write-char #\x p)
@ -264,6 +281,10 @@
(cond (cond
[(in-map? b subsequents-map) [(in-map? b subsequents-map)
(write-char c p)] (write-char c p)]
[($fx< b 128)
(write-inline-hex b p)]
[(unicode-printable-char? c)
(write-char c p)]
[else [else
(write-inline-hex b p)])) (write-inline-hex b p)]))
(write-subsequent* str ($fxadd1 i) j p))) (write-subsequent* str ($fxadd1 i) j p)))
@ -280,6 +301,8 @@
[b0 ($char->fixnum c0)]) [b0 ($char->fixnum c0)])
(cond (cond
[(in-map? b0 initials-map) (write-char c0 p)] [(in-map? b0 initials-map) (write-char c0 p)]
[($fx< b0 128) (write-inline-hex b0 p)]
[(unicode-printable-char? c0) (write-char c0 p)]
[else (write-inline-hex b0 p)]) [else (write-inline-hex b0 p)])
(write-subsequent* str 1 n p))])))) (write-subsequent* str 1 n p))]))))
@ -369,6 +392,8 @@
(write-char c p)] (write-char c p)]
[($fx< b 127) [($fx< b 127)
(write-char c p)] (write-char c p)]
[(unicode-printable-char? c)
(write-char c p)]
[else (write-inline-hex b p)])) [else (write-inline-hex b p)]))
(loop x (fxadd1 i) n p)))) (loop x (fxadd1 i) n p))))
(write-char #\" p) (write-char #\" p)