| 1 |
#!r6rs |
| 2 |
;;; FILE "intermediate-format-strings.sls" |
| 3 |
;;; IMPLEMENTS SRFI-48: Intermediary format strings |
| 4 |
;;; http://srfi.schemers.org/srfi-48/srfi-48.html |
| 5 |
;;; AUTHOR Ken Dickey |
| 6 |
;;; UPDATED Syntax updated for R6RS February 2008 by Ken Dickey |
| 7 |
;;; LANGUAGE R6RS |
| 8 |
|
| 9 |
;; Small changes by Derick Eddington to the beginning of `format'. |
| 10 |
|
| 11 |
;;;Copyright (C) Kenneth A Dickey (2003). All Rights Reserved. |
| 12 |
;;; |
| 13 |
;;;Permission is hereby granted, free of charge, to any person |
| 14 |
;;;obtaining a copy of this software and associated documentation |
| 15 |
;;;files (the "Software"), to deal in the Software without |
| 16 |
;;;restriction, including without limitation the rights to use, |
| 17 |
;;;copy, modify, merge, publish, distribute, sublicense, and/or |
| 18 |
;;;sell copies of the Software, and to permit persons to whom |
| 19 |
;;;the Software is furnished to do so, subject to the following |
| 20 |
;;;conditions: |
| 21 |
;;; |
| 22 |
;;;The above copyright notice and this permission notice shall |
| 23 |
;;;be included in all copies or substantial portions of the Software. |
| 24 |
;;; |
| 25 |
;;;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 26 |
;;;EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 27 |
;;;OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 28 |
;;;NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 29 |
;;;HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 30 |
;;;WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 31 |
;;;FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 32 |
;;;OTHER DEALINGS IN THE SOFTWARE. |
| 33 |
|
| 34 |
; The implementation below requires SRFI-6 (Basic string ports), |
| 35 |
; and SRFI-38 (External Representation for Data With Shared Structure). |
| 36 |
|
| 37 |
(library (srfi :48 intermediate-format-strings) |
| 38 |
(export |
| 39 |
format) |
| 40 |
(import |
| 41 |
(rnrs) |
| 42 |
(srfi :48 intermediate-format-strings compat) |
| 43 |
(srfi :6 basic-string-ports) |
| 44 |
(srfi :38 with-shared-structure)) |
| 45 |
|
| 46 |
(define ascii-tab #\tab) |
| 47 |
|
| 48 |
(define (format arg0 . arg*) |
| 49 |
|
| 50 |
(define (problem msg . irts) |
| 51 |
(apply assertion-violation 'format msg irts)) |
| 52 |
|
| 53 |
(define (_format port format-string args return-value) |
| 54 |
|
| 55 |
(define (string-index str c) |
| 56 |
(let ( [len (string-length str)] ) |
| 57 |
(let loop ( [i 0] ) |
| 58 |
(cond ((= i len) #f) |
| 59 |
((eqv? c (string-ref str i)) i) |
| 60 |
(else (loop (+ i 1))))))) |
| 61 |
|
| 62 |
(define (string-grow str len char) |
| 63 |
(let ( [off (- len (string-length str))] ) |
| 64 |
(if (positive? off) |
| 65 |
(string-append (make-string off char) str) |
| 66 |
str))) |
| 67 |
|
| 68 |
(define (compose-with-digits digits pre-str frac-str exp-str) |
| 69 |
(let ( [frac-len (string-length frac-str)] ) |
| 70 |
;;@@DEBUG |
| 71 |
;;(format #t "~%@@(compose-with-digits digits=~s pre-str=~s frac-str=~s exp-str=~s ) ~%" digits pre-str frac-str exp-str) |
| 72 |
(cond |
| 73 |
[(< frac-len digits) ;; grow frac part, pad with zeros |
| 74 |
(string-append pre-str "." |
| 75 |
frac-str (make-string (- digits frac-len) #\0) |
| 76 |
exp-str) |
| 77 |
] |
| 78 |
[(= frac-len digits) ;; frac-part is exactly the right size |
| 79 |
(string-append pre-str "." |
| 80 |
frac-str |
| 81 |
exp-str) |
| 82 |
] |
| 83 |
[else ;; must round to shrink frac-part |
| 84 |
(let* ( [first-part (substring frac-str 0 digits)] |
| 85 |
[last-part (substring frac-str digits frac-len)] |
| 86 |
;; NB: Scheme uses "Round to Even Rule" for .5 |
| 87 |
[rounded-frac |
| 88 |
;; NB: exact is r6; r5 is inexact->exact |
| 89 |
(exact (round (string->number |
| 90 |
(string-append first-part "." last-part)))) |
| 91 |
] |
| 92 |
[rounded-frac-str (number->string rounded-frac)] |
| 93 |
[rounded-frac-len (string-length rounded-frac-str)] |
| 94 |
[carry? (and (not (zero? rounded-frac)) |
| 95 |
(> rounded-frac-len digits)) |
| 96 |
] |
| 97 |
[new-frac |
| 98 |
(let ( (pre-frac |
| 99 |
(if carry? ;; trim leading "1" |
| 100 |
(substring rounded-frac-str 1 (min rounded-frac-len digits)) |
| 101 |
(substring rounded-frac-str 0 (min rounded-frac-len digits))) ;; may be zero length |
| 102 |
) |
| 103 |
) |
| 104 |
(if (< (string-length pre-frac) digits) |
| 105 |
(string-grow pre-frac digits #\0) |
| 106 |
pre-frac)) |
| 107 |
] |
| 108 |
) |
| 109 |
;;@@DEBUG |
| 110 |
;;(format #t "@@ first-part=~s last-part=~s rounded-frac=~s carry?=~s ~%" first-part last-part rounded-frac carry?) |
| 111 |
(string-append |
| 112 |
(if carry? (number->string (+ 1 (string->number pre-str))) pre-str) |
| 113 |
"." |
| 114 |
new-frac |
| 115 |
exp-str))] |
| 116 |
) ) ) |
| 117 |
|
| 118 |
|
| 119 |
(define (format-fixed number-or-string width digits) ; returns a string |
| 120 |
;;@@DEBUG |
| 121 |
;;(format #t "~%(format-fixed number-or-string=~s width=~s digits=~s)~%" number-or-string width digits) |
| 122 |
|
| 123 |
(cond |
| 124 |
[(string? number-or-string) |
| 125 |
(string-grow number-or-string width #\space) |
| 126 |
] |
| 127 |
[(number? number-or-string) |
| 128 |
(let* ( [num (real-part number-or-string)] |
| 129 |
[real (if digits (+ 0.0 num) num)] |
| 130 |
[imag (imag-part number-or-string)] |
| 131 |
) |
| 132 |
(cond |
| 133 |
[(not (zero? imag)) |
| 134 |
(string-grow |
| 135 |
(string-append (format-fixed real 0 digits) |
| 136 |
(if (negative? imag) "" "+") |
| 137 |
(format-fixed imag 0 digits) |
| 138 |
"i") |
| 139 |
width |
| 140 |
#\space) |
| 141 |
] |
| 142 |
[digits |
| 143 |
(let* ( [num-str (number->string (if (rational? real) |
| 144 |
(+ 0.0 real) |
| 145 |
real))] |
| 146 |
[dot-index (string-index num-str #\.)] |
| 147 |
[exp-index (string-index num-str #\e)] |
| 148 |
[length (string-length num-str)] |
| 149 |
[pre-string |
| 150 |
(cond |
| 151 |
((and exp-index (not dot-index)) |
| 152 |
(substring num-str 0 exp-index) |
| 153 |
) |
| 154 |
(dot-index |
| 155 |
(substring num-str 0 dot-index) |
| 156 |
) |
| 157 |
(else |
| 158 |
num-str)) |
| 159 |
] |
| 160 |
[exp-string |
| 161 |
(if exp-index (substring num-str exp-index length) "") |
| 162 |
] |
| 163 |
[frac-string |
| 164 |
(let ( (dot-idx (if dot-index dot-index -1)) ) |
| 165 |
(if exp-index |
| 166 |
(substring num-str (+ dot-idx 1) exp-index) |
| 167 |
(substring num-str (+ dot-idx 1) length))) |
| 168 |
] |
| 169 |
) |
| 170 |
(string-grow |
| 171 |
(if dot-index |
| 172 |
(compose-with-digits digits |
| 173 |
pre-string |
| 174 |
frac-string |
| 175 |
exp-string) |
| 176 |
(string-append pre-string exp-string)) |
| 177 |
width |
| 178 |
#\space) |
| 179 |
)] |
| 180 |
[else ;; no digits |
| 181 |
(string-grow (number->string real) width #\space)]) |
| 182 |
)] |
| 183 |
[else |
| 184 |
(error 'format "~F requires a number or a string" number-or-string)]) |
| 185 |
) |
| 186 |
|
| 187 |
|
| 188 |
(define documentation-string |
| 189 |
"(format [<port>] <format-string> [<arg>...]) -- <port> is #t, #f or an output-port |
| 190 |
OPTION [MNEMONIC] DESCRIPTION -- Implementation Assumes ASCII Text Encoding |
| 191 |
~H [Help] output this text |
| 192 |
~A [Any] (display arg) for humans |
| 193 |
~S [Slashified] (write arg) for parsers |
| 194 |
~W [WriteCircular] like ~s but outputs circular and recursive data structures |
| 195 |
~~ [tilde] output a tilde |
| 196 |
~T [Tab] output a tab character |
| 197 |
~% [Newline] output a newline character |
| 198 |
~& [Freshline] output a newline character if the previous output was not a newline |
| 199 |
~D [Decimal] the arg is a number which is output in decimal radix |
| 200 |
~X [heXadecimal] the arg is a number which is output in hexdecimal radix |
| 201 |
~O [Octal] the arg is a number which is output in octal radix |
| 202 |
~B [Binary] the arg is a number which is output in binary radix |
| 203 |
~w,dF [Fixed] the arg is a string or number which has width w and d digits after the decimal |
| 204 |
~C [Character] charater arg is output by write-char |
| 205 |
~_ [Space] a single space character is output |
| 206 |
~Y [Yuppify] the list arg is pretty-printed to the output |
| 207 |
~? [Indirection] recursive format: next 2 args are format-string and list of arguments |
| 208 |
~K [Indirection] same as ~? |
| 209 |
" |
| 210 |
) |
| 211 |
|
| 212 |
(define (require-an-arg args) |
| 213 |
(when (null? args) |
| 214 |
(problem "too few arguments")) |
| 215 |
) |
| 216 |
|
| 217 |
(define (format-help p format-strg arglist) |
| 218 |
|
| 219 |
(letrec ( |
| 220 |
[length-of-format-string (string-length format-strg)] |
| 221 |
|
| 222 |
[anychar-dispatch |
| 223 |
(lambda (pos arglist last-was-newline) |
| 224 |
(if (>= pos length-of-format-string) |
| 225 |
arglist ; return unused args |
| 226 |
(let ( [char (string-ref format-strg pos)] ) |
| 227 |
(cond |
| 228 |
[(eqv? char #\~) |
| 229 |
(tilde-dispatch (+ pos 1) arglist last-was-newline)] |
| 230 |
[else |
| 231 |
(write-char char p) |
| 232 |
(anychar-dispatch (+ pos 1) arglist #f) |
| 233 |
]) |
| 234 |
))) |
| 235 |
] ; end anychar-dispatch |
| 236 |
|
| 237 |
[has-newline? |
| 238 |
(lambda (whatever last-was-newline) |
| 239 |
(or (eqv? whatever #\newline) |
| 240 |
(and (string? whatever) |
| 241 |
(let ( [len (string-length whatever)] ) |
| 242 |
(if (zero? len) |
| 243 |
last-was-newline |
| 244 |
(eqv? #\newline (string-ref whatever (- len 1))))))) |
| 245 |
)] ; end has-newline? |
| 246 |
|
| 247 |
[tilde-dispatch |
| 248 |
(lambda (pos arglist last-was-newline) |
| 249 |
(cond |
| 250 |
((>= pos length-of-format-string) |
| 251 |
(write-char #\~ p) ; tilde at end of string is just output |
| 252 |
arglist ; return unused args |
| 253 |
) |
| 254 |
(else |
| 255 |
(case (char-upcase (string-ref format-strg pos)) |
| 256 |
((#\A) ; Any -- for humans |
| 257 |
(require-an-arg arglist) |
| 258 |
(let ( [whatever (car arglist)] ) |
| 259 |
(display whatever p) |
| 260 |
(anychar-dispatch (+ pos 1) |
| 261 |
(cdr arglist) |
| 262 |
(has-newline? whatever last-was-newline)) |
| 263 |
)) |
| 264 |
((#\S) ; Slashified -- for parsers |
| 265 |
(require-an-arg arglist) |
| 266 |
(let ( [whatever (car arglist)] ) |
| 267 |
(write whatever p) |
| 268 |
(anychar-dispatch (+ pos 1) |
| 269 |
(cdr arglist) |
| 270 |
(has-newline? whatever last-was-newline)) |
| 271 |
)) |
| 272 |
((#\W) |
| 273 |
(require-an-arg arglist) |
| 274 |
(let ( [whatever (car arglist)] ) |
| 275 |
(write-with-shared-structure whatever p) ;; srfi-38 |
| 276 |
(anychar-dispatch (+ pos 1) |
| 277 |
(cdr arglist) |
| 278 |
(has-newline? whatever last-was-newline)) |
| 279 |
)) |
| 280 |
((#\D) ; Decimal |
| 281 |
(require-an-arg arglist) |
| 282 |
(display (number->string (car arglist) 10) p) |
| 283 |
(anychar-dispatch (+ pos 1) (cdr arglist) #f) |
| 284 |
) |
| 285 |
((#\X) ; HeXadecimal |
| 286 |
(require-an-arg arglist) |
| 287 |
(display (number->string (car arglist) 16) p) |
| 288 |
(anychar-dispatch (+ pos 1) (cdr arglist) #f) |
| 289 |
) |
| 290 |
((#\O) ; Octal |
| 291 |
(require-an-arg arglist) |
| 292 |
(display (number->string (car arglist) 8) p) |
| 293 |
(anychar-dispatch (+ pos 1) (cdr arglist) #f) |
| 294 |
) |
| 295 |
((#\B) ; Binary |
| 296 |
(require-an-arg arglist) |
| 297 |
(display (number->string (car arglist) 2) p) |
| 298 |
(anychar-dispatch (+ pos 1) (cdr arglist) #f) |
| 299 |
) |
| 300 |
((#\C) ; Character |
| 301 |
(require-an-arg arglist) |
| 302 |
(write-char (car arglist) p) |
| 303 |
(anychar-dispatch (+ pos 1) |
| 304 |
(cdr arglist) |
| 305 |
(eqv? (car arglist) #\newline)) |
| 306 |
) |
| 307 |
((#\~) ; Tilde |
| 308 |
(write-char #\~ p) |
| 309 |
(anychar-dispatch (+ pos 1) arglist #f) |
| 310 |
) |
| 311 |
((#\%) ; Newline |
| 312 |
(newline p) |
| 313 |
(anychar-dispatch (+ pos 1) arglist #t) |
| 314 |
) |
| 315 |
((#\&) ; Freshline |
| 316 |
(if (not last-was-newline) ;; (unless last-was-newline .. |
| 317 |
(newline p)) |
| 318 |
(anychar-dispatch (+ pos 1) arglist #t) |
| 319 |
) |
| 320 |
((#\_) ; Space |
| 321 |
(write-char #\space p) |
| 322 |
(anychar-dispatch (+ pos 1) arglist #f) |
| 323 |
) |
| 324 |
((#\T) ; Tab -- IMPLEMENTATION DEPENDENT ENCODING |
| 325 |
(write-char ascii-tab p) |
| 326 |
(anychar-dispatch (+ pos 1) arglist #f) |
| 327 |
) |
| 328 |
((#\Y) ; Pretty-print |
| 329 |
(pretty-print (car arglist) p) |
| 330 |
(anychar-dispatch (+ pos 1) (cdr arglist) #f) |
| 331 |
) |
| 332 |
((#\F) |
| 333 |
(require-an-arg arglist) |
| 334 |
(display (format-fixed (car arglist) 0 #f) p) |
| 335 |
(anychar-dispatch (+ pos 1) (cdr arglist) #f) |
| 336 |
) |
| 337 |
((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9) |
| 338 |
;; gather "~w[,d]F" w and d digits |
| 339 |
(let loop ( [index (+ pos 1)] |
| 340 |
[w-digits (list (string-ref format-strg pos))] |
| 341 |
[d-digits '()] |
| 342 |
[in-width? #t] |
| 343 |
) |
| 344 |
(if (>= index length-of-format-string) |
| 345 |
(problem "improper numeric format directive" format-strg) |
| 346 |
(let ( [next-char (string-ref format-strg index)] ) |
| 347 |
(cond |
| 348 |
[(char-numeric? next-char) |
| 349 |
(if in-width? |
| 350 |
(loop (+ index 1) |
| 351 |
(cons next-char w-digits) |
| 352 |
d-digits |
| 353 |
in-width?) |
| 354 |
(loop (+ index 1) |
| 355 |
w-digits |
| 356 |
(cons next-char d-digits) |
| 357 |
in-width?)) |
| 358 |
] |
| 359 |
[(char=? (char-upcase next-char) #\F) |
| 360 |
(let ( [width |
| 361 |
(string->number |
| 362 |
(list->string |
| 363 |
(reverse w-digits))) |
| 364 |
] |
| 365 |
[digits |
| 366 |
(if (zero? (length d-digits)) |
| 367 |
#f |
| 368 |
(string->number |
| 369 |
(list->string (reverse d-digits))))] |
| 370 |
) |
| 371 |
(display |
| 372 |
(format-fixed (car arglist) width digits) |
| 373 |
p) |
| 374 |
(anychar-dispatch (+ index 1) (cdr arglist) #f)) |
| 375 |
] |
| 376 |
[(char=? next-char #\,) |
| 377 |
(if in-width? |
| 378 |
(loop (+ index 1) |
| 379 |
w-digits |
| 380 |
d-digits |
| 381 |
#f) |
| 382 |
(problem "too many commas in directive" format-strg)) |
| 383 |
] |
| 384 |
[else |
| 385 |
(problem "~w,dF directive ill-formed" format-strg)]))) |
| 386 |
)) |
| 387 |
((#\? #\K) ; indirection -- take next arg as format string |
| 388 |
(cond ; and following arg as list of format args |
| 389 |
((< (length arglist) 2) |
| 390 |
(problem "less arguments than specified for ~?" arglist) |
| 391 |
) |
| 392 |
((not (string? (car arglist))) |
| 393 |
(problem "~? requires a string" (car arglist)) |
| 394 |
) |
| 395 |
(else |
| 396 |
(format-help p (car arglist) (cadr arglist)) |
| 397 |
(anychar-dispatch (+ pos 1) (cddr arglist) #f) |
| 398 |
))) |
| 399 |
((#\H) ; Help |
| 400 |
(display documentation-string p) |
| 401 |
(anychar-dispatch (+ pos 1) arglist #t) |
| 402 |
) |
| 403 |
(else |
| 404 |
(problem "unknown tilde escape" (string-ref format-strg pos))) |
| 405 |
))) |
| 406 |
)] ; end tilde-dispatch |
| 407 |
) ; end letrec |
| 408 |
|
| 409 |
; format-help body |
| 410 |
(anychar-dispatch 0 arglist #f) |
| 411 |
)) ; end format-help |
| 412 |
|
| 413 |
; _format body |
| 414 |
(let ( [unused-args (format-help port format-string args)] ) |
| 415 |
(if (not (null? unused-args)) |
| 416 |
(problem "unused arguments" unused-args) |
| 417 |
(return-value port)))) |
| 418 |
|
| 419 |
; format body |
| 420 |
(if (string? arg0) |
| 421 |
(_format (open-output-string) arg0 arg* get-output-string) |
| 422 |
(if (null? arg*) |
| 423 |
(problem "too few arguments" (list arg0)) |
| 424 |
(let ([port (cond [(eq? arg0 #f) (open-output-string)] |
| 425 |
[(eq? arg0 #t) (current-output-port)] |
| 426 |
[(output-port? arg0) arg0] |
| 427 |
[else (problem "bad output-port argument" arg0)])] |
| 428 |
[arg1 (car arg*)]) |
| 429 |
(if (string? arg1) |
| 430 |
(_format port arg1 (cdr arg*) (if arg0 (lambda (ignore) (values)) get-output-string)) |
| 431 |
(problem "not a string" arg1)))))) |
| 432 |
) |