diff --git a/scribblings/string.scrbl b/scribblings/string.scrbl new file mode 100644 index 0000000..85054d0 --- /dev/null +++ b/scribblings/string.scrbl @@ -0,0 +1,53 @@ +#lang scribble/manual + +@(require scribble/eval (for-label racket sugar)) + +@(define my-eval (make-base-eval)) +@(my-eval `(require sugar)) + +@title{String} +@defmodule[sugar/string] + + +@defproc[ +(starts-with? +[str stringish?] +[starter stringish?]) +boolean?] +Return @racket[#t] if @racket[_str] starts with @racket[_starter], otherwise @racket[#f]. + +@examples[#:eval my-eval +(starts-with? "foobar" "foo") +(starts-with? "foobar" "foobar") +(starts-with? "foobar" "zam") +(starts-with? "foobar" "foobars") +] + + +@defproc[ +(ends-with? +[str stringish?] +[ender stringish?]) +boolean?] +Return @racket[#t] if @racket[_str] ends with @racket[_ender], otherwise @racket[#f]. + +@examples[#:eval my-eval +(ends-with? "foobar" "foo") +(ends-with? "foobar" "foobar") +(ends-with? "foobar" "zam") +(ends-with? "foobar" "foobars") +] + + +@defproc[ +(capitalized? +[str stringish?]) +boolean?] +Return @racket[#t] if @racket[_str] starts with a capital letter, otherwise @racket[#f]. + +@examples[#:eval my-eval +(capitalized? "Brennan") +(capitalized? "Brennan stinks") +(capitalized? "stinks") +] + diff --git a/scribblings/sugar.scrbl b/scribblings/sugar.scrbl index dcb252a..15d9a5e 100644 --- a/scribblings/sugar.scrbl +++ b/scribblings/sugar.scrbl @@ -30,6 +30,8 @@ A collection of small functions to help make Racket code simpler & more readable @include-section["list.scrbl"] +@include-section["string.scrbl"] + @include-section["license.scrbl"] @index-section[] diff --git a/string.rkt b/string.rkt index 9188b22..20ad535 100644 --- a/string.rkt +++ b/string.rkt @@ -1,13 +1,11 @@ #lang racket/base (require "define.rkt" "coerce.rkt") - (define+provide/contract (starts-with? str starter) (coerce/string? coerce/string? . -> . coerce/boolean?) (and (<= (string-length starter) (string-length str)) (equal? (substring str 0 (string-length starter)) starter))) - (define+provide/contract (ends-with? str ender) (coerce/string? coerce/string? . -> . coerce/boolean?) (and (<= (string-length ender) (string-length str)) diff --git a/tests.rkt b/tests.rkt index 7134d94..e2ea756 100644 --- a/tests.rkt +++ b/tests.rkt @@ -141,4 +141,16 @@ (check-equal? (remove-ext* foo.txt-path) foo-path) (check-equal? (remove-ext* (->path ".foo.txt")) (->path ".foo.txt")) (check-not-equal? (remove-ext* foo.bar.txt-path) foo.bar-path) ; removes more than one ext -(check-equal? (remove-ext* foo.bar.txt-path) foo-path) \ No newline at end of file +(check-equal? (remove-ext* foo.bar.txt-path) foo-path) + + +(check-true (starts-with? "foobar" "foo")) +(check-true (starts-with? "foobar" "foobar")) +(check-false (starts-with? "foobar" "zam")) +(check-false (starts-with? "foobar" "foobars")) +(check-true (ends-with? "foobar" "bar")) +(check-false (ends-with? "foobar" "zam")) +(check-true (ends-with? "foobar" "foobar")) +(check-false (ends-with? "foobar" "foobars")) +(check-true (capitalized? "Brennan")) +(check-false (capitalized? "foobar")) \ No newline at end of file