File tree 7 files changed +43
-10
lines changed
7 files changed +43
-10
lines changed Original file line number Diff line number Diff line change @@ -286,11 +286,23 @@ lastIndexOf p s = (\i -> length (CU.take i s)) <$> CU.lastIndexOf p s
286
286
287
287
-- | Returns the number of code points preceding the first match of the given
288
288
-- | 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.
290
296
-- |
291
297
-- | ```purescript
298
+ -- | >>> lastIndexOf' (Pattern "𝐀") (-1) "b 𝐀𝐀 c 𝐀"
299
+ -- | Nothing
300
+ -- | >>> lastIndexOf' (Pattern "𝐀") 0 "b 𝐀𝐀 c 𝐀"
301
+ -- | Nothing
292
302
-- | >>> lastIndexOf' (Pattern "𝐀") 5 "b 𝐀𝐀 c 𝐀"
293
303
-- | Just 3
304
+ -- | >>> lastIndexOf' (Pattern "𝐀") 8 "b 𝐀𝐀 c 𝐀"
305
+ -- | Just 7
294
306
-- | >>> lastIndexOf' (Pattern "o") 5 "b 𝐀𝐀 c 𝐀"
295
307
-- | Nothing
296
308
-- | ```
Original file line number Diff line number Diff line change @@ -83,7 +83,6 @@ exports._lastIndexOfStartingAt = function (just) {
83
83
return function ( x ) {
84
84
return function ( startAt ) {
85
85
return function ( s ) {
86
- if ( startAt < 0 || startAt > s . length ) return nothing ;
87
86
var i = s . lastIndexOf ( x , startAt ) ;
88
87
return i === - 1 ? nothing : just ( i ) ;
89
88
} ;
Original file line number Diff line number Diff line change @@ -215,14 +215,21 @@ foreign import _lastIndexOf
215
215
-> Maybe Int
216
216
217
217
-- | 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
+ -- |
220
225
-- | Returns `Nothing` if there is no match.
221
226
-- |
222
227
-- | ```purescript
228
+ -- | lastIndexOf' (Pattern "a") (-1) "ababa" == Just 0
223
229
-- | lastIndexOf' (Pattern "a") 1 "ababa" == Just 0
224
230
-- | lastIndexOf' (Pattern "a") 3 "ababa" == Just 2
225
231
-- | lastIndexOf' (Pattern "a") 4 "ababa" == Just 4
232
+ -- | lastIndexOf' (Pattern "a") 5 "ababa" == Just 4
226
233
-- | ```
227
234
-- |
228
235
lastIndexOf' :: Pattern -> Int -> String -> Maybe Int
Original file line number Diff line number Diff line change @@ -159,14 +159,21 @@ lastIndexOf :: Pattern -> NonEmptyString -> Maybe Int
159
159
lastIndexOf = liftS <<< CU .lastIndexOf
160
160
161
161
-- | 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
+ -- |
164
169
-- | Returns `Nothing` if there is no match.
165
170
-- |
166
171
-- | ```purescript
172
+ -- | lastIndexOf' (Pattern "a") (-1) (NonEmptyString "ababa") == Just 0
167
173
-- | lastIndexOf' (Pattern "a") 1 (NonEmptyString "ababa") == Just 0
168
174
-- | lastIndexOf' (Pattern "a") 3 (NonEmptyString "ababa") == Just 2
169
175
-- | lastIndexOf' (Pattern "a") 4 (NonEmptyString "ababa") == Just 4
176
+ -- | lastIndexOf' (Pattern "a") 5 (NonEmptyString "ababa") == Just 4
170
177
-- | ```
171
178
lastIndexOf' :: Pattern -> Int -> NonEmptyString -> Maybe Int
172
179
lastIndexOf' pat = liftS <<< CU .lastIndexOf' pat
Original file line number Diff line number Diff line change @@ -366,6 +366,10 @@ testStringCodePoints = do
366
366
{ actual: SCP .lastIndexOf' (Pattern str) 1 str
367
367
, expected: Just 0
368
368
}
369
+ assertEqual
370
+ { actual: SCP .lastIndexOf' (Pattern " a" ) (-1 ) str
371
+ , expected: Just 0
372
+ }
369
373
assertEqual
370
374
{ actual: SCP .lastIndexOf' (Pattern " a" ) 0 str
371
375
, expected: Just 0
@@ -374,6 +378,10 @@ testStringCodePoints = do
374
378
{ actual: SCP .lastIndexOf' (Pattern " a" ) 7 str
375
379
, expected: Just 0
376
380
}
381
+ assertEqual
382
+ { actual: SCP .lastIndexOf' (Pattern " a" ) (SCP .length str) str
383
+ , expected: Just 0
384
+ }
377
385
assertEqual
378
386
{ actual: SCP .lastIndexOf' (Pattern " z" ) 0 str
379
387
, expected: Nothing
Original file line number Diff line number Diff line change @@ -270,7 +270,7 @@ testStringCodeUnits = do
270
270
}
271
271
assertEqual
272
272
{ actual: SCU .lastIndexOf' (Pattern " " ) (-1 ) " ab"
273
- , expected: Nothing
273
+ , expected: Just 0
274
274
}
275
275
assertEqual
276
276
{ actual: SCU .lastIndexOf' (Pattern " " ) 0 " ab"
@@ -286,7 +286,7 @@ testStringCodeUnits = do
286
286
}
287
287
assertEqual
288
288
{ actual: SCU .lastIndexOf' (Pattern " " ) 3 " ab"
289
- , expected: Nothing
289
+ , expected: Just 2
290
290
}
291
291
assertEqual
292
292
{ actual: SCU .lastIndexOf' (Pattern " bc" ) 0 " abcd"
Original file line number Diff line number Diff line change @@ -277,7 +277,7 @@ testNonEmptyStringCodeUnits = do
277
277
log " lastIndexOf'"
278
278
assertEqual
279
279
{ actual: NESCU .lastIndexOf' (Pattern " " ) (-1 ) (nes (Proxy :: Proxy " ab" ))
280
- , expected: Nothing
280
+ , expected: Just 0
281
281
}
282
282
assertEqual
283
283
{ actual: NESCU .lastIndexOf' (Pattern " " ) 0 (nes (Proxy :: Proxy " ab" ))
@@ -293,7 +293,7 @@ testNonEmptyStringCodeUnits = do
293
293
}
294
294
assertEqual
295
295
{ actual: NESCU .lastIndexOf' (Pattern " " ) 3 (nes (Proxy :: Proxy " ab" ))
296
- , expected: Nothing
296
+ , expected: Just 2
297
297
}
298
298
assertEqual
299
299
{ actual: NESCU .lastIndexOf' (Pattern " bc" ) 0 (nes (Proxy :: Proxy " abcd" ))
You can’t perform that action at this time.
0 commit comments