import ArrBase
import Maybe
+# if __GLASGOW_HASKELL__ >= 209
+import GlaExts ( thenST, returnST )
+# endif
+
#else
#define ARR_ELT (:=)
#if __GLASGOW_HASKELL__ >= 206
import PackBase
#endif
+#if __GLASGOW_HASKELL__ >= 209
+import Addr
+import IORef
+# define newVar newIORef
+# define readVar readIORef
+# define writeVar writeIORef
+#endif
+
#endif
import PrimPacked
Int#
(MutableArray# _RealWorld [FastString])
+#if __GLASGOW_HASKELL__ < 209
type FastStringTableVar = MutableVar _RealWorld FastStringTable
+#else
+type FastStringTableVar = IORef FastStringTable
+#endif
string_table :: FastStringTableVar
string_table =
unsafePerformPrimIO (
- newArray (0::Int,hASH_TBL_SIZE) [] `thenPrimIO` \ (_MutableArray _ arr#) ->
+ ST_TO_PrimIO (newArray (0::Int,hASH_TBL_SIZE) []) `thenPrimIO` \ (_MutableArray _ arr#) ->
newVar (FastStringTable 0# arr#))
lookupTbl :: FastStringTable -> Int# -> PrimIO [FastString]
lookupTbl (FastStringTable _ arr#) i# =
- MkST ( \ (S# s#) ->
+ ST_TO_PrimIO (
+ MkST ( \ STATE_TOK(s#) ->
case readArray# arr# i# s# of { StateAndPtr# s2# r ->
- (r, S# s2#) })
+ ST_RET(r, STATE_TOK(s2#)) }))
updTbl :: FastStringTableVar -> FastStringTable -> Int# -> [FastString] -> PrimIO ()
-updTbl (_MutableArray _ var#) (FastStringTable uid# arr#) i# ls =
- MkST ( \ (S# s#) ->
+updTbl ref (FastStringTable uid# arr#) i# ls =
+ ST_TO_PrimIO (
+ MkST ( \ STATE_TOK(s#) ->
case writeArray# arr# i# ls s# of { s2# ->
- case writeArray# var# 0# (FastStringTable (uid# +# 1#) arr#) s2# of { s3# ->
- ((), S# s3#) }})
+ ST_RET((), STATE_TOK(s2#)) })) `thenPrimIO` \ _ ->
+ writeVar ref (FastStringTable (uid# +# 1#) arr#)
mkFastString# :: Addr# -> Int# -> FastString
mkFastString# a# len# =
#if __GLASGOW_HASKELL__ >= 202
import IO
import GlaExts
+# if __GLASGOW_HASKELL__ >= 209
+import Addr
+# endif
+
#else
import Ubiq ( Uniquable(..), Unique, Name ) -- FastString mentions it; todo: rm
import PackBase
# endif
+# if __GLASGOW_HASKELL__ >= 209
+import Addr
+# endif
+
#endif
\end{code}
NULs.
\begin{code}
+
copyPrefixStr :: _Addr -> Int -> _ByteArray Int
copyPrefixStr (A# a) len@(I# length#) =
- unsafePerformPrimIO (
+ unsafePerformST (
{- allocate an array that will hold the string
(not forgetting the NUL at the end)
-}
- (new_ps_array (length# +# 1#)) `thenPrimIO` \ ch_array ->
-{- Revert back to Haskell-only solution for the moment.
- _ccall_ memcpy ch_array (A# a) len `thenPrimIO` \ () ->
- write_ps_array ch_array length# (chr# 0#) `seqPrimIO`
--}
+ new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
-- fill in packed string from "addr"
- fill_in ch_array 0# `seqPrimIO`
+ fill_in ch_array 0# `thenStrictlyST` \ _ ->
-- freeze the puppy:
- freeze_ps_array ch_array)
+ freeze_ps_array ch_array `thenStrictlyST` \ barr ->
+ returnStrictlyST barr )
where
fill_in :: _MutableByteArray s Int -> Int# -> _ST s ()
\begin{code}
copySubStrFO :: _ForeignObj -> Int -> Int -> _ByteArray Int
copySubStrFO (_ForeignObj fo) (I# start#) len@(I# length#) =
- unsafePerformPrimIO (
+ unsafePerformST (
{- allocate an array that will hold the string
(not forgetting the NUL at the end)
-}
copySubStrBA :: _ByteArray Int -> Int -> Int -> _ByteArray Int
copySubStrBA (_ByteArray _ barr#) (I# start#) len@(I# length#) =
- unsafePerformPrimIO (
+ unsafePerformST (
{- allocate an array that will hold the string
(not forgetting the NUL at the end)
-}
freeze_ps_array :: _MutableByteArray s Int -> _ST s (_ByteArray Int)
new_ps_array size =
- MkST ( \ (S# s) ->
- case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
- (_MutableByteArray (0, max 0 (I# (size -# 1#))) barr#, S# s2#)})
+ MkST ( \ STATE_TOK(s#) ->
+ case (newCharArray# size s#) of { StateAndMutableByteArray# s2# barr# ->
+ ST_RET(_MutableByteArray (0, max 0 (I# (size -# 1#))) barr#, STATE_TOK(s2#))})
write_ps_array (_MutableByteArray _ barr#) n ch =
- MkST ( \ (S# s#) ->
+ MkST ( \ STATE_TOK(s#) ->
case writeCharArray# barr# n ch s# of { s2# ->
- ((), S# s2#)})
+ ST_RET((), STATE_TOK(s2#) )})
-- same as unsafeFreezeByteArray
freeze_ps_array (_MutableByteArray ixs arr#) =
- MkST ( \ (S# s#) ->
+ MkST ( \ STATE_TOK(s#) ->
case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
- (_ByteArray ixs frozen#, S# s2#) })
+ ST_RET((_ByteArray ixs frozen#), STATE_TOK(s2#))})
\end{code}
Compare two equal-length strings for equality:
sstToST :: SST s r -> ST s r
stToSST :: ST s r -> SST s r
-#if __GLASGOW_HASKELL__ >= 200
+#if __GLASGOW_HASKELL__ >= 200 && __GLASGOW_HASKELL__ < 209
sstToST sst = ST $ \ (S# s) ->
case sst s of SST_R r s' -> (r, S# s')
stToSST (ST st) = \ s ->
case st (S# s) of (r, S# s') -> SST_R r s'
+#elif __GLASGOW_HASKELL__ >= 209
+
+sstToST sst = ST $ \ s ->
+ case sst s of SST_R r s' -> STret s' r
+
+stToSST (ST st) = \ s ->
+ case st s of STret s' r -> SST_R r s'
+
#else
sstToST sst (S# s)
= case sst s of SST_R r s' -> (r, S# s')
# if __GLASGOW_HASKELL__ >= 206
import PackBase
# endif
+# if __GLASGOW_HASKELL__ >= 209
+import Addr
+# endif
#endif
import PrimPacked
import FastString