Skip to content

Commit

Permalink
str:contains? and containsp
Browse files Browse the repository at this point in the history
  • Loading branch information
vindarel committed Sep 11, 2017
1 parent a45dcb7 commit 0b22a04
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ or `str:concat strings` instead of an unusual `format` construct; one discoverab
easier to feed pipes and arrows.

<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-generate-toc again -->

**Table of Contents**

- [A modern and consistent Common Lisp string manipulation library](#a-modern-and-consistent-common-lisp-string-manipulation-library)
Expand All @@ -36,6 +35,7 @@ or `str:concat strings` instead of an unusual `format` construct; one discoverab
- [blank?, blankp `(s)`](#blank-blankp-s)
- [starts-with?, starts-with-p `(start s &key ignore-case)`](#starts-with-starts-with-p-start-s-key-ignore-case)
- [ends-with?, ends-with-p `(end s &key ignore-case)`](#ends-with-ends-with-p-end-s-key-ignore-case)
- [contains?, containsp `(substring s &key (ignore-case nil))`](#contains-containsp-substring-s-key-ignore-case-nil)
- [Others](#others)
- [replace `(old new s)`](#replace-old-new-s)
- [Dev and test](#dev-and-test)
Expand Down Expand Up @@ -184,6 +184,15 @@ True if `s` ends with the substring `end`. Ignore case by default.

(ends-with? "bar" "foobar") ;; => T

#### contains?, containsp `(substring s &key (ignore-case nil))`

Return true if `s` contains `substring`, nil otherwise. Ignore the
case with `:ignore-case t` (don't ignore by default).

Based on a simple call to the built-in `search` (which returns the
position of the substring).


### Others

#### replace `(old new s)`
Expand Down
20 changes: 20 additions & 0 deletions str.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
(defpackage str
(:use :cl)
(:export
:contains?
:containsp
:trim-left
:trim-right
:trim
Expand Down Expand Up @@ -132,3 +134,21 @@
(funcall fn s end :start1 (- (length s) (length end))))))

(setf (fdefinition 'ends-with-p) #'ends-with?)

(defun contains? (substring s &key (ignore-case nil))
"Return `t` if `s` contains `substring`, nil otherwise. Ignore the case with `:ignore-case t`.
A simple call to the built-in `search` (which returns the position of the substring)."
(let ((a (if ignore-case
(string-downcase substring)
substring))
(b (if ignore-case
(string-downcase s)
s)))
;; weird case: (search "" nil) => 0
(if (and (blank? substring)
(null s))
nil
(if (search a b)
t))))

(setf (fdefinition 'containsp) #'contains?)
14 changes: 12 additions & 2 deletions test-str.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
2
" (unlines '("1" "2" ""))))

(subtest "Starts-with"
(subtest "starts-with?"
(ok (starts-with? "foo" "foobar") "default case")
(ok (starts-with? "" "foo") "with blank start")
(ok (not (starts-with? "rs" "")) "with blank s")
Expand All @@ -118,11 +118,21 @@
(ok (starts-with-p "f" "foo") "starts-with-p alias")
(ok (starts-with? "FOO" "foobar" :ignore-case t) "ignore case"))

(subtest "ends-with"
(subtest "ends-with?"
(ok (ends-with? "bar" "foobar") "default case")
(ok (ends-with-p "bar" "foobar") "ends-with-p alias")
(ok (not (ends-with? "BAR" "foobar")) "don't ignore case")
(ok (ends-with? "BAR" "foobar" :ignore-case t) "ignore case"))

(subtest "contains?"
(ok (contains? "foo" "blafoobar") "default")
(ok (not (contains? "foo" "")) "with no string")
(ok (not (contains? "" nil)) "a blank substring in a nil str")
(ok (not (contains? "foo" nil)) "with string nil")
(ok (not (contains? "Foo" "blafoobar")) "with case")
(ok (contains? "Foo" "blafoobar" :ignore-case t) "ignore case")
(ok (containsp "Foo" "blafoobar" :ignore-case t) "containsp alias")
)

;; prove end
(finalize)

0 comments on commit 0b22a04

Please sign in to comment.