| 1 |
#!r6rs |
| 2 |
;; Copyright 2009 Derick Eddington. My MIT-style license is in the file named |
| 3 |
;; LICENSE from the original collection this file is distributed with. |
| 4 |
|
| 5 |
(library (srfi :6 basic-string-ports) |
| 6 |
(export |
| 7 |
(rename (open-string-input-port open-input-string)) |
| 8 |
open-output-string |
| 9 |
get-output-string) |
| 10 |
(import |
| 11 |
(rnrs) |
| 12 |
(only (scheme base) make-weak-hasheq hash-ref hash-set!)) |
| 13 |
|
| 14 |
(define accumed-ht (make-weak-hasheq)) |
| 15 |
|
| 16 |
(define (open-output-string) |
| 17 |
(letrec ((sop |
| 18 |
(make-custom-textual-output-port |
| 19 |
"string-output-port" |
| 20 |
(lambda (string start count) ; write! |
| 21 |
(when (positive? count) |
| 22 |
(let ((al (hash-ref accumed-ht sop))) |
| 23 |
(hash-set! accumed-ht sop |
| 24 |
(cons (substring string start (+ start count)) al)))) |
| 25 |
count) |
| 26 |
#F ; get-position TODO? |
| 27 |
#F ; set-position! TODO? |
| 28 |
#F #| closed TODO? |# ))) |
| 29 |
(hash-set! accumed-ht sop '()) |
| 30 |
sop)) |
| 31 |
|
| 32 |
(define (get-output-string sop) |
| 33 |
(if (output-port? sop) |
| 34 |
(cond ((hash-ref accumed-ht sop #F) |
| 35 |
=> (lambda (al) (apply string-append (reverse al)))) |
| 36 |
(else |
| 37 |
(assertion-violation 'get-output-string "not a string-output-port" sop))) |
| 38 |
(assertion-violation 'get-output-string "not an output-port" sop))) |
| 39 |
|
| 40 |
) |