From 5072890db74b62059e253ce4fd32b5fd3d7214b6 Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Fri, 2 Sep 2016 21:36:13 +0100 Subject: [PATCH] expose copy operation for Boxed Array and Strings --- Foundation/Array/Boxed.hs | 5 +++++ Foundation/String/UTF8.hs | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Foundation/Array/Boxed.hs b/Foundation/Array/Boxed.hs index df865ec2..7d41da92 100644 --- a/Foundation/Array/Boxed.hs +++ b/Foundation/Array/Boxed.hs @@ -13,6 +13,7 @@ module Foundation.Array.Boxed ( Array , MArray + , copy ) where import GHC.Prim @@ -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 diff --git a/Foundation/String/UTF8.hs b/Foundation/String/UTF8.hs index 3a91b17f..24da9a42 100644 --- a/Foundation/String/UTF8.hs +++ b/Foundation/String/UTF8.hs @@ -33,6 +33,7 @@ module Foundation.String.UTF8 , fromBytesLenient , toBytes , mutableValidate + , copy , ValidationFailure(..) -- * Legacy utility , lines @@ -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 @@ -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.