Commit e5357e8d authored by Bodigrim's avatar Bodigrim
Browse files

Rename constructors in Data.Array to highlight compatibility issues in downstream packages

parent 4b3667e0
......@@ -55,14 +55,10 @@ import GHC.Word (Word8(..))
import Prelude hiding (length, read)
-- | Immutable array type.
--
-- The 'Array' constructor is exposed since @text-1.1.1.3@
data Array = Array { aBA :: ByteArray# }
data Array = ByteArray ByteArray#
-- | Mutable array type, for use in the ST monad.
--
-- The 'MArray' constructor is exposed since @text-1.1.1.3@
data MArray s = MArray { maBA :: MutableByteArray# s }
data MArray s = MutableByteArray (MutableByteArray# s)
-- | Create an uninitialized mutable array.
new :: forall s. Int -> ST s (MArray s)
......@@ -70,7 +66,7 @@ new n
| n < 0 || n .&. highBit /= 0 = array_size_error
| otherwise = ST $ \s1# ->
case newByteArray# len# s1# of
(# s2#, marr# #) -> (# s2#, MArray marr# #)
(# s2#, marr# #) -> (# s2#, MutableByteArray marr# #)
where !(I# len#) = bytesInArray n
highBit = maxBound `xor` (maxBound `shiftR` 1)
{-# INLINE new #-}
......@@ -80,9 +76,9 @@ array_size_error = error "Data.Text.Array.new: size overflow"
-- | Freeze a mutable array. Do not mutate the 'MArray' afterwards!
unsafeFreeze :: MArray s -> ST s Array
unsafeFreeze MArray{..} = ST $ \s1# ->
case unsafeFreezeByteArray# maBA s1# of
(# s2#, ba# #) -> (# s2#, Array ba# #)
unsafeFreeze (MutableByteArray marr) = ST $ \s1# ->
case unsafeFreezeByteArray# marr s1# of
(# s2#, ba# #) -> (# s2#, ByteArray ba# #)
{-# INLINE unsafeFreeze #-}
-- | Indicate how many bytes would be used for an array of the given
......@@ -98,22 +94,22 @@ unsafeIndex ::
HasCallStack =>
#endif
Array -> Int -> Word8
unsafeIndex a@Array{..} i@(I# i#) =
unsafeIndex (ByteArray arr) i@(I# i#) =
#if defined(ASSERTS)
let word8len = I# (sizeofByteArray# aBA) in
let word8len = I# (sizeofByteArray# arr) in
if i < 0 || i >= word8len
then error ("Data.Text.Array.unsafeIndex: bounds error, offset " ++ show i ++ ", length " ++ show word8len)
else
#endif
case indexWord8Array# aBA i# of r# -> (W8# r#)
case indexWord8Array# arr i# of r# -> (W8# r#)
{-# INLINE unsafeIndex #-}
#if defined(ASSERTS)
-- sizeofMutableByteArray# is deprecated, because it is unsafe in the presence of
-- shrinkMutableByteArray# and resizeMutableByteArray#.
getSizeofMArray :: MArray s -> ST s Int
getSizeofMArray ma@MArray{..} = ST $ \s0# ->
case getSizeofMutableByteArray# maBA s0# of
getSizeofMArray (MutableByteArray marr) = ST $ \s0# ->
case getSizeofMutableByteArray# marr s0# of
(# s1#, word8len# #) -> (# s1#, I# word8len# #)
checkBoundsM :: HasCallStack => MArray s -> Int -> Int -> ST s ()
......@@ -131,11 +127,11 @@ unsafeWrite ::
HasCallStack =>
#endif
MArray s -> Int -> Word8 -> ST s ()
unsafeWrite ma@MArray{..} i@(I# i#) (W8# e#) =
unsafeWrite ma@(MutableByteArray marr) i@(I# i#) (W8# e#) =
#if defined(ASSERTS)
checkBoundsM ma i 1 >>
#endif
(ST $ \s1# -> case writeWord8Array# maBA i# e# s1# of
(ST $ \s1# -> case writeWord8Array# marr i# e# s1# of
s2# -> (# s2#, () #))
{-# INLINE unsafeWrite #-}
......@@ -170,7 +166,7 @@ copyM :: MArray s -- ^ Destination
-> Int -- ^ Source offset
-> Int -- ^ Count
-> ST s ()
copyM dst@(MArray dst#) dstOff@(I# dstOff#) src@(MArray src#) srcOff@(I# srcOff#) count@(I# count#)
copyM dst@(MutableByteArray dst#) dstOff@(I# dstOff#) src@(MutableByteArray src#) srcOff@(I# srcOff#) count@(I# count#)
| I# count# <= 0 = return ()
| otherwise = do
#if defined(ASSERTS)
......@@ -191,7 +187,7 @@ copyI :: MArray s -- ^ Destination
-> Int -- ^ First offset in destination /not/ to
-- copy (i.e. /not/ length)
-> ST s ()
copyI (MArray dst#) dstOff@(I# dstOff#) (Array src#) (I# srcOff#) top@(I# top#)
copyI (MutableByteArray dst#) dstOff@(I# dstOff#) (ByteArray src#) (I# srcOff#) top@(I# top#)
| dstOff >= top = return ()
| otherwise = ST $ \s1# ->
case copyByteArray# src# srcOff# dst# dstOff# (top# -# dstOff#) s1# of
......@@ -206,7 +202,7 @@ equal :: Array -- ^ First
-> Int -- ^ Offset into second
-> Int -- ^ Count
-> Bool
equal (Array src1#) (I# off1#) (Array src2#) (I# off2#) (I# count#) = i == 0
equal (ByteArray src1#) (I# off1#) (ByteArray src2#) (I# off2#) (I# count#) = i == 0
where
#if MIN_VERSION_base(4,11,0)
i = I# (compareByteArrays# src1# off1# src2# off2# count#)
......
......@@ -127,9 +127,9 @@ decodeLatin1 bs = withBS bs aux where
aux fp len = text a 0 actualLen
where
(a, actualLen) = A.run2 (A.new (2 * len) >>= unsafeIOToST . go)
go dest = unsafeWithForeignPtr fp $ \src -> do
destLen <- c_decode_latin1 (A.maBA dest) src (src `plusPtr` len)
return (dest, destLen)
go (A.MutableByteArray dest) = unsafeWithForeignPtr fp $ \src -> do
destLen <- c_decode_latin1 dest src (src `plusPtr` len)
return (A.MutableByteArray dest, destLen)
-- | Decode a 'ByteString' containing UTF-8 encoded text.
--
......@@ -147,15 +147,15 @@ decodeUtf8With ::
decodeUtf8With onErr bs = withBS bs aux
where
aux fp len = runText $ \done -> do
let go dest = unsafeWithForeignPtr fp $ \ptr ->
let go (A.MutableByteArray dest) = unsafeWithForeignPtr fp $ \ptr ->
with (0::CSize) $ \destOffPtr -> do
let end = ptr `plusPtr` len
loop curPtr = do
curPtr' <- c_decode_utf8 (A.maBA dest) destOffPtr curPtr end
curPtr' <- c_decode_utf8 dest destOffPtr curPtr end
if curPtr' == end
then do
n <- peek destOffPtr
unsafeSTToIO (done dest (cSizeToInt n))
unsafeSTToIO (done (A.MutableByteArray dest) (cSizeToInt n))
else do
x <- peek curPtr'
case onErr desc (Just x) of
......@@ -167,7 +167,7 @@ decodeUtf8With onErr bs = withBS bs aux
| otherwise -> do
destOff <- peek destOffPtr
w <- unsafeSTToIO $
unsafeWrite dest (cSizeToInt destOff)
unsafeWrite (A.MutableByteArray dest) (cSizeToInt destOff)
(safe c)
poke destOffPtr (destOff + intToCSize w)
loop $ curPtr' `plusPtr` 1
......@@ -288,7 +288,7 @@ streamDecodeUtf8With onErr = decodeChunk B.empty 0 0
aux fp len = runST $ (unsafeIOToST . decodeChunkToBuffer) =<< A.new (len+100)
where
decodeChunkToBuffer :: A.MArray s -> IO Decoding
decodeChunkToBuffer dest = unsafeWithForeignPtr fp $ \ptr ->
decodeChunkToBuffer (A.MutableByteArray dest) = unsafeWithForeignPtr fp $ \ptr ->
with (0::CSize) $ \destOffPtr ->
with codepoint0 $ \codepointPtr ->
with state0 $ \statePtr ->
......@@ -297,7 +297,7 @@ streamDecodeUtf8With onErr = decodeChunk B.empty 0 0
loop curPtr = do
prevState <- peek statePtr
poke curPtrPtr curPtr
lastPtr <- c_decode_utf8_with_state (A.maBA dest) destOffPtr
lastPtr <- c_decode_utf8_with_state dest destOffPtr
curPtrPtr end codepointPtr statePtr
state <- peek statePtr
case state of
......@@ -309,7 +309,7 @@ streamDecodeUtf8With onErr = decodeChunk B.empty 0 0
Just c -> do
destOff <- peek destOffPtr
w <- unsafeSTToIO $
unsafeWrite dest (cSizeToInt destOff) (safe c)
unsafeWrite (A.MutableByteArray dest) (cSizeToInt destOff) (safe c)
poke destOffPtr (destOff + intToCSize w)
if ptr == lastPtr && prevState /= UTF8_ACCEPT then do
-- If we can't complete the sequence @undecoded0@ from
......@@ -327,7 +327,7 @@ streamDecodeUtf8With onErr = decodeChunk B.empty 0 0
n <- peek destOffPtr
codepoint <- peek codepointPtr
chunkText <- unsafeSTToIO $ do
arr <- A.unsafeFreeze dest
arr <- A.unsafeFreeze (A.MutableByteArray dest)
return $! text arr 0 (cSizeToInt n)
let left = lastPtr `minusPtr` ptr
!undecoded = case state of
......@@ -434,7 +434,7 @@ encodeUtf8BuilderEscaped be =
-- | Encode text using UTF-8 encoding.
encodeUtf8 :: Text -> ByteString
encodeUtf8 (Text (A.Array arr) off len)
encodeUtf8 (Text (A.ByteArray arr) off len)
| len == 0 = B.empty
| otherwise = B.take len $ B.drop off $ SBS.fromShort $ SBS.SBS arr
......
-- | Regression tests for specific bugs.
--
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
......@@ -127,15 +128,15 @@ t280_singleton =
t301 :: IO ()
t301 = do
assertEqual "The length of the array remains the same despite slicing"
(I# (sizeofByteArray# (TA.aBA originalArr)))
(I# (sizeofByteArray# (TA.aBA newArr)))
(I# (sizeofByteArray# originalArr))
(I# (sizeofByteArray# newArr))
assertEqual "The new array still contains the original value"
(T.Text newArr originalOff originalLen)
(T.Text (TA.ByteArray newArr) originalOff originalLen)
original
where
original@(T.Text originalArr originalOff originalLen) = T.pack "1234567890"
T.Text newArr _off _len = T.take 1 $ T.drop 1 original
!original@(T.Text (TA.ByteArray originalArr) originalOff originalLen) = T.pack "1234567890"
!(T.Text (TA.ByteArray newArr) _off _len) = T.take 1 $ T.drop 1 original
t330 :: IO ()
t330 = do
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment