Skip to content

Remove the bounds check from the foreign implementation of lastIndexOf' #137

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

Merged
merged 2 commits into from
Dec 19, 2020
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: 13 additions & 1 deletion src/Data/String/CodePoints.purs
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,23 @@ lastIndexOf p s = (\i -> length (CU.take i s)) <$> CU.lastIndexOf p s

-- | Returns the number of code points preceding the first match of the given
-- | pattern in the string. Pattern matches following the given index will be
-- | ignored. Returns Nothing when no matches are found.
-- | ignored.
-- |
-- | Giving a negative index is equivalent to giving 0 and giving an index
-- | greater than the number of code points in the string is equivalent to
-- | searching in the whole string.
-- |
-- | Returns Nothing when no matches are found.
-- |
-- | ```purescript
-- | >>> lastIndexOf' (Pattern "𝐀") (-1) "b 𝐀𝐀 c 𝐀"
-- | Nothing
-- | >>> lastIndexOf' (Pattern "𝐀") 0 "b 𝐀𝐀 c 𝐀"
-- | Nothing
-- | >>> lastIndexOf' (Pattern "𝐀") 5 "b 𝐀𝐀 c 𝐀"
-- | Just 3
-- | >>> lastIndexOf' (Pattern "𝐀") 8 "b 𝐀𝐀 c 𝐀"
-- | Just 7
-- | >>> lastIndexOf' (Pattern "o") 5 "b 𝐀𝐀 c 𝐀"
-- | Nothing
-- | ```
Expand Down
1 change: 0 additions & 1 deletion src/Data/String/CodeUnits.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ exports._lastIndexOfStartingAt = function (just) {
return function (x) {
return function (startAt) {
return function (s) {
if (startAt < 0 || startAt > s.length) return nothing;
var i = s.lastIndexOf(x, startAt);
return i === -1 ? nothing : just(i);
};
Expand Down
11 changes: 9 additions & 2 deletions src/Data/String/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,21 @@ foreign import _lastIndexOf
-> Maybe Int

-- | Returns the index of the last occurrence of the pattern in the
-- | given string, starting at the specified index
-- | and searching backwards towards the beginning of the string.
-- | given string, starting at the specified index and searching
-- | backwards towards the beginning of the string.
-- |
-- | Starting at a negative index is equivalent to starting at 0 and
-- | starting at an index greater than the string length is equivalent
-- | to searching in the whole string.
-- |
-- | Returns `Nothing` if there is no match.
-- |
-- | ```purescript
-- | lastIndexOf' (Pattern "a") (-1) "ababa" == Just 0
-- | lastIndexOf' (Pattern "a") 1 "ababa" == Just 0
-- | lastIndexOf' (Pattern "a") 3 "ababa" == Just 2
-- | lastIndexOf' (Pattern "a") 4 "ababa" == Just 4
-- | lastIndexOf' (Pattern "a") 5 "ababa" == Just 4
-- | ```
-- |
lastIndexOf' :: Pattern -> Int -> String -> Maybe Int
Expand Down
11 changes: 9 additions & 2 deletions src/Data/String/NonEmpty/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,21 @@ lastIndexOf :: Pattern -> NonEmptyString -> Maybe Int
lastIndexOf = liftS <<< CU.lastIndexOf

-- | Returns the index of the last occurrence of the pattern in the
-- | given string, starting at the specified index
-- | and searching backwards towards the beginning of the string.
-- | given string, starting at the specified index and searching
-- | backwards towards the beginning of the string.
-- |
-- | Starting at a negative index is equivalent to starting at 0 and
-- | starting at an index greater than the string length is equivalent
-- | to searching in the whole string.
-- |
-- | Returns `Nothing` if there is no match.
-- |
-- | ```purescript
-- | lastIndexOf' (Pattern "a") (-1) (NonEmptyString "ababa") == Just 0
-- | lastIndexOf' (Pattern "a") 1 (NonEmptyString "ababa") == Just 0
-- | lastIndexOf' (Pattern "a") 3 (NonEmptyString "ababa") == Just 2
-- | lastIndexOf' (Pattern "a") 4 (NonEmptyString "ababa") == Just 4
-- | lastIndexOf' (Pattern "a") 5 (NonEmptyString "ababa") == Just 4
-- | ```
lastIndexOf' :: Pattern -> Int -> NonEmptyString -> Maybe Int
lastIndexOf' pat = liftS <<< CU.lastIndexOf' pat
Expand Down
8 changes: 8 additions & 0 deletions test/Test/Data/String/CodePoints.purs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ testStringCodePoints = do
{ actual: SCP.lastIndexOf' (Pattern str) 1 str
, expected: Just 0
}
assertEqual
{ actual: SCP.lastIndexOf' (Pattern "a") (-1) str
, expected: Just 0
}
assertEqual
{ actual: SCP.lastIndexOf' (Pattern "a") 0 str
, expected: Just 0
Expand All @@ -374,6 +378,10 @@ testStringCodePoints = do
{ actual: SCP.lastIndexOf' (Pattern "a") 7 str
, expected: Just 0
}
assertEqual
{ actual: SCP.lastIndexOf' (Pattern "a") (SCP.length str) str
, expected: Just 0
}
assertEqual
{ actual: SCP.lastIndexOf' (Pattern "z") 0 str
, expected: Nothing
Expand Down
4 changes: 2 additions & 2 deletions test/Test/Data/String/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ testStringCodeUnits = do
}
assertEqual
{ actual: SCU.lastIndexOf' (Pattern "") (-1) "ab"
, expected: Nothing
, expected: Just 0
}
assertEqual
{ actual: SCU.lastIndexOf' (Pattern "") 0 "ab"
Expand All @@ -286,7 +286,7 @@ testStringCodeUnits = do
}
assertEqual
{ actual: SCU.lastIndexOf' (Pattern "") 3 "ab"
, expected: Nothing
, expected: Just 2
}
assertEqual
{ actual: SCU.lastIndexOf' (Pattern "bc") 0 "abcd"
Expand Down
4 changes: 2 additions & 2 deletions test/Test/Data/String/NonEmpty/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ testNonEmptyStringCodeUnits = do
log "lastIndexOf'"
assertEqual
{ actual: NESCU.lastIndexOf' (Pattern "") (-1) (nes (Proxy :: Proxy "ab"))
, expected: Nothing
, expected: Just 0
}
assertEqual
{ actual: NESCU.lastIndexOf' (Pattern "") 0 (nes (Proxy :: Proxy "ab"))
Expand All @@ -293,7 +293,7 @@ testNonEmptyStringCodeUnits = do
}
assertEqual
{ actual: NESCU.lastIndexOf' (Pattern "") 3 (nes (Proxy :: Proxy "ab"))
, expected: Nothing
, expected: Just 2
}
assertEqual
{ actual: NESCU.lastIndexOf' (Pattern "bc") 0 (nes (Proxy :: Proxy "abcd"))
Expand Down