fix string

pull/2/head
Matthew Butterick 11 years ago
parent a36a6c7f0d
commit ba24e81e21

@ -3,12 +3,12 @@
(define+provide/contract (starts-with? str starter) (define+provide/contract (starts-with? str starter)
(string? string? . -> . coerce/boolean?) (coerce/string? coerce/string? . -> . coerce/boolean?)
(define starter-pattern (regexp (format "^~a" starter))) (and (<= (string-length starter) (string-length str))
(regexp-match starter-pattern str)) (equal? (substring str 0 (string-length starter)) starter)))
(define+provide/contract (ends-with? str ender) (define+provide/contract (ends-with? str ender)
(string? string? . -> . coerce/boolean?) (coerce/string? coerce/string? . -> . coerce/boolean?)
(define ender-pattern (regexp (format "~a$" ender))) (and (<= (string-length ender) (string-length str))
(regexp-match ender-pattern str)) (equal? (substring str (- (string-length str) (string-length ender)) (string-length str)) ender)))

@ -83,6 +83,7 @@
(check-true ("foobar" . starts-with? . "f")) (check-true ("foobar" . starts-with? . "f"))
(check-true ("foobar" . starts-with? . "foobar")) (check-true ("foobar" . starts-with? . "foobar"))
(check-false ("foobar" . starts-with? . "bar")) (check-false ("foobar" . starts-with? . "bar"))
(check-false ("foobar" . starts-with? . "."))
(check-true ("foobar" . ends-with? . "bar")) (check-true ("foobar" . ends-with? . "bar"))
(check-true ("foobar" . ends-with? . "r")) (check-true ("foobar" . ends-with? . "r"))
(check-true ("foobar" . ends-with? . "foobar")) (check-true ("foobar" . ends-with? . "foobar"))

Loading…
Cancel
Save