[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bug in string-index-right and string-skip-right



Both string-index-right and string-skip-right will continue to search left past a given start index.

   (string-index-right "abbb" #\a 1) ;; => 0, but should be #f
   (string-skip-right  "abbb" #\b 1) ;; => 0, but should be #f

This also causes incorrect results for string-trim-right, string-trim-both and string-tokenize when given a non-zero start argument.

The attached patch should solve the problem. I will apply it to the reference implementation unless anyone objects.

David
Index: srfi-13.scm
===================================================================
RCS file: /afs/informatik.uni-tuebingen.de/home/srfi/srfi-cvs/srfi/srfi-13/srfi-13.scm,v
retrieving revision 1.4
diff -u -r1.4 srfi-13.scm
--- srfi-13.scm	11 Oct 2005 06:59:49 -0000	1.4
+++ srfi-13.scm	1 Nov 2006 03:23:50 -0000
@@ -1159,17 +1159,17 @@
   (let-string-start+end (start end) string-index-right str maybe-start+end
     (cond ((char? criterion)
 	   (let lp ((i (- end 1)))
-	     (and (>= i 0)
+	     (and (>= i start)
 		  (if (char=? criterion (string-ref str i)) i
 		      (lp (- i 1))))))
 	  ((char-set? criterion)
 	   (let lp ((i (- end 1)))
-	     (and (>= i 0)
+	     (and (>= i start)
 		  (if (char-set-contains? criterion (string-ref str i)) i
 		      (lp (- i 1))))))
 	  ((procedure? criterion)
 	   (let lp ((i (- end 1)))
-	     (and (>= i 0)
+	     (and (>= i start)
 		  (if (criterion (string-ref str i)) i
 		      (lp (- i 1))))))
 	  (else (error "Second param is neither char-set, char, or predicate procedure."
@@ -1201,19 +1201,19 @@
   (let-string-start+end (start end) string-skip-right str maybe-start+end
     (cond ((char? criterion)
 	   (let lp ((i (- end 1)))
-	     (and (>= i 0)
+	     (and (>= i start)
 		  (if (char=? criterion (string-ref str i))
 		      (lp (- i 1))
 		      i))))
 	  ((char-set? criterion)
 	   (let lp ((i (- end 1)))
-	     (and (>= i 0)
+	     (and (>= i start)
 		  (if (char-set-contains? criterion (string-ref str i))
 		      (lp (- i 1))
 		      i))))
 	  ((procedure? criterion)
 	   (let lp ((i (- end 1)))
-	     (and (>= i 0)
+	     (and (>= i start)
 		  (if (criterion (string-ref str i)) (lp (- i 1))
 		      i))))
 	  (else (error "CRITERION param is neither char-set or char."