File tree Expand file tree Collapse file tree 3 files changed +66
-24
lines changed Expand file tree Collapse file tree 3 files changed +66
-24
lines changed Original file line number Diff line number Diff line change @@ -44,10 +44,9 @@ import Data.String.Unsafe as U
4444-- | stripPrefix (Pattern "http:") "https://purescript.org" == Nothing
4545-- | ```
4646stripPrefix :: Pattern -> String -> Maybe String
47- stripPrefix prefix@(Pattern prefixS) str =
48- case indexOf prefix str of
49- Just 0 -> Just $ drop (length prefixS) str
50- _ -> Nothing
47+ stripPrefix (Pattern prefix) str =
48+ let { before, after } = splitAt (length prefix) str in
49+ if before == prefix then Just after else Nothing
5150
5251-- | If the string ends with the given suffix, return the portion of the
5352-- | string left after removing it, as a `Just` value. Otherwise, return
@@ -58,10 +57,9 @@ stripPrefix prefix@(Pattern prefixS) str =
5857-- | stripSuffix (Pattern ".exe") "psc" == Nothing
5958-- | ```
6059stripSuffix :: Pattern -> String -> Maybe String
61- stripSuffix suffix@(Pattern suffixS) str =
62- case lastIndexOf suffix str of
63- Just x | x == length str - length suffixS -> Just $ take x str
64- _ -> Nothing
60+ stripSuffix (Pattern suffix) str =
61+ let { before, after } = splitAt (length str - length suffix) str in
62+ if after == suffix then Just before else Nothing
6563
6664-- | Checks whether the pattern appears in the given string.
6765-- |
Original file line number Diff line number Diff line change @@ -17,25 +17,17 @@ testString = do
1717 assert $ not (S .null " a" )
1818
1919 log " stripPrefix"
20+ -- this is a re-export from Data.String.CodeUnits, so the majority of tests are in there
2021 assertEqual
21- { actual: S .stripPrefix (Pattern " " ) " "
22- , expected: Just " "
23- }
24- assertEqual
25- { actual: S .stripPrefix (Pattern " " ) " abc"
26- , expected: Just " abc"
27- }
28- assertEqual
29- { actual: S .stripPrefix (Pattern " a" ) " abc"
30- , expected: Just " bc"
31- }
32- assertEqual
33- { actual: S .stripPrefix (Pattern " !" ) " abc"
34- , expected: Nothing
22+ { actual: S .stripPrefix (Pattern " 𝕒𝕓𝕔" ) " 𝕒𝕓𝕔𝕕𝕖"
23+ , expected: Just " 𝕕𝕖"
3524 }
25+
26+ log " stripSuffix"
27+ -- this is a re-export from Data.String.CodeUnits, so the majority of tests are in there
3628 assertEqual
37- { actual: S .stripPrefix (Pattern " ! " ) " "
38- , expected: Nothing
29+ { actual: S .stripSuffix (Pattern " 𝕔𝕕𝕖 " ) " 𝕒𝕓𝕔𝕕𝕖 "
30+ , expected: Just " 𝕒𝕓 "
3931 }
4032
4133 log " contains"
Original file line number Diff line number Diff line change @@ -12,6 +12,58 @@ import Test.Assert (assert, assertEqual)
1212
1313testStringCodeUnits :: Effect Unit
1414testStringCodeUnits = do
15+ log " stripPrefix"
16+ assertEqual
17+ { actual: SCU .stripPrefix (Pattern " abc" ) " abcde"
18+ , expected: Just " de"
19+ }
20+ assertEqual
21+ { actual: SCU .stripPrefix (Pattern " xyz" ) " abcde"
22+ , expected: Nothing
23+ }
24+ assertEqual
25+ { actual: SCU .stripPrefix (Pattern " abcd" ) " ab"
26+ , expected: Nothing
27+ }
28+ assertEqual
29+ { actual: SCU .stripPrefix (Pattern " abc" ) " abc"
30+ , expected: Just " "
31+ }
32+ assertEqual
33+ { actual: SCU .stripPrefix (Pattern " " ) " abc"
34+ , expected: Just " abc"
35+ }
36+ assertEqual
37+ { actual: SCU .stripPrefix (Pattern " " ) " "
38+ , expected: Just " "
39+ }
40+
41+ log " stripSuffix"
42+ assertEqual
43+ { actual: SCU .stripSuffix (Pattern " cde" ) " abcde"
44+ , expected: Just " ab"
45+ }
46+ assertEqual
47+ { actual: SCU .stripSuffix (Pattern " xyz" ) " abcde"
48+ , expected: Nothing
49+ }
50+ assertEqual
51+ { actual: SCU .stripSuffix (Pattern " abcd" ) " cd"
52+ , expected: Nothing
53+ }
54+ assertEqual
55+ { actual: SCU .stripSuffix (Pattern " abc" ) " abc"
56+ , expected: Just " "
57+ }
58+ assertEqual
59+ { actual: SCU .stripSuffix (Pattern " " ) " abc"
60+ , expected: Just " abc"
61+ }
62+ assertEqual
63+ { actual: SCU .stripSuffix (Pattern " " ) " "
64+ , expected: Just " "
65+ }
66+
1567 log " charAt"
1668 assertEqual
1769 { actual: SCU .charAt 0 " "
You can’t perform that action at this time.
0 commit comments