guile-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Guile-commits] 09/09: put-string in Scheme


From: Andy Wingo
Subject: [Guile-commits] 09/09: put-string in Scheme
Date: Wed, 1 Jun 2016 10:11:30 +0000 (UTC)

wingo pushed a commit to branch master
in repository guile.

commit 9454068a54a4e9e84db61dc42d9a5d7a544a7ece
Author: Andy Wingo <address@hidden>
Date:   Wed Jun 1 12:01:16 2016 +0200

    put-string in Scheme
    
    * module/ice-9/ports.scm: Export port-encode-chars and
      port-clear-stream-start-for-bom-write via the internals module.
    * module/ice-9/sports.scm (put-string): New function.
---
 module/ice-9/ports.scm  |    4 ++++
 module/ice-9/sports.scm |   22 ++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/module/ice-9/ports.scm b/module/ice-9/ports.scm
index 3fc2f64..68afed1 100644
--- a/module/ice-9/ports.scm
+++ b/module/ice-9/ports.scm
@@ -182,10 +182,12 @@ interpret its input and output."
             port-read
             port-write
             port-clear-stream-start-for-bom-read
+            port-clear-stream-start-for-bom-write
             %port-encoding
             specialize-port-encoding!
             port-random-access?
             port-decode-char
+            port-encode-chars
             port-read-buffering
             port-poll
             port-read-wait-fd
@@ -227,9 +229,11 @@ interpret its input and output."
                        port-read
                        port-write
                        port-clear-stream-start-for-bom-read
+                       port-clear-stream-start-for-bom-write
                        %port-encoding
                        specialize-port-encoding!
                        port-decode-char
+                       port-encode-chars
                        port-random-access?
                        port-read-buffering
                        port-poll
diff --git a/module/ice-9/sports.scm b/module/ice-9/sports.scm
index 86d9a5b..d145d07 100644
--- a/module/ice-9/sports.scm
+++ b/module/ice-9/sports.scm
@@ -64,6 +64,7 @@
             get-bytevector-n
             put-u8
             put-bytevector
+            put-string
 
             %read-line
             read-line
@@ -653,6 +654,27 @@
 (define* (%read-line port)
   (read-line port 'split))
 
+(define* (put-string port str #:optional (start 0)
+                     (count (- (string-length str) start)))
+  (let* ((aux (port-auxiliary-write-buffer port))
+         (pos (port-buffer-position aux))
+         (line (port-position-line pos)))
+    (set-port-buffer-cur! aux 0)
+    (port-clear-stream-start-for-bom-write port aux)
+    (let lp ((encoded 0))
+      (when (< encoded count)
+        (let ((encoded (+ encoded
+                          (port-encode-chars port aux str
+                                             (+ start encoded)
+                                             (- count encoded)))))
+          (let ((end (port-buffer-end aux)))
+            (set-port-buffer-end! aux 0)
+            (put-bytevector port (port-buffer-bytevector aux) 0 end)
+            (lp encoded)))))
+    (when (and (not (eqv? line (port-position-line pos)))
+               (port-line-buffered? port))
+      (flush-output port))))
+
 (define saved-port-bindings #f)
 (define port-bindings
   '(((guile) read-char peek-char force-output close-port)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]