Skip to content

Commit d9ba5d0

Browse files
Relax Data.String.CodeUnits.slice bounds checking and drop Maybe in return type (#145)
* For Data.String.CodeUnits.slice: remove bounds checking and drop `Maybe` in return type * Add changelog entry Co-authored-by: JordanMartinez <[email protected]>
1 parent a8e757f commit d9ba5d0

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88
- Migrate FFI to ES modules (#158 by @kl0tl and @JordanMartinez)
99
- Replaced polymorphic proxies with monomorphic `Proxy` (#158 by @JordanMartinez)
10+
- In `slice`, drop bounds checking and `Maybe` return type (#145 by Quelklef)
1011

1112
New features:
1213

Diff for: src/Data/String/CodeUnits.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export const drop = function (n) {
101101
};
102102
};
103103

104-
export const _slice = function (b) {
104+
export const slice = function (b) {
105105
return function (e) {
106106
return function (s) {
107107
return s.slice(b,e);

Diff for: src/Data/String/CodeUnits.purs

+7-20
Original file line numberDiff line numberDiff line change
@@ -298,30 +298,17 @@ dropWhile p s = drop (countPrefix p s) s
298298

299299
-- | Returns the substring at indices `[begin, end)`.
300300
-- | If either index is negative, it is normalised to `length s - index`,
301-
-- | where `s` is the input string. `Nothing` is returned if either
301+
-- | where `s` is the input string. `""` is returned if either
302302
-- | index is out of bounds or if `begin > end` after normalisation.
303303
-- |
304304
-- | ```purescript
305-
-- | slice 0 0 "purescript" == Just ""
306-
-- | slice 0 1 "purescript" == Just "p"
307-
-- | slice 3 6 "purescript" == Just "esc"
308-
-- | slice (-4) (-1) "purescript" == Just "rip"
309-
-- | slice (-4) 3 "purescript" == Nothing
305+
-- | slice 0 0 "purescript" == ""
306+
-- | slice 0 1 "purescript" == "p"
307+
-- | slice 3 6 "purescript" == "esc"
308+
-- | slice (-4) (-1) "purescript" == "rip"
309+
-- | slice (-4) 3 "purescript" == ""
310310
-- | ```
311-
slice :: Int -> Int -> String -> Maybe String
312-
slice b e s = if b' < 0 || b' >= l ||
313-
e' < 0 || e' > l ||
314-
b' > e'
315-
then Nothing
316-
else Just (_slice b e s)
317-
where
318-
l = length s
319-
norm x | x < 0 = l + x
320-
| otherwise = x
321-
b' = norm b
322-
e' = norm e
323-
324-
foreign import _slice :: Int -> Int -> String -> String
311+
foreign import slice :: Int -> Int -> String -> String
325312

326313
-- | Splits a string into two substrings, where `before` contains the
327314
-- | characters up to (but not including) the given index, and `after` contains

Diff for: test/Test/Data/String/CodeUnits.purs

+14-10
Original file line numberDiff line numberDiff line change
@@ -472,41 +472,45 @@ testStringCodeUnits = do
472472
log "slice"
473473
assertEqual
474474
{ actual: SCU.slice 0 0 "purescript"
475-
, expected: Just ""
475+
, expected: ""
476476
}
477477
assertEqual
478478
{ actual: SCU.slice 0 1 "purescript"
479-
, expected: Just "p"
479+
, expected: "p"
480480
}
481481
assertEqual
482482
{ actual: SCU.slice 3 6 "purescript"
483-
, expected: Just "esc"
483+
, expected: "esc"
484484
}
485485
assertEqual
486486
{ actual: SCU.slice 3 10 "purescript"
487-
, expected: Just "escript"
487+
, expected: "escript"
488+
}
489+
assertEqual
490+
{ actual: SCU.slice 10 10 "purescript"
491+
, expected: ""
488492
}
489493
assertEqual
490494
{ actual: SCU.slice (-4) (-1) "purescript"
491-
, expected: Just "rip"
495+
, expected: "rip"
492496
}
493497
assertEqual
494498
{ actual: SCU.slice (-4) 3 "purescript"
495-
, expected: Nothing -- b' > e'
499+
, expected: ""
496500
}
497501
assertEqual
498502
{ actual: SCU.slice 1000 3 "purescript"
499-
, expected: Nothing -- b' > e' (subsumes b > l)
503+
, expected: ""
500504
}
501505
assertEqual
502506
{ actual: SCU.slice 2 (-15) "purescript"
503-
, expected: Nothing -- e' < 0
507+
, expected: ""
504508
}
505509
assertEqual
506510
{ actual: SCU.slice (-15) 9 "purescript"
507-
, expected: Nothing -- b' < 0
511+
, expected: "purescrip"
508512
}
509513
assertEqual
510514
{ actual: SCU.slice 3 1000 "purescript"
511-
, expected: Nothing -- e > l
515+
, expected: "escript"
512516
}

0 commit comments

Comments
 (0)