File tree 3 files changed +66
-24
lines changed
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
44
44
-- | stripPrefix (Pattern "http:") "https://purescript.org" == Nothing
45
45
-- | ```
46
46
stripPrefix :: 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
51
50
52
51
-- | If the string ends with the given suffix, return the portion of the
53
52
-- | string left after removing it, as a `Just` value. Otherwise, return
@@ -58,10 +57,9 @@ stripPrefix prefix@(Pattern prefixS) str =
58
57
-- | stripSuffix (Pattern ".exe") "psc" == Nothing
59
58
-- | ```
60
59
stripSuffix :: 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
65
63
66
64
-- | Checks whether the pattern appears in the given string.
67
65
-- |
Original file line number Diff line number Diff line change @@ -17,25 +17,17 @@ testString = do
17
17
assert $ not (S .null " a" )
18
18
19
19
log " stripPrefix"
20
+ -- this is a re-export from Data.String.CodeUnits, so the majority of tests are in there
20
21
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 " 𝕕𝕖"
35
24
}
25
+
26
+ log " stripSuffix"
27
+ -- this is a re-export from Data.String.CodeUnits, so the majority of tests are in there
36
28
assertEqual
37
- { actual: S .stripPrefix (Pattern " ! " ) " "
38
- , expected: Nothing
29
+ { actual: S .stripSuffix (Pattern " 𝕔𝕕𝕖 " ) " 𝕒𝕓𝕔𝕕𝕖 "
30
+ , expected: Just " 𝕒𝕓 "
39
31
}
40
32
41
33
log " contains"
Original file line number Diff line number Diff line change @@ -12,6 +12,58 @@ import Test.Assert (assert, assertEqual)
12
12
13
13
testStringCodeUnits :: Effect Unit
14
14
testStringCodeUnits = 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
+
15
67
log " charAt"
16
68
assertEqual
17
69
{ actual: SCU .charAt 0 " "
You can’t perform that action at this time.
0 commit comments