(string-set! result j (string-ref string i)))
result)))
\f
-(define (string-match-forward string1 string2)
- (guarantee-2-strings string1 string2 'STRING-MATCH-FORWARD)
- (%substring-match-forward string1 0 (string-length string1)
- string2 0 (string-length string2)))
-
-(define (substring-match-forward string1 start1 end1 string2 start2 end2)
- (guarantee-2-substrings string1 start1 end1
- string2 start2 end2
- 'SUBSTRING-MATCH-FORWARD)
- (%substring-match-forward string1 start1 end1 string2 start2 end2))
-
-(define (%substring-match-forward string1 start1 end1 string2 start2 end2)
- (let ((end (fix:+ start1 (fix:min (fix:- end1 start1) (fix:- end2 start2)))))
- (let loop ((i1 start1) (i2 start2))
- (if (or (fix:= i1 end)
- (not (char=? (string-ref string1 i1)
- (string-ref string2 i2))))
- (fix:- i1 start1)
- (loop (fix:+ i1 1) (fix:+ i2 1))))))
-
-(define (string-match-forward-ci string1 string2)
- (guarantee-2-strings string1 string2 'STRING-MATCH-FORWARD-CI)
- (%substring-match-forward-ci string1 0 (string-length string1)
- string2 0 (string-length string2)))
-
-(define (substring-match-forward-ci string1 start1 end1 string2 start2 end2)
- (guarantee-2-substrings string1 start1 end1
- string2 start2 end2
- 'SUBSTRING-MATCH-FORWARD-CI)
- (%substring-match-forward-ci string1 start1 end1 string2 start2 end2))
-
-(define (%substring-match-forward-ci string1 start1 end1 string2 start2 end2)
- (let ((end (fix:+ start1 (fix:min (fix:- end1 start1) (fix:- end2 start2)))))
- (let loop ((i1 start1) (i2 start2))
- (if (or (fix:= i1 end)
- (not (char-ci=? (string-ref string1 i1)
- (string-ref string2 i2))))
- (fix:- i1 start1)
- (loop (fix:+ i1 1) (fix:+ i2 1))))))
-\f
-(define (string-match-backward string1 string2)
- (guarantee-2-strings string1 string2 'STRING-MATCH-BACKWARD)
- (%substring-match-backward string1 0 (string-length string1)
- string2 0 (string-length string2)))
-
-(define (substring-match-backward string1 start1 end1 string2 start2 end2)
- (guarantee-2-substrings string1 start1 end1
- string2 start2 end2
- 'SUBSTRING-MATCH-BACKWARD)
- (%substring-match-backward string1 start1 end1 string2 start2 end2))
-
-(define (%substring-match-backward string1 start1 end1 string2 start2 end2)
- (let ((start (fix:- end1 (fix:min (fix:- end1 start1) (fix:- end2 start2)))))
- (if (fix:= end1 start)
- 0
- (let loop ((i1 (fix:- end1 1)) (i2 (fix:- end2 1)))
- (if (char=? (string-ref string1 i1) (string-ref string2 i2))
- (if (fix:= i1 start)
- (fix:- end1 i1)
- (loop (fix:- i1 1) (fix:- i2 1)))
- (fix:- end1 (fix:+ i1 1)))))))
-
-(define (string-match-backward-ci string1 string2)
- (guarantee-2-strings string1 string2 'STRING-MATCH-BACKWARD-CI)
- (%substring-match-backward-ci string1 0 (string-length string1)
- string2 0 (string-length string2)))
-
-(define (substring-match-backward-ci string1 start1 end1 string2 start2 end2)
- (guarantee-2-substrings string1 start1 end1
- string2 start2 end2
- 'SUBSTRING-MATCH-BACKWARD-CI)
- (%substring-match-backward-ci string1 start1 end1 string2 start2 end2))
-
-(define (%substring-match-backward-ci string1 start1 end1 string2 start2 end2)
- (let ((start (fix:- end1 (fix:min (fix:- end1 start1) (fix:- end2 start2)))))
- (if (fix:= end1 start)
- 0
- (let loop ((i1 (fix:- end1 1)) (i2 (fix:- end2 1)))
- (if (char-ci=? (string-ref string1 i1) (string-ref string2 i2))
- (if (fix:= i1 start)
- (fix:- end1 i1)
- (loop (fix:- i1 1) (fix:- i2 1)))
- (fix:- end1 (fix:+ i1 1)))))))
-\f
;;;; Trim
(define (string-trim-left string #!optional char-set)
(define string-ci>? (string-comparison-maker %string-ci>?))
(define string-ci>=? (string-comparison-maker %string-ci>=?))
\f
+;;;; Match
+
+(define (string-match-forward string1 string2)
+ (let ((end1 (string-length string1))
+ (end2 (string-length string2)))
+ (let ((end (fix:min end1 end2)))
+ (let loop ((i 0))
+ (if (and (fix:< i end)
+ (char=? (string-ref string1 i)
+ (string-ref string2 i)))
+ (loop (fix:+ i 1))
+ i)))))
+
+(define (string-match-forward-ci string1 string2)
+ (string-match-forward (string-foldcase string1)
+ (string-foldcase string2)))
+
+(define (string-match-backward string1 string2)
+ (let ((s1 (fix:- (string-length string1) 1)))
+ (let loop ((i s1) (j (fix:- (string-length string2) 1)))
+ (if (and (fix:>= i 0)
+ (fix:>= j 0)
+ (char=? (string-ref string1 i)
+ (string-ref string2 j)))
+ (loop (fix:- i 1)
+ (fix:- j 1))
+ (fix:- s1 i)))))
+
+(define (string-match-backward-ci string1 string2)
+ (string-match-backward (string-foldcase string1)
+ (string-foldcase string2)))
+
(define-integrable (prefix-maker c= caller)
(lambda (prefix string #!optional start end)
(let* ((end (fix:end-index end (string-length string) caller))
;;; Incorrect implementation: should do string-foldcase on both args.
(define string-prefix-ci? (prefix-maker char-ci=? 'string-prefix-ci?))
(define string-suffix-ci? (suffix-maker char-ci=? 'string-suffix-ci?))
+\f
+;;;; Case
(define (string-downcase string)
(case-transform char-downcase-full string))