Skip to content

Commit b9054ec

Browse files
committed
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.
1 parent e9c8e7f commit b9054ec

2 files changed

Lines changed: 42 additions & 41 deletions

File tree

src/Data/String/CodeUnits.purs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ import Data.String.Pattern (Pattern(..))
3333
import Data.String.Unsafe as U
3434

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

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

67+
-- | Checks whether the given string starts with the pattern.
68+
-- |
69+
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
70+
-- | `stripPrefix`.
71+
-- |
72+
-- | ```purescript
73+
-- | startsWith (Pattern "foo") "foobar" == true
74+
-- | startsWith (Pattern "bar") "foobar" == false
75+
-- | ```
76+
startsWith :: Pattern -> String -> Boolean
77+
startsWith pat = isJust <<< stripPrefix pat
78+
79+
-- | Checks whether the given string ends with the pattern.
80+
-- |
81+
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
82+
-- | `stripSuffix`.
83+
-- |
84+
-- | ```purescript
85+
-- | endsWith (Pattern "bar") "foobar" == true
86+
-- | endsWith (Pattern "foo") "foobar" == false
87+
-- | ```
88+
endsWith :: Pattern -> String -> Boolean
89+
endsWith pat = isJust <<< stripSuffix pat
90+
6691
-- | Checks whether the pattern appears in the given string.
6792
-- |
6893
-- | ```purescript
@@ -345,27 +370,3 @@ foreign import _slice :: Int -> Int -> String -> String
345370
-- | splitAt i s == {before: take i s, after: drop i s}
346371
-- | ```
347372
foreign import splitAt :: Int -> String -> { before :: String, after :: String }
348-
349-
-- | Checks whether the given string starts with the pattern.
350-
-- |
351-
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
352-
-- | `stripPrefix`.
353-
-- |
354-
-- | ```purescript
355-
-- | startsWith (Pattern "foo") "foobar" == true
356-
-- | startsWith (Pattern "bar") "foobar" == false
357-
-- | ```
358-
startsWith :: Pattern -> String -> Boolean
359-
startsWith pat = isJust <<< stripPrefix pat
360-
361-
-- | Checks whether the given string ends with the pattern.
362-
-- |
363-
-- | **NOTE**: if you also want to get the string stripped of the pattern, see
364-
-- | `stripSuffix`.
365-
-- |
366-
-- | ```purescript
367-
-- | endsWith (Pattern "bar") "foobar" == true
368-
-- | endsWith (Pattern "foo") "foobar" == false
369-
-- | ```
370-
endsWith :: Pattern -> String -> Boolean
371-
endsWith pat = isJust <<< stripSuffix pat

test/Test/Data/String/CodeUnits.purs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ testStringCodeUnits = do
6464
, expected: Just ""
6565
}
6666

67+
log "startsWith"
68+
assert $ SCU.startsWith (Pattern "foo") "foobar"
69+
assert $ SCU.startsWith (Pattern "foo") "foo"
70+
assert $ SCU.startsWith (Pattern "") ""
71+
assert $ SCU.startsWith (Pattern "") "foo"
72+
assert $ not $ SCU.startsWith (Pattern "foo") ""
73+
74+
log "endsWith"
75+
assert $ SCU.endsWith (Pattern "bar") "foobar"
76+
assert $ SCU.endsWith (Pattern "bar") "bar"
77+
assert $ SCU.endsWith (Pattern "") ""
78+
assert $ SCU.endsWith (Pattern "") "bar"
79+
assert $ not $ SCU.endsWith (Pattern "bar") ""
80+
6781
log "charAt"
6882
assertEqual
6983
{ actual: SCU.charAt 0 ""
@@ -510,17 +524,3 @@ testStringCodeUnits = do
510524
{ actual: SCU.slice 3 1000 "purescript"
511525
, expected: Nothing -- e > l
512526
}
513-
514-
log "startsWith"
515-
assert $ SCU.startsWith (Pattern "foo") "foobar"
516-
assert $ SCU.startsWith (Pattern "foo") "foo"
517-
assert $ SCU.startsWith (Pattern "") ""
518-
assert $ SCU.startsWith (Pattern "") "foo"
519-
assert $ not $ SCU.startsWith (Pattern "foo") ""
520-
521-
log "endsWith"
522-
assert $ SCU.endsWith (Pattern "bar") "foobar"
523-
assert $ SCU.endsWith (Pattern "bar") "bar"
524-
assert $ SCU.endsWith (Pattern "") ""
525-
assert $ SCU.endsWith (Pattern "") "bar"
526-
assert $ not $ SCU.endsWith (Pattern "bar") ""

0 commit comments

Comments
 (0)