Change string-search-X interface back to its original design.
authorChris Hanson <org/chris-hanson/cph>
Sat, 4 Mar 2017 04:30:13 +0000 (20:30 -0800)
committerChris Hanson <org/chris-hanson/cph>
Sat, 4 Mar 2017 04:30:13 +0000 (20:30 -0800)
src/runtime/runtime.pkg
src/runtime/ustring.scm

index d98ccbeb1213ed865ed86a9f00c3610cd0d6e342..e8b584ada59b1464f61023a9afae6ef51c5fcff5 100644 (file)
@@ -937,6 +937,9 @@ USA.
          (substring->list string->list)
          (substring-move-left! substring-move!)
          (substring-move-right! substring-move!)
+         (substring-search-all string-search-all)
+         (substring-search-backward string-search-backward)
+         (substring-search-forward string-search-forward)
          string-move!
          substring-ci<?
          substring-ci=?
@@ -951,17 +954,12 @@ USA.
          substring-move!
          substring-prefix-ci?
          substring-prefix?
-         substring-search-all
-         substring-search-backward
-         substring-search-forward
          substring-suffix-ci?
          substring-suffix?
          substring-upper-case?
          substring<?
          substring=?)
   (export ()
-         (string-search-all string-find-all-matches)
-         (string-search-forward string-find-first-match)
          (substring string-copy)
          8-bit-string?
          burst-string
@@ -995,11 +993,8 @@ USA.
          string-downcase
          string-every
          string-fill!
-         string-find-all-matches
          string-find-first-index
-         string-find-first-match
          string-find-last-index
-         string-find-last-match
          string-find-next-char
          string-find-next-char-ci
          string-find-next-char-in-set
@@ -1028,7 +1023,9 @@ USA.
          string-prefix?
          string-ref
          string-replace
+         string-search-all
          string-search-backward
+         string-search-forward
          string-set!
          string-slice
          string-splitter
index 366b9a5382fea74713376ee52eb0a5c5be4cfce1..f8a6e587d3fa59b2b3423dde853d992b46b87cc6 100644 (file)
@@ -942,64 +942,60 @@ USA.
 ;;;; Search
 
 (define-integrable (string-matcher caller matcher)
-  (lambda (pattern text)
-    (guarantee string? pattern caller)
-    (guarantee string? text caller)
+  (lambda (pattern text #!optional start end)
     (let ((pend (string-length pattern)))
       (if (fix:= 0 pend)
          (error:bad-range-argument pend caller))
-      (matcher pattern pend text (fix:- (string-length text) pend)))))
-
-(define string-find-first-match
-  (string-matcher 'string-find-first-match
-                 %dumb-string-find-first-match))
-
-(define string-find-last-match
-  (string-matcher 'string-find-last-match
-                 %dumb-string-find-last-match))
-
-(define string-find-all-matches
-  (string-matcher 'string-find-all-matches
-                 %dumb-string-find-all-matches))
-
-(define (%dumb-string-find-first-match pattern pend text tlast)
-  (and (fix:>= tlast 0)
-       (let find-match ((tstart 0))
-        (and (fix:<= tstart tlast)
-             (let match ((pi 0) (ti tstart))
-               (if (fix:< pi pend)
-                   (if (char=? (string-ref pattern pi)
-                               (string-ref text ti))
-                       (match (fix:+ pi 1) (fix:+ ti 1))
-                       (find-match (fix:+ tstart 1)))
-                   tstart))))))
-
-(define (%dumb-string-find-last-match pattern pend text tlast)
-  (and (fix:>= tlast 0)
-       (let find-match ((tstart tlast))
-        (and (fix:>= tstart 0)
-             (let match ((pi 0) (ti tstart))
-               (if (fix:< pi pend)
-                   (if (char=? (string-ref pattern pi)
-                               (string-ref text ti))
-                       (match (fix:+ pi 1) (fix:+ ti 1))
-                       (find-match (fix:- tstart 1)))
-                   tstart))))))
-
-(define (%dumb-string-find-all-matches pattern pend text tlast)
-  (if (fix:>= tlast 0)
-      (let find-match ((tstart tlast) (matches '()))
-       (if (fix:>= tstart 0)
-           (find-match (fix:- tstart 1)
-                       (let match ((pi 0) (ti tstart))
-                         (if (fix:< pi pend)
-                             (if (char=? (string-ref pattern pi)
-                                         (string-ref text ti))
-                                 (match (fix:+ pi 1) (fix:+ ti 1))
-                                 matches)
-                             (cons tstart matches))))
-           matches))
-      '()))
+      (let* ((tend (fix:end-index end (string-length text) caller))
+            (tstart (fix:start-index start end caller)))
+       (matcher pattern pend text tstart (fix:- tend pend))))))
+
+(define string-search-forward
+  (string-matcher 'string-search-forward
+                 %dumb-string-search-forward))
+
+(define string-search-backward
+  (string-matcher 'string-search-backward
+                 %dumb-string-search-backward))
+
+(define string-search-all
+  (string-matcher 'string-search-all
+                 %dumb-string-search-all))
+
+(define (%dumb-string-search-forward pattern pend text tstart tlast)
+  (let find-match ((tindex tstart))
+    (and (fix:<= tindex tlast)
+        (let match ((pi 0) (ti tindex))
+          (if (fix:< pi pend)
+              (if (char=? (string-ref pattern pi)
+                          (string-ref text ti))
+                  (match (fix:+ pi 1) (fix:+ ti 1))
+                  (find-match (fix:+ tindex 1)))
+              tindex)))))
+
+(define (%dumb-string-search-backward pattern pend text tstart tlast)
+  (let find-match ((tindex tlast))
+    (and (fix:>= tindex tstart)
+        (let match ((pi 0) (ti tindex))
+          (if (fix:< pi pend)
+              (if (char=? (string-ref pattern pi)
+                          (string-ref text ti))
+                  (match (fix:+ pi 1) (fix:+ ti 1))
+                  (find-match (fix:- tindex 1)))
+              ti)))))
+
+(define (%dumb-string-search-all pattern pend text tstart tlast)
+  (let find-match ((tindex tlast) (matches '()))
+    (if (fix:>= tindex tstart)
+       (find-match (fix:- tindex 1)
+                   (let match ((pi 0) (ti tindex))
+                     (if (fix:< pi pend)
+                         (if (char=? (string-ref pattern pi)
+                                     (string-ref text ti))
+                             (match (fix:+ pi 1) (fix:+ ti 1))
+                             matches)
+                         (cons tindex matches))))
+       matches)))
 \f
 ;;;; Sequence converters
 
@@ -1474,32 +1470,8 @@ USA.
 
 (define (substring? pattern text)
   (and (or (fix:= 0 (string-length pattern))
-          (string-find-first-match pattern text))
+          (string-search-forward pattern text))
        #t))
-
-(define (string-search-backward pattern text)
-  (let ((index (string-find-last-match pattern text)))
-    (and index
-        (fix:+ index (string-length pattern)))))
-
-(define-integrable (substring-search-maker string-search)
-  (lambda (pattern text tstart tend)
-    (let* ((slice (string-slice text tstart tend))
-          (index (string-search pattern slice)))
-      (and index
-          (fix:+ tstart index)))))
-
-(define substring-search-forward
-  (substring-search-maker string-find-first-match))
-
-(define substring-search-backward
-  (substring-search-maker string-search-backward))
-
-(define (substring-search-all pattern text tstart tend)
-  (let ((slice (string-slice text tstart tend)))
-    (map (lambda (index)
-          (fix:+ tstart index))
-        (string-find-all-matches pattern slice))))
 \f
 (define (string-move! string1 string2 start2)
   (string-copy! string2 start2 string1))