Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

expose copy operation for Boxed Array and Strings #97

Merged
merged 1 commit into from
Sep 3, 2016
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
5 changes: 5 additions & 0 deletions Foundation/Array/Boxed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
module Foundation.Array.Boxed
( Array
, MArray
, copy
) where

import GHC.Prim
Expand Down Expand Up @@ -226,6 +227,10 @@ freeze marray = do
where
sz = Size $ mutableLength marray

-- | Copy the element to a new element array
copy :: Array ty -> Array ty
copy a = runST (unsafeThaw a >>= freeze)

-- | Copy a number of elements from an array to another array with offsets
copyAt :: PrimMonad prim
=> MArray ty (PrimState prim) -- ^ destination array
Expand Down
11 changes: 8 additions & 3 deletions Foundation/String/UTF8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Foundation.String.UTF8
, fromBytesLenient
, toBytes
, mutableValidate
, copy
, ValidationFailure(..)
-- * Legacy utility
, lines
Expand Down Expand Up @@ -461,13 +462,13 @@ sToList s = loop azero
#-}

sFromList :: [Char] -> String
sFromList l = runST (new bytes >>= copy)
sFromList l = runST (new bytes >>= startCopy)
where
-- count how many bytes
!bytes = C.foldl' (+) (Size 0) $ fmap (charToBytes . fromEnum) l

copy :: MutableString (PrimState (ST st)) -> ST st String
copy ms = loop azero l
startCopy :: MutableString (PrimState (ST st)) -> ST st String
startCopy ms = loop azero l
where
loop _ [] = freeze ms
loop idx (c:xs) = write ms idx c >>= \idx' -> loop idx' xs
Expand Down Expand Up @@ -671,6 +672,10 @@ sizeBytes :: String -> Int
sizeBytes (String ba) = I# (sizeofByteArray# ba)
-}

-- | Copy the String
copy :: String -> String
copy (String s) = String (Vec.copy s)

-- | Allocate a MutableString of a specific size in bytes.
new :: PrimMonad prim
=> Size8 -- ^ in number of bytes, not of elements.
Expand Down