chicken-hackers
[Top][All Lists]
Advanced

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

[Chicken-hackers] [PATCH 2/4] Use "internally-namespaced" aliases for re


From: Evan Hanson
Subject: [Chicken-hackers] [PATCH 2/4] Use "internally-namespaced" aliases for read-string[!]/port procedures
Date: Sat, 13 May 2017 19:55:46 +1200

Replace `##sys#read-string/port' and `##sys#read-string!/port', both of
which are defined in extras.scm but also used in srfi-4.scm, with
module-namespaced (but still internal) names.

Also, reshuffle their argument checks so that the internal procedures
trust their argument types and all the "##sys#check"-ing happens in the
user-facing variants. This gives slightly better error locations when
bad values are passed to `read-u8vector'.
---
 c-platform.scm |  2 +-
 extras.scm     | 26 ++++++++++++++++----------
 srfi-4.scm     |  6 ++++--
 3 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/c-platform.scm b/c-platform.scm
index 49bbfc06..c34dc4fb 100644
--- a/c-platform.scm
+++ b/c-platform.scm
@@ -1040,7 +1040,7 @@
 
 (rewrite 'read-char 23 0 '##sys#read-char/port '##sys#standard-input)
 (rewrite 'write-char 23 1 '##sys#write-char/port '##sys#standard-output)
-(rewrite 'chicken.io#read-string 23 1 '##sys#read-string/port 
'##sys#standard-input)
+(rewrite 'chicken.io#read-string 23 1 'chicken.io#read-string/port 
'##sys#standard-input)
 (rewrite 'chicken.data-structures#substring=? 23 2 '##sys#substring=? 0 0 #f)
 (rewrite 'chicken.data-structures#substring-ci=? 23 2 '##sys#substring-ci=? 0 
0 #f)
 (rewrite 'chicken.data-structures#substring-index 23 2 '##sys#substring-index 
0)
diff --git a/extras.scm b/extras.scm
index a49b96d2..aa23e839 100644
--- a/extras.scm
+++ b/extras.scm
@@ -114,7 +114,7 @@
 
 ;;; Extended I/O 
 
-(define (##sys#read-string! n dest port start)
+(define (read-string!/port n dest port start)
   (cond ((eq? n 0) 0)
        (else
         (let ((rdstring (##sys#slot (##sys#slot port 2) 7)))
@@ -147,18 +147,16 @@
     (unless (and n (fx<= (fx+ start n) dest-size))
       (set! n (fx- dest-size start))))
   (##sys#check-fixnum start 'read-string!)
-  (##sys#read-string! n dest port start) )
+  (read-string!/port n dest port start))
 
 (define-constant read-string-buffer-size 2048)
 
-(define ##sys#read-string/port
+(define read-string/port
   (lambda (n p)
-    (##sys#check-input-port p #t 'read-string)
     (cond ((eof-object? (##sys#peek-char-0 p))
           (if (eq? n 0) "" #!eof))
-          (n (##sys#check-fixnum n 'read-string)
-            (let* ((str (##sys#make-string n))
-                   (n2 (##sys#read-string! n str p 0)) )
+          (n (let* ((str (##sys#make-string n))
+                   (n2 (read-string!/port n str p 0)))
               (if (eq? n n2)
                   str
                   (##sys#substring str 0 n2))))
@@ -167,8 +165,7 @@
                 (buf (make-string read-string-buffer-size)))
             (let loop ()
               (let ((c (peek-char p))
-                    (n (##sys#read-string! read-string-buffer-size
-                                           buf p 0)))
+                    (n (read-string!/port read-string-buffer-size buf p 0)))
                 (cond ((eq? n 0)
                        (get-output-string out))
                       (else
@@ -176,7 +173,16 @@
                        (loop))))))))))
 
 (define (read-string #!optional n (port ##sys#standard-input))
-  (##sys#read-string/port n port) )
+  (##sys#check-input-port port #t 'read-string)
+  (when n (##sys#check-fixnum n 'read-string))
+  (read-string/port n port))
+
+
+;; Make internal reader procedures available for use in srfi-4.scm:
+
+(define chicken.io#read-string/port read-string/port)
+(define chicken.io#read-string!/port read-string!/port)
+
 
 ;; <procedure>(read-buffered [PORT])</procedure>
 ;;
diff --git a/srfi-4.scm b/srfi-4.scm
index 22266c22..bd6d727d 100644
--- a/srfi-4.scm
+++ b/srfi-4.scm
@@ -806,10 +806,12 @@ EOF
         (size (##sys#size dest)))
     (unless (and n (fx<= (fx+ start n) size))
       (set! n (fx- size start)))
-    (##sys#read-string! n dest port start)))
+    (chicken.io#read-string!/port n dest port start)))
 
 (define (read-u8vector #!optional n (p ##sys#standard-input))
-  (let ((str (##sys#read-string/port n p)))
+  (##sys#check-input-port p #t 'read-u8vector)
+  (when n (##sys#check-fixnum n 'read-u8vector))
+  (let ((str (chicken.io#read-string/port n p)))
     (cond ((eof-object? str) str)
          (else
           (##core#inline "C_string_to_bytevector" str)
-- 
2.11.0




reply via email to

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