Skip to content

Commit 7a94736

Browse files
authored
Remove the bounds check from the foreign implementation of lastIndexOf' (#137)
* Remove the bounds check from the foreign implementation of lastIndexOf' * Update lastIndexOf' documentation
1 parent f1082da commit 7a94736

File tree

7 files changed

+43
-10
lines changed

7 files changed

+43
-10
lines changed

src/Data/String/CodePoints.purs

+13-1
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,23 @@ lastIndexOf p s = (\i -> length (CU.take i s)) <$> CU.lastIndexOf p s
286286

287287
-- | Returns the number of code points preceding the first match of the given
288288
-- | pattern in the string. Pattern matches following the given index will be
289-
-- | ignored. Returns Nothing when no matches are found.
289+
-- | ignored.
290+
-- |
291+
-- | Giving a negative index is equivalent to giving 0 and giving an index
292+
-- | greater than the number of code points in the string is equivalent to
293+
-- | searching in the whole string.
294+
-- |
295+
-- | Returns Nothing when no matches are found.
290296
-- |
291297
-- | ```purescript
298+
-- | >>> lastIndexOf' (Pattern "𝐀") (-1) "b 𝐀𝐀 c 𝐀"
299+
-- | Nothing
300+
-- | >>> lastIndexOf' (Pattern "𝐀") 0 "b 𝐀𝐀 c 𝐀"
301+
-- | Nothing
292302
-- | >>> lastIndexOf' (Pattern "𝐀") 5 "b 𝐀𝐀 c 𝐀"
293303
-- | Just 3
304+
-- | >>> lastIndexOf' (Pattern "𝐀") 8 "b 𝐀𝐀 c 𝐀"
305+
-- | Just 7
294306
-- | >>> lastIndexOf' (Pattern "o") 5 "b 𝐀𝐀 c 𝐀"
295307
-- | Nothing
296308
-- | ```

