From 3f338dfb7b657fadfc16a93ffabad4c4b9fa1105 Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Fri, 3 Mar 2017 20:30:13 -0800 Subject: [PATCH] Change string-search-X interface back to its original design. --- src/runtime/runtime.pkg | 13 ++-- src/runtime/ustring.scm | 132 ++++++++++++++++------------------------ 2 files changed, 57 insertions(+), 88 deletions(-) diff --git a/src/runtime/runtime.pkg b/src/runtime/runtime.pkg index d98ccbeb1..e8b584ada 100644 --- a/src/runtime/runtime.pkg +++ b/src/runtime/runtime.pkg @@ -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= 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))) ;;;; 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)))) (define (string-move! string1 string2 start2) (string-copy! string2 start2 string1)) -- 2.25.1