| 1 |
;; srfi.29.format - SRFI-29 compatible 'format'. |
| 2 |
|
| 3 |
;; Taken from SRFI-29 reference implementation. |
| 4 |
;; |
| 5 |
;; (c) 2002 Scott G. Miller. |
| 6 |
;; |
| 7 |
;; Permission is hereby granted, free of charge, to any person obtaining |
| 8 |
;; a copy of this software and associated documentation files (the |
| 9 |
;; "Software"), to deal in the Software without restriction, including |
| 10 |
;; without limitation the rights to use, copy, modify, merge, publish, |
| 11 |
;; distribute, sublicense, and/or sell copies of the Software, and to |
| 12 |
;; permit persons to whom the Software is furnished to do so, subject to |
| 13 |
;; the following conditions: |
| 14 |
;; |
| 15 |
;; The above copyright notice and this permission notice (including the |
| 16 |
;; next paragraph) shall be included in all copies or substantial |
| 17 |
;; portions of the Software. |
| 18 |
;; |
| 19 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 |
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 |
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 |
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 23 |
;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 24 |
;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 25 |
;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 |
|
| 27 |
(define-module srfi.29.format |
| 28 |
(export format)) |
| 29 |
(select-module srfi.29.format) |
| 30 |
|
| 31 |
(define (format format-string . objects) |
| 32 |
(let ((buffer (open-output-string))) |
| 33 |
(let loop ((format-list (string->list format-string)) |
| 34 |
(objects objects) |
| 35 |
(object-override #f)) |
| 36 |
(cond ((null? format-list) (get-output-string buffer)) |
| 37 |
((char=? (car format-list) #\~) |
| 38 |
(cond ((null? (cdr format-list)) |
| 39 |
(error 'format "Incomplete escape sequence")) |
| 40 |
((char-numeric? (cadr format-list)) |
| 41 |
(let posloop ((fl (cddr format-list)) |
| 42 |
(pos (string->number |
| 43 |
(string (cadr format-list))))) |
| 44 |
(cond ((null? fl) |
| 45 |
(error 'format "Incomplete escape sequence")) |
| 46 |
((and (eq? (car fl) '#\@) |
| 47 |
(null? (cdr fl))) |
| 48 |
(error 'format "Incomplete escape sequence")) |
| 49 |
((and (eq? (car fl) '#\@) |
| 50 |
(eq? (cadr fl) '#\*)) |
| 51 |
(loop (cddr fl) objects (list-ref objects pos))) |
| 52 |
(else |
| 53 |
(posloop (cdr fl) |
| 54 |
(+ (* 10 pos) |
| 55 |
(string->number |
| 56 |
(string (car fl))))))))) |
| 57 |
(else |
| 58 |
(case (cadr format-list) |
| 59 |
((#\a) |
| 60 |
(cond (object-override |
| 61 |
(begin |
| 62 |
(display object-override buffer) |
| 63 |
(loop (cddr format-list) objects #f))) |
| 64 |
((null? objects) |
| 65 |
(error 'format "No value for escape sequence")) |
| 66 |
(else |
| 67 |
(begin |
| 68 |
(display (car objects) buffer) |
| 69 |
(loop (cddr format-list) |
| 70 |
(cdr objects) #f))))) |
| 71 |
((#\s) |
| 72 |
(cond (object-override |
| 73 |
(begin |
| 74 |
(display object-override buffer) |
| 75 |
(loop (cddr format-list) objects #f))) |
| 76 |
((null? objects) |
| 77 |
(error 'format "No value for escape sequence")) |
| 78 |
(else |
| 79 |
(begin |
| 80 |
(write (car objects) buffer) |
| 81 |
(loop (cddr format-list) |
| 82 |
(cdr objects) #f))))) |
| 83 |
((#\%) |
| 84 |
(if object-override |
| 85 |
(error 'format "Escape sequence following positional override does not require a value")) |
| 86 |
(display #\newline buffer) |
| 87 |
(loop (cddr format-list) objects #f)) |
| 88 |
((#\~) |
| 89 |
(if object-override |
| 90 |
(error 'format "Escape sequence following positional override does not require a value")) |
| 91 |
(display #\~ buffer) |
| 92 |
(loop (cddr format-list) objects #f)) |
| 93 |
(else |
| 94 |
(error 'format "Unrecognized escape sequence")))))) |
| 95 |
(else (display (car format-list) buffer) |
| 96 |
(loop (cdr format-list) objects #f)))))) |