src/Data/String/CodeUnits.js

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ exports._lastIndexOfStartingAt = function (just) {
8383
return function (x) {
8484
return function (startAt) {
8585
return function (s) {
86-
if (startAt < 0 || startAt > s.length) return nothing;
8786
var i = s.lastIndexOf(x, startAt);
8887
return i === -1 ? nothing : just(i);
8988
};

src/Data/String/CodeUnits.purs

+9-2
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,21 @@ foreign import _lastIndexOf
215215
-> Maybe Int
216216

217217
-- | Returns the index of the last occurrence of the pattern in the
218-
-- | given string, starting at the specified index
219-
-- | and searching backwards towards the beginning of the string.
218+
-- | given string, starting at the specified index and searching
219+
-- | backwards towards the beginning of the string.
220+
-- |
221+
-- | Starting at a negative index is equivalent to starting at 0 and
222+
-- | starting at an index greater than the string length is equivalent
223+
-- | to searching in the whole string.
224+
-- |
220225
-- | Returns `Nothing` if there is no match.
221226
-- |
222227
-- | ```purescript
228+
-- | lastIndexOf' (Pattern "a") (-1) "ababa" == Just 0
223229
-- | lastIndexOf' (Pattern "a") 1 "ababa" == Just 0
224230
-- | lastIndexOf' (Pattern "a") 3 "ababa" == Just 2
225231
-- | lastIndexOf' (Pattern "a") 4 "ababa" == Just 4
232+
-- | lastIndexOf' (Pattern "a") 5 "ababa" == Just 4
226233
-- | ```
227234
-- |
228235
lastIndexOf' :: Pattern -> Int -> String -> Maybe Int

src/Data/String/NonEmpty/CodeUnits.purs

+9-2
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,21 @@ lastIndexOf :: Pattern -> NonEmptyString -> Maybe Int
159159
lastIndexOf = liftS <<< CU.lastIndexOf
160160

161161
-- | Returns the index of the last occurrence of the pattern in the
162-
-- | given string, starting at the specified index
163-
-- | and searching backwards towards the beginning of the string.
162+
-- | given string, starting at the specified index and searching
163+
-- | backwards towards the beginning of the string.
164+
-- |
165+
-- | Starting at a negative index is equivalent to starting at 0 and
166+
-- | starting at an index greater than the string length is equivalent
167+
-- | to searching in the whole string.
168+
-- |
164169
-- | Returns `Nothing` if there is no match.
165170
-- |
166171
-- | ```purescript
172+
-- | lastIndexOf' (Pattern "a") (-1) (NonEmptyString "ababa") == Just 0
167173
-- | lastIndexOf' (Pattern "a") 1 (NonEmptyString "ababa") == Just 0
168174
-- | lastIndexOf' (Pattern "a") 3 (NonEmptyString "ababa") == Just 2
169175
-- | lastIndexOf' (Pattern "a") 4 (NonEmptyString "ababa") == Just 4
176+
-- | lastIndexOf' (Pattern "a") 5 (NonEmptyString "ababa") == Just 4
170177
-- | ```
171178
lastIndexOf' :: Pattern -> Int -> NonEmptyString -> Maybe Int
172179
lastIndexOf' pat = liftS <<< CU.lastIndexOf' pat

test/Test/Data/String/CodePoints.purs

+8
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ testStringCodePoints = do
366366
{ actual: SCP.lastIndexOf' (Pattern str) 1 str
367367
, expected: Just 0
368368
}
369+
assertEqual
370+
{ actual: SCP.lastIndexOf' (Pattern "a") (-1) str
371+
, expected: Just 0
372+
}
369373
assertEqual
370374
{ actual: SCP.lastIndexOf' (Pattern "a") 0 str
371375
, expected: Just 0
@@ -374,6 +378,10 @@ testStringCodePoints = do
374378
{ actual: SCP.lastIndexOf' (Pattern "a") 7 str
375379
, expected: Just 0
376380
}
381+
assertEqual
382+
{ actual: SCP.lastIndexOf' (Pattern "a") (SCP.length str) str
383+
, expected: Just 0
384+
}
377385
assertEqual
378386
{ actual: SCP.lastIndexOf' (Pattern "z") 0 str
379387
, expected: Nothing

test/Test/Data/String/CodeUnits.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ testStringCodeUnits = do
270270
}
271271
assertEqual
272272
{ actual: SCU.lastIndexOf' (Pattern "") (-1) "ab"
273-
, expected: Nothing
273+
, expected: Just 0
274274
}
275275
assertEqual
276276
{ actual: SCU.lastIndexOf' (Pattern "") 0 "ab"
@@ -286,7 +286,7 @@ testStringCodeUnits = do
286286
}
287287
assertEqual
288288
{ actual: SCU.lastIndexOf' (Pattern "") 3 "ab"
289-
, expected: Nothing
289+
, expected: Just 2
290290
}
291291
assertEqual
292292
{ actual: SCU.lastIndexOf' (Pattern "bc") 0 "abcd"

test/Test/Data/String/NonEmpty/CodeUnits.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ testNonEmptyStringCodeUnits = do
277277
log "lastIndexOf'"
278278
assertEqual
279279
{ actual: NESCU.lastIndexOf' (Pattern "") (-1) (nes (Proxy :: Proxy "ab"))
280-
, expected: Nothing
280+
, expected: Just 0
281281
}
282282
assertEqual
283283
{ actual: NESCU.lastIndexOf' (Pattern "") 0 (nes (Proxy :: Proxy "ab"))
@@ -293,7 +293,7 @@ testNonEmptyStringCodeUnits = do
293293
}
294294
assertEqual
295295
{ actual: NESCU.lastIndexOf' (Pattern "") 3 (nes (Proxy :: Proxy "ab"))
296-
, expected: Nothing
296+
, expected: Just 2
297297
}
298298
assertEqual
299299
{ actual: NESCU.lastIndexOf' (Pattern "bc") 0 (nes (Proxy :: Proxy "abcd"))

0 commit comments

Comments
 (0)