Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 6 additions & 8 deletions src/Data/String/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ import Data.String.Unsafe as U
-- | stripPrefix (Pattern "http:") "https://purescript.org" == Nothing
-- | ```
stripPrefix :: Pattern -> String -> Maybe String
stripPrefix prefix@(Pattern prefixS) str =
case indexOf prefix str of
Just 0 -> Just $ drop (length prefixS) str
_ -> Nothing
stripPrefix (Pattern prefix) str =
let { before, after } = splitAt (length prefix) str in
if before == prefix then Just after else Nothing

-- | If the string ends with the given suffix, return the portion of the
-- | string left after removing it, as a `Just` value. Otherwise, return
Expand All @@ -58,10 +57,9 @@ stripPrefix prefix@(Pattern prefixS) str =
-- | stripSuffix (Pattern ".exe") "psc" == Nothing
-- | ```
stripSuffix :: Pattern -> String -> Maybe String
stripSuffix suffix@(Pattern suffixS) str =
case lastIndexOf suffix str of
Just x | x == length str - length suffixS -> Just $ take x str
_ -> Nothing
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 pattern appears in the given string.
-- |
Expand Down
24 changes: 8 additions & 16 deletions test/Test/Data/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,17 @@ testString = do
assert $ not (S.null "a")

log "stripPrefix"
-- this is a re-export from Data.String.CodeUnits, so the majority of tests are in there
assertEqual
{ actual: S.stripPrefix (Pattern "") ""
, expected: Just ""
}
assertEqual
{ actual: S.stripPrefix (Pattern "") "abc"
, expected: Just "abc"
}
assertEqual
{ actual: S.stripPrefix (Pattern "a") "abc"
, expected: Just "bc"
}
assertEqual
{ actual: S.stripPrefix (Pattern "!") "abc"
, expected: Nothing
{ actual: S.stripPrefix (Pattern "𝕒𝕓𝕔") "𝕒𝕓𝕔𝕕𝕖"
, expected: Just "𝕕𝕖"
}

log "stripSuffix"
-- this is a re-export from Data.String.CodeUnits, so the majority of tests are in there
assertEqual
{ actual: S.stripPrefix (Pattern "!") ""
, expected: Nothing
{ actual: S.stripSuffix (Pattern "𝕔𝕕𝕖") "𝕒𝕓𝕔𝕕𝕖"
, expected: Just "𝕒𝕓"
}

log "contains"
Expand Down
52 changes: 52 additions & 0 deletions test/Test/Data/String/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,58 @@ import Test.Assert (assert, assertEqual)

testStringCodeUnits :: Effect Unit
testStringCodeUnits = do
log "stripPrefix"
assertEqual
{ actual: SCU.stripPrefix (Pattern "abc") "abcde"
, expected: Just "de"
}
assertEqual
{ actual: SCU.stripPrefix (Pattern "xyz") "abcde"
, expected: Nothing
}
assertEqual
{ actual: SCU.stripPrefix (Pattern "abcd") "ab"
, expected: Nothing
}
assertEqual
{ actual: SCU.stripPrefix (Pattern "abc") "abc"
, expected: Just ""
}
assertEqual
{ actual: SCU.stripPrefix (Pattern "") "abc"
, expected: Just "abc"
}
assertEqual
{ actual: SCU.stripPrefix (Pattern "") ""
, expected: Just ""
}

log "stripSuffix"
assertEqual
{ actual: SCU.stripSuffix (Pattern "cde") "abcde"
, expected: Just "ab"
}
assertEqual
{ actual: SCU.stripSuffix (Pattern "xyz") "abcde"
, expected: Nothing
}
assertEqual
{ actual: SCU.stripSuffix (Pattern "abcd") "cd"
, expected: Nothing
}
assertEqual
{ actual: SCU.stripSuffix (Pattern "abc") "abc"
, expected: Just ""
}
assertEqual
{ actual: SCU.stripSuffix (Pattern "") "abc"
, expected: Just "abc"
}
assertEqual
{ actual: SCU.stripSuffix (Pattern "") ""
, expected: Just ""
}

log "charAt"
assertEqual
{ actual: SCU.charAt 0 ""
Expand Down