Fixed implementation of STRING-CONTAINS by using the commented out

"slow & simple" version instead of calling the KMP searcher.

See http://srfi.schemers.org/srfi-13/post-mail-archive/msg00003.html

I fixed the mentioned "off-by-one error" by using <= instead of < at
the termination check.
This commit is contained in:
mainzelm 2004-01-13 15:37:35 +00:00
parent b7388740b9
commit 90ea0cf502
1 changed files with 24 additions and 16 deletions

View File

@ -1365,30 +1365,38 @@
;;; comparison testing with fancier implementations.
;;; See below for fast KMP version.
;(define (string-contains string substring . maybe-starts+ends)
; (let-string-start+end2 (start1 end1 start2 end2)
; string-contains string substring maybe-starts+ends
; (let* ((len (- end2 start2))
; (i-bound (- end1 len)))
; (let lp ((i start1))
; (and (< i i-bound)
; (if (string= string substring i (+ i len) start2 end2)
; i
; (lp (+ i 1))))))))
;;; Searching for an occurrence of a substring
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (%string-contains string substring start1 end1 start2 end2 the-string=)
(let* ((len (- end2 start2))
(i-bound (- end1 len)))
(let lp ((i start1))
(and (<= i i-bound)
(if (the-string= string substring i (+ i len) start2 end2)
i
(lp (+ i 1)))))))
(define (string-contains text pattern . maybe-starts+ends)
(let-string-start+end2 (t-start t-end p-start p-end)
string-contains text pattern maybe-starts+ends
(%kmp-search pattern text char=? p-start p-end t-start t-end)))
(%string-contains text pattern t-start t-end p-start p-end string=)))
(define (string-contains-ci text pattern . maybe-starts+ends)
(let-string-start+end2 (t-start t-end p-start p-end)
string-contains-ci text pattern maybe-starts+ends
(%kmp-search pattern text char-ci=? p-start p-end t-start t-end)))
(%string-contains text pattern t-start t-end p-start p-end string-ci=)))
;;; Searching for an occurrence of a substring
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Broken, see http://srfi.schemers.org/srfi-13/post-mail-archive/msg00003.html
; (define (string-contains text pattern . maybe-starts+ends)
; (let-string-start+end2 (t-start t-end p-start p-end)
; string-contains text pattern maybe-starts+ends
; (%kmp-search pattern text char=? p-start p-end t-start t-end)))
; (define (string-contains-ci text pattern . maybe-starts+ends)
; (let-string-start+end2 (t-start t-end p-start p-end)
; string-contains-ci text pattern maybe-starts+ends
; (%kmp-search pattern text char-ci=? p-start p-end t-start t-end)))
;;; Knuth-Morris-Pratt string searching