Skip to content

Add startsWith and endsWith #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move startsWith and endsWith up
This is because like `stripPrefix` and `stripSuffix`, these functions
are CodeUnit/CodePoint-agnostic. The same was also done for tests.
  • Loading branch information
triallax committed Jul 3, 2021
commit b9054ecc8b269a8299f584958356e9085527de12
55 changes: 28 additions & 27 deletions src/Data/String/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import Data.String.Pattern (Pattern(..))
import Data.String.Unsafe as U

-------------------------------------------------------------------------------
-- `stripPrefix`, `stripSuffix`, and `contains` are CodeUnit/CodePoint agnostic
-- as they are based on patterns rather than lengths/indices, but they need to
-- be defined in here to avoid a circular module dependency
-- `stripPrefix`, `stripSuffix`, `startsWith`, `endsWith`, and `contains` are
-- CodeUnit/CodePoint agnostic as they are based on patterns rather than
-- lengths/indices, but they need to be defined in here to avoid a circular
-- module dependency
-------------------------------------------------------------------------------

-- | If the string starts with the given prefix, return the portion of the
Expand Down Expand Up @@ -63,6 +64,30 @@ stripSuffix (Pattern suffix) str =
let { before, after } = splitAt (length str - length suffix) str in
if after == suffix then Just before else Nothing

-- | Checks whether the given string starts with the pattern.
-- |
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
-- | `stripPrefix`.
-- |
-- | ```purescript
-- | startsWith (Pattern "foo") "foobar" == true
-- | startsWith (Pattern "bar") "foobar" == false
-- | ```
startsWith :: Pattern -> String -> Boolean
startsWith pat = isJust <<< stripPrefix pat

-- | Checks whether the given string ends with the pattern.
-- |
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
-- | `stripSuffix`.
-- |
-- | ```purescript
-- | endsWith (Pattern "bar") "foobar" == true
-- | endsWith (Pattern "foo") "foobar" == false
-- | ```
endsWith :: Pattern -> String -> Boolean
endsWith pat = isJust <<< stripSuffix pat

-- | Checks whether the pattern appears in the given string.
-- |
-- | ```purescript
Expand Down Expand Up @@ -345,27 +370,3 @@ foreign import _slice :: Int -> Int -> String -> String
-- | splitAt i s == {before: take i s, after: drop i s}
-- | ```
foreign import splitAt :: Int -> String -> { before :: String, after :: String }

-- | Checks whether the given string starts with the pattern.
-- |
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
-- | `stripPrefix`.
-- |
-- | ```purescript
-- | startsWith (Pattern "foo") "foobar" == true
-- | startsWith (Pattern "bar") "foobar" == false
-- | ```
startsWith :: Pattern -> String -> Boolean
startsWith pat = isJust <<< stripPrefix pat

-- | Checks whether the given string ends with the pattern.
-- |
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
-- | `stripSuffix`.
-- |
-- | ```purescript
-- | endsWith (Pattern "bar") "foobar" == true
-- | endsWith (Pattern "foo") "foobar" == false
-- | ```
endsWith :: Pattern -> String -> Boolean
endsWith pat = isJust <<< stripSuffix pat
28 changes: 14 additions & 14 deletions test/Test/Data/String/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ testStringCodeUnits = do
, expected: Just ""
}

log "startsWith"
assert $ SCU.startsWith (Pattern "foo") "foobar"
assert $ SCU.startsWith (Pattern "foo") "foo"
assert $ SCU.startsWith (Pattern "") ""
assert $ SCU.startsWith (Pattern "") "foo"
assert $ not $ SCU.startsWith (Pattern "foo") ""

log "endsWith"
assert $ SCU.endsWith (Pattern "bar") "foobar"
assert $ SCU.endsWith (Pattern "bar") "bar"
assert $ SCU.endsWith (Pattern "") ""
assert $ SCU.endsWith (Pattern "") "bar"
assert $ not $ SCU.endsWith (Pattern "bar") ""

log "charAt"
assertEqual
{ actual: SCU.charAt 0 ""
Expand Down Expand Up @@ -510,17 +524,3 @@ testStringCodeUnits = do
{ actual: SCU.slice 3 1000 "purescript"
, expected: Nothing -- e > l
}

log "startsWith"
assert $ SCU.startsWith (Pattern "foo") "foobar"
assert $ SCU.startsWith (Pattern "foo") "foo"
assert $ SCU.startsWith (Pattern "") ""
assert $ SCU.startsWith (Pattern "") "foo"
assert $ not $ SCU.startsWith (Pattern "foo") ""

log "endsWith"
assert $ SCU.endsWith (Pattern "bar") "foobar"
assert $ SCU.endsWith (Pattern "bar") "bar"
assert $ SCU.endsWith (Pattern "") ""
assert $ SCU.endsWith (Pattern "") "bar"
assert $ not $ SCU.endsWith (Pattern "bar") ""