mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-29 22:40:34 +02:00
format handles ~@c better
* module/ice-9/format.scm (format): Fix ascii-ism in ~@c. Pull flush-output? into the let*.
This commit is contained in:
parent
79f124ac87
commit
b90b4b2bf2
1 changed files with 8 additions and 35 deletions
|
@ -33,15 +33,7 @@
|
||||||
#:autoload (ice-9 pretty-print) (pretty-print truncated-print)
|
#:autoload (ice-9 pretty-print) (pretty-print truncated-print)
|
||||||
#:replace (format))
|
#:replace (format))
|
||||||
|
|
||||||
;;; Configuration ------------------------------------------------------------
|
(define format:version "3.0")
|
||||||
|
|
||||||
(define format:ascii-non-printable-charnames
|
|
||||||
'#("nul" "soh" "stx" "etx" "eot" "enq" "ack" "bel"
|
|
||||||
"bs" "ht" "nl" "vt" "np" "cr" "so" "si"
|
|
||||||
"dle" "dc1" "dc2" "dc3" "dc4" "nak" "syn" "etb"
|
|
||||||
"can" "em" "sub" "esc" "fs" "gs" "rs" "us" "space"))
|
|
||||||
|
|
||||||
;;; End of configuration ----------------------------------------------------
|
|
||||||
|
|
||||||
(define (format destination format-string . format-args)
|
(define (format destination format-string . format-args)
|
||||||
(if (not (string? format-string))
|
(if (not (string? format-string))
|
||||||
|
@ -59,10 +51,11 @@
|
||||||
(current-error-port))
|
(current-error-port))
|
||||||
(else
|
(else
|
||||||
(error "format: bad destination `~a'" destination))))
|
(error "format: bad destination `~a'" destination))))
|
||||||
(output-col (or (port-column port) 0)))
|
|
||||||
|
|
||||||
(define format:version "3.0")
|
(output-col (or (port-column port) 0))
|
||||||
(define format:flush-output #f) ; flush output at end of formatting
|
|
||||||
|
(flush-output? #f))
|
||||||
|
|
||||||
(define format:case-conversion #f)
|
(define format:case-conversion #f)
|
||||||
(define format:pos 0) ; curr. format string parsing position
|
(define format:pos 0) ; curr. format string parsing position
|
||||||
(define format:arg-pos 0) ; curr. format argument position
|
(define format:arg-pos 0) ; curr. format argument position
|
||||||
|
@ -328,7 +321,7 @@
|
||||||
(format:error "~~c expects a character"))
|
(format:error "~~c expects a character"))
|
||||||
(case modifier
|
(case modifier
|
||||||
((at)
|
((at)
|
||||||
(format:out-str (format:char->str ch)))
|
(format:out-str (object->string ch)))
|
||||||
((colon)
|
((colon)
|
||||||
(let ((c (char->integer ch)))
|
(let ((c (char->integer ch)))
|
||||||
(if (< c 0)
|
(if (< c 0)
|
||||||
|
@ -446,7 +439,7 @@
|
||||||
(format:format-work frmt args))))
|
(format:format-work frmt args))))
|
||||||
(anychar-dispatch))
|
(anychar-dispatch))
|
||||||
((#\!) ; Flush output
|
((#\!) ; Flush output
|
||||||
(set! format:flush-output #t)
|
(set! flush-output? #t)
|
||||||
(anychar-dispatch))
|
(anychar-dispatch))
|
||||||
((#\newline) ; Continuation lines
|
((#\newline) ; Continuation lines
|
||||||
(if (eq? modifier 'at)
|
(if (eq? modifier 'at)
|
||||||
|
@ -769,26 +762,6 @@
|
||||||
(object->string res)
|
(object->string res)
|
||||||
res)))
|
res)))
|
||||||
|
|
||||||
;; format:char->str converts a character into a slashified string as
|
|
||||||
;; done by `write'. The procedure is dependent on the integer
|
|
||||||
;; representation of characters and assumes a character number according to
|
|
||||||
;; the ASCII character set.
|
|
||||||
|
|
||||||
(define (format:char->str ch)
|
|
||||||
(let ((int-rep (char->integer ch)))
|
|
||||||
(if (< int-rep 0) ; if chars are [-128...+127]
|
|
||||||
(set! int-rep (+ int-rep 256)))
|
|
||||||
(string-append
|
|
||||||
"#\\"
|
|
||||||
(cond
|
|
||||||
((char=? ch #\newline) "newline")
|
|
||||||
((and (>= int-rep 0) (<= int-rep 32))
|
|
||||||
(vector-ref format:ascii-non-printable-charnames int-rep))
|
|
||||||
((= int-rep 127) "del")
|
|
||||||
((>= int-rep 128) ; octal representation
|
|
||||||
(number->string int-rep 8))
|
|
||||||
(else (string ch))))))
|
|
||||||
|
|
||||||
(define format:space-ch (char->integer #\space))
|
(define format:space-ch (char->integer #\space))
|
||||||
(define format:zero-ch (char->integer #\0))
|
(define format:zero-ch (char->integer #\0))
|
||||||
|
|
||||||
|
@ -1599,7 +1572,7 @@
|
||||||
(display format:arg-pos)
|
(display format:arg-pos)
|
||||||
(format:error "~a missing argument~:p" (- arg-pos arg-len)))
|
(format:error "~a missing argument~:p" (- arg-pos arg-len)))
|
||||||
(else
|
(else
|
||||||
(if format:flush-output
|
(if flush-output?
|
||||||
(force-output port))
|
(force-output port))
|
||||||
(if destination
|
(if destination
|
||||||
#t
|
#t
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue