From 85e5db38fb742da47427994f4438b75c42247119 Mon Sep 17 00:00:00 2001 From: ben Date: Tue, 28 Jul 2015 13:23:26 -0400 Subject: [PATCH] renamed string-startswith/endswith to string-prefix/suffix --- .../scribblings/reference/strings.scrbl | 28 +++++++-------- .../racket-test-core/tests/racket/string.rktl | 35 ++++++++++--------- racket/collects/racket/string.rkt | 9 +++-- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/pkgs/racket-doc/scribblings/reference/strings.scrbl b/pkgs/racket-doc/scribblings/reference/strings.scrbl index eb991e8993..6da9befe99 100644 --- a/pkgs/racket-doc/scribblings/reference/strings.scrbl +++ b/pkgs/racket-doc/scribblings/reference/strings.scrbl @@ -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] diff --git a/pkgs/racket-test-core/tests/racket/string.rktl b/pkgs/racket-test-core/tests/racket/string.rktl index e0eec0caaa..07cd8c4b9a 100644 --- a/pkgs/racket-test-core/tests/racket/string.rktl +++ b/pkgs/racket-test-core/tests/racket/string.rktl @@ -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) diff --git a/racket/collects/racket/string.rkt b/racket/collects/racket/string.rkt index 3d0db26729..31b8df1fe5 100644 --- a/racket/collects/racket/string.rkt +++ b/racket/collects/racket/string.rkt @@ -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))