Skip to content

Commit d69ea5f

Browse files
Make change for CodePoints too
1 parent 9f5d65b commit d69ea5f

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

src/Data/String/CodePoints.purs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,16 @@ singletonFallback (CodePoint cp) =
214214
fromCharCode lead <> fromCharCode trail
215215

216216

217-
-- | Returns a record with strings created from the code points on either side
218-
-- | of the given index. If the index is not within the string, Nothing is
219-
-- | returned.
220-
splitAt :: Int -> String -> Maybe { before :: String, after :: String }
217+
-- | Splits a string into two substrings, where `before` contains the code
218+
-- | points up to (but not including) the given index, and `after` contains the
219+
-- | rest of the string, from that index on.
220+
splitAt :: Int -> String -> { before :: String, after :: String }
221221
splitAt i s =
222-
let cps = toCodePointArray s in
223-
if i < 0 || Array.length cps < i
224-
then Nothing
225-
else Just {
226-
before: fromCodePointArray (Array.take i cps),
227-
after: fromCodePointArray (Array.drop i cps)
228-
}
229-
222+
let before = take i s in
223+
{ before
224+
-- inline drop i s to reuse the result of take i s
225+
, after: String.drop (String.length before) s
226+
}
230227

231228
-- | Returns a string containing the given number of code points from the
232229
-- | beginning of the given string. If the string does not have that many code

test/Test/Data/String/CodePoints.purs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,29 +137,26 @@ testStringCodePoints = do
137137
assert $ (singleton <$> codePointFromInt 0x16805) == Just "\x16805"
138138

139139
log "splitAt"
140-
let testSplitAt i s res =
140+
let testSplitAt i s r =
141141
assert $ case splitAt i s of
142-
Nothing ->
143-
isNothing res
144-
Just { before, after } ->
145-
maybe false (\r ->
146-
r.before == before && r.after == after) res
147-
148-
testSplitAt 0 "" $ Just {before: "", after: ""}
149-
testSplitAt 1 "" Nothing
150-
testSplitAt 0 "a" $ Just {before: "", after: "a"}
151-
testSplitAt 1 "ab" $ Just {before: "a", after: "b"}
152-
testSplitAt 3 "aabcc" $ Just {before: "aab", after: "cc"}
153-
testSplitAt (-1) "abc" $ Nothing
154-
testSplitAt 0 str $ Just {before: "", after: str}
155-
testSplitAt 1 str $ Just {before: "a", after: "\xDC00\xD800\xD800\x16805\x16A06\&z"}
156-
testSplitAt 2 str $ Just {before: "a\xDC00", after: "\xD800\xD800\x16805\x16A06\&z"}
157-
testSplitAt 3 str $ Just {before: "a\xDC00\xD800", after: "\xD800\x16805\x16A06\&z"}
158-
testSplitAt 4 str $ Just {before: "a\xDC00\xD800\xD800", after: "\x16805\x16A06\&z"}
159-
testSplitAt 5 str $ Just {before: "a\xDC00\xD800\xD800\x16805", after: "\x16A06\&z"}
160-
testSplitAt 6 str $ Just {before: "a\xDC00\xD800\xD800\x16805\x16A06", after: "z"}
161-
testSplitAt 7 str $ Just {before: str, after: ""}
162-
testSplitAt 8 str $ Nothing
142+
{ before, after } ->
143+
r.before == before && r.after == after
144+
145+
testSplitAt 0 "" {before: "", after: "" }
146+
testSplitAt 1 "" {before: "", after: "" }
147+
testSplitAt 0 "a" {before: "", after: "a"}
148+
testSplitAt 1 "ab" {before: "a", after: "b"}
149+
testSplitAt 3 "aabcc" {before: "aab", after: "cc"}
150+
testSplitAt (-1) "abc" {before: "", after: "abc"}
151+
testSplitAt 0 str {before: "", after: str}
152+
testSplitAt 1 str {before: "a", after: "\xDC00\xD800\xD800\x16805\x16A06\&z"}
153+
testSplitAt 2 str {before: "a\xDC00", after: "\xD800\xD800\x16805\x16A06\&z"}
154+
testSplitAt 3 str {before: "a\xDC00\xD800", after: "\xD800\x16805\x16A06\&z"}
155+
testSplitAt 4 str {before: "a\xDC00\xD800\xD800", after: "\x16805\x16A06\&z"}
156+
testSplitAt 5 str {before: "a\xDC00\xD800\xD800\x16805", after: "\x16A06\&z"}
157+
testSplitAt 6 str {before: "a\xDC00\xD800\xD800\x16805\x16A06", after: "z"}
158+
testSplitAt 7 str {before: str, after: ""}
159+
testSplitAt 8 str {before: str, after: ""}
163160

164161
log "take"
165162
assert $ take (-1) str == ""

0 commit comments

Comments
 (0)