Skip to content

Commit 6fce657

Browse files
authored
Merge pull request purescript#104 from themattchan/master
add slice
2 parents bdf8c1b + e2d4b1d commit 6fce657

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/Data/String.js

+8
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ exports.drop = function (n) {
131131
};
132132
};
133133

134+
exports._slice = function (b) {
135+
return function (e) {
136+
return function (s) {
137+
return s.slice(b,e);
138+
};
139+
};
140+
};
141+
134142
exports.count = function (p) {
135143
return function (s) {
136144
var i = 0;

src/Data/String.purs

+28
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Data.String
2626
, drop
2727
, dropRight
2828
, dropWhile
29+
, slice
2930
, stripPrefix
3031
, stripSuffix
3132
, count
@@ -172,6 +173,33 @@ takeWhile p s = take (count p s) s
172173
dropWhile :: (Char -> Boolean) -> String -> String
173174
dropWhile p s = drop (count p s) s
174175

176+
-- | Returns the substring at indices `[begin, end)`.
177+
-- | If either index is negative, it is normalised to `length s - index`,
178+
-- | where `s` is the input string. `Nothing` is returned if either
179+
-- | index is out of bounds or if `begin > end` after normalisation.
180+
-- |
181+
-- | ```purescript
182+
-- | slice 0 0 "purescript" == Just ""
183+
-- | slice 0 1 "purescript" == Just "p"
184+
-- | slice 3 6 "purescript" == Just "esc"
185+
-- | slice (-4) (-1) "purescript" == Just "rip"
186+
-- | slice (-4) 3 "purescript" == Nothing
187+
-- | ```
188+
slice :: Int -> Int -> String -> Maybe String
189+
slice b e s = if b' < 0 || b' >= l ||
190+
e' < 0 || e' >= l ||
191+
b' > e'
192+
then Nothing
193+
else Just (_slice b e s)
194+
where
195+
l = length s
196+
norm x | x < 0 = l + x
197+
| otherwise = x
198+
b' = norm b
199+
e' = norm e
200+
201+
foreign import _slice :: Int -> Int -> String -> String
202+
175203
-- | If the string starts with the given prefix, return the portion of the
176204
-- | string left after removing it, as a Just value. Otherwise, return Nothing.
177205
-- |

test/Test/Data/String.purs

+11
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,14 @@ testString = do
206206
assert $ joinWith "" [] == ""
207207
assert $ joinWith "" ["a", "b"] == "ab"
208208
assert $ joinWith "--" ["a", "b", "c"] == "a--b--c"
209+
210+
log "slice"
211+
assert $ slice 0 0 "purescript" == Just ""
212+
assert $ slice 0 1 "purescript" == Just "p"
213+
assert $ slice 3 6 "purescript" == Just "esc"
214+
assert $ slice (-4) (-1) "purescript" == Just "rip"
215+
assert $ slice (-4) 3 "purescript" == Nothing -- b' > e'
216+
assert $ slice 1000 3 "purescript" == Nothing -- b' > e' (subsumes b > l)
217+
assert $ slice 2 (-15) "purescript" == Nothing -- e' < 0
218+
assert $ slice (-15) 9 "purescript" == Nothing -- b' < 0
219+
assert $ slice 3 1000 "purescript" == Nothing -- e > l

0 commit comments

Comments
 (0)