diff --git a/CHANGELOG.md b/CHANGELOG.md index d63cbd2..02cb63e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: - Migrate FFI to ES modules (#158 by @kl0tl and @JordanMartinez) - Replaced polymorphic proxies with monomorphic `Proxy` (#158 by @JordanMartinez) +- In `slice`, drop bounds checking and `Maybe` return type (#145 by Quelklef) New features: diff --git a/src/Data/String/CodeUnits.js b/src/Data/String/CodeUnits.js index 47d61f9..2608384 100644 --- a/src/Data/String/CodeUnits.js +++ b/src/Data/String/CodeUnits.js @@ -101,7 +101,7 @@ export const drop = function (n) { }; }; -export const _slice = function (b) { +export const slice = function (b) { return function (e) { return function (s) { return s.slice(b,e); diff --git a/src/Data/String/CodeUnits.purs b/src/Data/String/CodeUnits.purs index fbc1803..5fed21f 100644 --- a/src/Data/String/CodeUnits.purs +++ b/src/Data/String/CodeUnits.purs @@ -298,30 +298,17 @@ dropWhile p s = drop (countPrefix p s) s -- | Returns the substring at indices `[begin, end)`. -- | If either index is negative, it is normalised to `length s - index`, --- | where `s` is the input string. `Nothing` is returned if either +-- | where `s` is the input string. `""` is returned if either -- | index is out of bounds or if `begin > end` after normalisation. -- | -- | ```purescript --- | slice 0 0 "purescript" == Just "" --- | slice 0 1 "purescript" == Just "p" --- | slice 3 6 "purescript" == Just "esc" --- | slice (-4) (-1) "purescript" == Just "rip" --- | slice (-4) 3 "purescript" == Nothing +-- | slice 0 0 "purescript" == "" +-- | slice 0 1 "purescript" == "p" +-- | slice 3 6 "purescript" == "esc" +-- | slice (-4) (-1) "purescript" == "rip" +-- | slice (-4) 3 "purescript" == "" -- | ``` -slice :: Int -> Int -> String -> Maybe String -slice b e s = if b' < 0 || b' >= l || - e' < 0 || e' > l || - b' > e' - then Nothing - else Just (_slice b e s) - where - l = length s - norm x | x < 0 = l + x - | otherwise = x - b' = norm b - e' = norm e - -foreign import _slice :: Int -> Int -> String -> String +foreign import slice :: Int -> Int -> String -> String -- | Splits a string into two substrings, where `before` contains the -- | characters up to (but not including) the given index, and `after` contains diff --git a/test/Test/Data/String/CodeUnits.purs b/test/Test/Data/String/CodeUnits.purs index 9cf010b..ddd512b 100644 --- a/test/Test/Data/String/CodeUnits.purs +++ b/test/Test/Data/String/CodeUnits.purs @@ -472,41 +472,45 @@ testStringCodeUnits = do log "slice" assertEqual { actual: SCU.slice 0 0 "purescript" - , expected: Just "" + , expected: "" } assertEqual { actual: SCU.slice 0 1 "purescript" - , expected: Just "p" + , expected: "p" } assertEqual { actual: SCU.slice 3 6 "purescript" - , expected: Just "esc" + , expected: "esc" } assertEqual { actual: SCU.slice 3 10 "purescript" - , expected: Just "escript" + , expected: "escript" + } + assertEqual + { actual: SCU.slice 10 10 "purescript" + , expected: "" } assertEqual { actual: SCU.slice (-4) (-1) "purescript" - , expected: Just "rip" + , expected: "rip" } assertEqual { actual: SCU.slice (-4) 3 "purescript" - , expected: Nothing -- b' > e' + , expected: "" } assertEqual { actual: SCU.slice 1000 3 "purescript" - , expected: Nothing -- b' > e' (subsumes b > l) + , expected: "" } assertEqual { actual: SCU.slice 2 (-15) "purescript" - , expected: Nothing -- e' < 0 + , expected: "" } assertEqual { actual: SCU.slice (-15) 9 "purescript" - , expected: Nothing -- b' < 0 + , expected: "purescrip" } assertEqual { actual: SCU.slice 3 1000 "purescript" - , expected: Nothing -- e > l + , expected: "escript" }