(string-any, string-every): Use a scheme
authorKevin Ryde <user42@zip.com.au>
Tue, 14 Dec 2004 00:21:25 +0000 (00:21 +0000)
committerKevin Ryde <user42@zip.com.au>
Tue, 14 Dec 2004 00:21:25 +0000 (00:21 +0000)
wrapper around the C code so for the final call to the predicate
procedure is a tail call, per SRFI-13 spec.

ice-9/boot-9.scm
libguile/srfi-13.c

index defab8b..7756b4c 100644 (file)
 
 (define format simple-format)
 
+;; this is scheme wrapping the C code so the final pred call is a tail call,
+;; per SRFI-13 spec
+(define (string-any char_pred s . rest)
+  (let ((start (if (null? rest)
+                  0 (car rest)))
+       (end   (if (or (null? rest) (null? (cdr rest)))
+                  (string-length s) (cadr rest))))
+    (if (and (procedure? char_pred)
+            (> end start)
+            (<= end (string-length s))) ;; let c-code handle range error
+       (or (string-any-c-code char_pred s start (1- end))
+           (char_pred (string-ref s (1- end))))
+       (string-any-c-code char_pred s start end))))
+
+;; this is scheme wrapping the C code so the final pred call is a tail call,
+;; per SRFI-13 spec
+(define (string-every char_pred s . rest)
+  (let ((start (if (null? rest)
+                  0 (car rest)))
+       (end   (if (or (null? rest) (null? (cdr rest)))
+                  (string-length s) (cadr rest))))
+    (if (and (procedure? char_pred)
+            (> end start)
+            (<= end (string-length s))) ;; let c-code handle range error
+       (and (string-every-c-code char_pred s start (1- end))
+            (char_pred (string-ref s (1- end))))
+       (string-every-c-code char_pred s start end))))
+
 \f
 
 ;;; {EVAL-CASE}
index 169bbc9..c002790 100644 (file)
@@ -75,7 +75,7 @@ race_error ()
 }
 #endif
 
-SCM_DEFINE (scm_string_any, "string-any", 2, 2, 0,
+SCM_DEFINE (scm_string_any, "string-any-c-code", 2, 2, 0,
             (SCM char_pred, SCM s, SCM start, SCM end),
            "Check if the predicate @var{pred} is true for any character in\n"
            "the string @var{s}.\n"
@@ -134,7 +134,7 @@ SCM_DEFINE (scm_string_any, "string-any", 2, 2, 0,
 #undef FUNC_NAME
 
 
-SCM_DEFINE (scm_string_every, "string-every", 2, 2, 0,
+SCM_DEFINE (scm_string_every, "string-every-c-code", 2, 2, 0,
             (SCM char_pred, SCM s, SCM start, SCM end),
            "Check if the predicate @var{pred} is true for every character\n"
            "in the string @var{s}.\n"