Debian Codegraph @

%3a28/basic-format-strings.sls

raw
1 (library (srfi :28 basic-format-strings)
2 (export format)
3 (import (rnrs) (srfi :6))
4
5 ;; reference implementation from srfi :28 documentation
6
7 ;; Copyright (C) Scott G. Miller (2002). All Rights Reserved.
8 ;;
9 ;; Permission is hereby granted, free of charge, to any person obtaining a
10 ;; copy of this software and associated documentation files (the "Software"),
11 ;; to deal in the Software without restriction, including without limitation
12 ;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 ;; and/or sell copies of the Software, and to permit persons to whom the
14 ;; Software is furnished to do so, subject to the following conditions:
15 ;;
16 ;; The above copyright notice and this permission notice shall be included in
17 ;; all copies or substantial portions of the Software.
18 ;;
19 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 ;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 ;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 ;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 ;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 ;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 ;; DEALINGS IN THE SOFTWARE.
26 ;;
27 (define format
28 (lambda (format-string . objects)
29 (let ((buffer (open-output-string)))
30 (let loop ((format-list (string->list format-string))
31 (objects objects))
32 (cond ((null? format-list) (get-output-string buffer))
33 ((char=? (car format-list) #\~)
34 (if (null? (cdr format-list))
35 (error 'format "Incomplete escape sequence")
36 (case (cadr format-list)
37 ((#\a)
38 (if (null? objects)
39 (error 'format "No value for escape sequence")
40 (begin
41 (display (car objects) buffer)
42 (loop (cddr format-list) (cdr objects)))))
43 ((#\s)
44 (if (null? objects)
45 (error 'format "No value for escape sequence")
46 (begin
47 (write (car objects) buffer)
48 (loop (cddr format-list) (cdr objects)))))
49 ((#\%)
50 (newline buffer)
51 (loop (cddr format-list) objects))
52 ((#\~)
53 (write-char #\~ buffer)
54 (loop (cddr format-list) objects))
55 (else
56 (error 'format "Unrecognized escape sequence")))))
57 (else (write-char (car format-list) buffer)
58 (loop (cdr format-list) objects))))))))