Skip to content

[WIP] relax Data.String.CodeUnits.slice + related changes as per #143 #145

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 3 commits into from
Mar 25, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion src/Data/String/CodeUnits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 7 additions & 20 deletions src/Data/String/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions test/Test/Data/String/CodeUnits.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}