As a follow-up, here is a patch including some basic tests (in
tests/runtime/test-string.scm).
Greetings, Peter
diff --git a/src/runtime/ustring.scm b/src/runtime/ustring.scm
index 2cada76b3..adaf7e530 100644
--- a/src/runtime/ustring.scm
+++ b/src/runtime/ustring.scm
@@ -1962,23 +1962,25 @@ USA.
(let ((predicate (char-matcher->predicate to-trim 'string-trimmer))
(get-trimmed (if copy? substring string-slice)))
(lambda (string)
- (let ((end (string-length string)))
- (get-trimmed
- string
- (if (eq? where 'trailing)
+ (let* ((end (string-length string))
+ (the-start (if (eq? where 'trailing)
0
(let loop ((index 0))
(if (and (fix:< index end)
(predicate (string-ref string index)))
(loop (fix:+ index 1))
- index)))
- (if (eq? where 'leading)
+ index))))
+ (the-end (if (eq? where 'leading)
end
(let loop ((index end))
(if (and (fix:> index 0)
(predicate (string-ref string (fix:- index 1))))
(loop (fix:- index 1))
- index)))))))))
+ index)))))
+ (get-trimmed
+ string
+ (min the-start the-end)
+ the-end))))))
(define-deferred string-trimmer-options
(keyword-option-parser
diff --git a/tests/runtime/test-string.scm b/tests/runtime/test-string.scm
index 44f37966a..0e0ade85a 100644
--- a/tests/runtime/test-string.scm
+++ b/tests/runtime/test-string.scm
@@ -3076,3 +3076,15 @@ USA.
(#t #\x0061 #t #\x002C #t #\x002C #t #\x0061 #t)
(#t #\x0061 #f #\x005F #f #\x0031 #t #\x002C #t #\x002C #t #\x0061 #t)
(#t #\x0061 #f #\x005F #f #\x0061 #t #\x002C #t #\x002C #t #\x0061 #t))))
+
+(assert-string= "foo" (string-trim "foo "))
+(assert-string= "foo" (string-trim " foo"))
+(assert-string= "foo" (string-trim " foo "))
+(assert-string= "foo " (string-trim-left " foo "))
+(assert-string= " foo" (string-trim-right " foo "))
+(assert-string= "" (string-trim "\"\"" (char-set-invert (char-set #\"))))
+(assert-string= "" (string-trim-left "\"\"" (char-set-invert (char-set #\"))))
+(assert-string= "" (string-trim-right "\"\"" (char-set-invert (char-set #\"))))
+(assert-string= "foo" (string-trim "aaafooaaa" (char-set #\f #\o)))
+(assert-string= "fooaaa" (string-trim-left "aaafooaaa" (char-set #\f #\o)))
+(assert-string= "aaafoo" (string-trim-right "aaafooaaa" (char-set #\f #\o)))
_______________________________________________
MIT-Scheme-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/mit-scheme-devel