renamed string-startswith/endswith to string-prefix/suffix

This commit is contained in:
ben 2015-07-28 13:23:26 -04:00 committed by Vincent St-Amour
parent 22cda63200
commit 85e5db38fb
3 changed files with 36 additions and 36 deletions

View File

@ -397,13 +397,6 @@ one between @racket[list] and @racket[list*].
]}
@defproc[(string-endswith? [str string?]
[suffix string?])
boolean?]{
Returns @racket[#t] if the second argument is a suffix of the first.}
@defproc[(string-join [strs (listof string?)] [sep string? " "]
[#:before-first before-first string? ""]
[#:before-last before-last string? sep]
@ -448,6 +441,13 @@ The result of @racket[(string-normalize-spaces str sep space)] is the same
as @racket[(string-join (string-split str sep ....) space)].}
@defproc[(string-prefix? [str string?]
[prefix string?])
boolean?]{
Returns @racket[#t] if the second argument is a prefix of the first.}
@defproc[(string-replace [str string?]
[from (or/c string? regexp?)]
[to string?]
@ -466,13 +466,6 @@ replaced if @racket[all?] is @racket[#f].
]}
@defproc[(string-startswith? [str string?]
[prefix string?])
boolean?]{
Returns @racket[#t] if the second argument is a prefix of the first.}
@defproc[(string-split [str string?]
[sep (or/c string? regexp?) #px"\\s+"]
[#:trim? trim? any/c #t]
@ -497,6 +490,13 @@ and @racket[repeat?] controls matching repeated sequences.
]}
@defproc[(string-suffix? [str string?]
[suffix string?])
boolean?]{
Returns @racket[#t] if the second argument is a suffix of the first.}
@defproc[(string-trim [str string?]
[sep (or/c string? regexp?) #px"\\s+"]
[#:left? left? any/c #t]

View File

@ -490,23 +490,24 @@
(test "_1_ !!!" string-replace "_1_ _2_" str "!!!") ;verify that the new str is used
)
;; ---------- string-starts/endswith ----------
;; ---------- string-prefix?/suffix? ----------
(let ()
(test #t string-startswith? "racket" "")
(test #t string-startswith? "racket" "r")
(test #t string-startswith? "racket" "rack")
(test #t string-startswith? "racket" "racket")
(test #t string-endswith? "racket" "")
(test #t string-endswith? "racket" "t")
(test #t string-endswith? "racket" "cket")
(test #t string-endswith? "racket" "racket")
(test #f string-startswith? "" "racket")
(test #f string-startswith? "racket" "R")
(test #f string-startswith? "racket" "rak")
(test #f string-startswith? "racket" "racket2")
(test #f string-endswith? "" "racket")
(test #f string-endswith? "racket" "T")
(test #f string-endswith? "racket" "r")
(test #f string-endswith? "racket" "kat"))
(test #t string-prefix? "racket" "")
(test #t string-prefix? "racket" "r")
(test #t string-prefix? "racket" "rack")
(test #t string-prefix? "racket" "racket")
(test #t string-suffix? "racket" "")
(test #t string-suffix? "racket" "t")
(test #t string-suffix? "racket" "cket")
(test #t string-suffix? "racket" "racket")
;; --------------------
(test #f string-prefix? "" "racket")
(test #f string-prefix? "racket" "R")
(test #f string-prefix? "racket" "rak")
(test #f string-prefix? "racket" "racket2")
(test #f string-suffix? "" "racket")
(test #f string-suffix? "racket" "T")
(test #f string-suffix? "racket" "r")
(test #f string-suffix? "racket" "kat"))
(report-errs)

View File

@ -7,8 +7,8 @@
string-split
string-replace
non-empty-string?
string-startswith?
string-endswith?)
string-prefix?
string-suffix?)
(define string-append*
(case-lambda [(strs) (apply string-append strs)] ; optimize common cases
@ -141,15 +141,14 @@
(define (non-empty-string? x)
(and (string? x) (not (zero? (string-length x)))))
(define (string-startswith? str prefix)
(define (string-prefix? str prefix)
(and
(<= (string-length prefix) (string-length str))
(for/and ([c1 (in-string str)]
[c2 (in-string prefix)])
(char=? c1 c2))))
(define (string-endswith? str suffix)
;; Skip all but the last `suffix` characters of `str`
(define (string-suffix? str suffix)
(define offset (- (string-length str) (string-length suffix)))
(and
(not (negative? offset))