bytebuild/src-checked/Op.hs

46 lines
1.8 KiB
Haskell
Raw Normal View History

{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
2022-08-01 16:40:16 -04:00
module Op
( writeCharArray#
2022-08-05 09:03:52 -04:00
, copyByteArray#
, copyMutableByteArray#
2022-08-01 16:40:16 -04:00
) where
import GHC.Exts (ByteArray#, Char#, Int#, MutableByteArray#, State#, (<#), (>=#))
2022-08-01 16:40:16 -04:00
import qualified GHC.Exts as Exts
import GHC.Int (Int (I#))
2022-08-01 16:40:16 -04:00
2022-08-05 09:03:52 -04:00
writeCharArray# :: MutableByteArray# s -> Int# -> Char# -> State# s -> State# s
2022-08-01 16:40:16 -04:00
writeCharArray# arr i v st = case i <# 0# of
1# -> error ("writeCharArray#: negative index " ++ show (I# i))
_ -> case Exts.getSizeofMutableByteArray# arr st of
(# st', sz #) -> case i >=# sz of
1# -> error ("writeCharArray#: index " ++ show (I# i) ++ " >= length " ++ show (I# sz))
_ -> Exts.writeCharArray# arr i v st'
2022-08-05 09:03:52 -04:00
copyByteArray# :: ByteArray# -> Int# -> MutableByteArray# s -> Int# -> Int# -> State# s -> State# s
copyByteArray# src soff dst doff len s0 =
case Exts.getSizeofMutableByteArray# dst s0 of
(# s1, sz #)
| I# soff >= 0
, I# doff >= 0
, I# len >= 0
, I# doff + I# len <= I# sz
, I# soff + I# len <= I# (Exts.sizeofByteArray# src) ->
Exts.copyByteArray# src soff dst doff len s1
2022-08-05 09:03:52 -04:00
| otherwise -> error "copyByteArray#: index range out of bounds"
copyMutableByteArray# :: MutableByteArray# s -> Int# -> MutableByteArray# s -> Int# -> Int# -> State# s -> State# s
copyMutableByteArray# src soff dst doff len s0 =
case Exts.getSizeofMutableByteArray# dst s0 of
(# s1, szDst #) -> case Exts.getSizeofMutableByteArray# src s1 of
(# s2, szSrc #)
| I# soff >= 0
, I# doff >= 0
, I# len >= 0
, I# doff + I# len <= I# szDst
, I# soff + I# len <= I# szSrc ->
Exts.copyMutableByteArray# src soff dst doff len s2
2022-08-05 09:03:52 -04:00
| otherwise -> error "copyMutableByteArray#: index range out of bounds"