2 % (c) The GRASP/AQUA Project, Glasgow University, 1997
4 \section{Basic ops on packed representations}
6 Some basic operations for working on packed representations of series
7 of bytes (character strings). Used by the interface lexer input
13 strLength, -- :: _Addr -> Int
14 copyPrefixStr, -- :: _Addr -> Int -> ByteArray Int
15 copySubStr, -- :: _Addr -> Int -> Int -> ByteArray Int
16 copySubStrFO, -- :: ForeignObj -> Int -> Int -> ByteArray Int
17 copySubStrBA, -- :: ByteArray Int -> Int -> Int -> ByteArray Int
19 eqStrPrefix, -- :: Addr# -> ByteArray# -> Int# -> Bool
20 eqCharStrPrefix, -- :: Addr# -> Addr# -> Int# -> Bool
21 eqStrPrefixBA, -- :: ByteArray# -> ByteArray# -> Int# -> Int# -> Bool
22 eqCharStrPrefixBA, -- :: Addr# -> ByteArray# -> Int# -> Int# -> Bool
23 eqStrPrefixFO, -- :: ForeignObj# -> ByteArray# -> Int# -> Int# -> Bool
25 addrOffset#, -- :: Addr# -> Int# -> Addr#
26 indexCharOffFO# -- :: ForeignObj# -> Int# -> Char#
29 -- This #define suppresses the "import FastString" that
30 -- HsVersions otherwise produces
31 #define COMPILING_FAST_STRING
32 #include "HsVersions.h"
35 import Addr ( Addr(..) )
40 import IOBase ( ForeignObj(..) )
41 import PackBase ( unpackCStringBA, packString )
44 Return the length of a @\\NUL@ terminated character string:
47 strLength :: Addr -> Int
50 _ccall_ strlen a >>= \ len@(I# _) ->
56 Copying a char string prefix into a byte array,
57 {\em assuming} the prefix does not contain any
61 copyPrefixStr :: Addr -> Int -> ByteArray Int
62 copyPrefixStr (A# a) len@(I# length#) =
64 {- allocate an array that will hold the string
65 (not forgetting the NUL at the end)
67 (new_ps_array (length# +# 1#)) >>= \ ch_array ->
68 {- Revert back to Haskell-only solution for the moment.
69 _ccall_ memcpy ch_array (A# a) len >>= \ () ->
70 write_ps_array ch_array length# (chr# 0#) >>
72 -- fill in packed string from "addr"
73 fill_in ch_array 0# >>
75 freeze_ps_array ch_array length# `thenStrictlyST` \ barr ->
76 returnStrictlyST barr )
78 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
82 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
85 = case (indexCharOffAddr# a idx) of { ch ->
86 write_ps_array arr_in# idx ch `seqStrictlyST`
87 fill_in arr_in# (idx +# 1#) }
91 Copying out a substring, assume a 0-indexed string:
92 (and positive lengths, thank you).
95 copySubStr :: Addr -> Int -> Int -> ByteArray Int
96 copySubStr a start length =
98 _casm_ `` %r= (char *)((char *)%0 + (int)%1); '' a start
100 return (copyPrefixStr a_start length))
103 pCopying a sub-string out of a ForeignObj
106 copySubStrFO :: ForeignObj -> Int -> Int -> ByteArray Int
107 copySubStrFO (ForeignObj fo) (I# start#) len@(I# length#) =
109 {- allocate an array that will hold the string
110 (not forgetting the NUL at the end)
112 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
113 -- fill in packed string from "addr"
114 fill_in ch_array 0# `seqStrictlyST`
116 freeze_ps_array ch_array length#)
118 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
122 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
125 = case (indexCharOffFO# fo (idx +# start#)) of { ch ->
126 write_ps_array arr_in# idx ch `seqStrictlyST`
127 fill_in arr_in# (idx +# 1#) }
129 {- ToDo: add FO primitives.. -}
130 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <=205
131 indexCharOffFO# :: ForeignObj# -> Int# -> Char#
132 indexCharOffFO# fo# i# =
133 case unsafePerformIO (_casm_ ``%r=(char)*((char *)%0 + (int)%1); '' (ForeignObj fo#) (I# i#)) of
136 indexCharOffFO# :: ForeignObj# -> Int# -> Char#
137 indexCharOffFO# fo i = indexCharOffForeignObj# fo i
140 -- step on (char *) pointer by x units.
141 addrOffset# :: Addr# -> Int# -> Addr#
143 case unsafePerformIO (_casm_ ``%r=(char *)((char *)%0 + (int)%1); '' (A# a#) (I# i#)) of
146 copySubStrBA :: ByteArray Int -> Int -> Int -> ByteArray Int
147 copySubStrBA (ByteArray _ barr#) (I# start#) len@(I# length#) =
149 {- allocate an array that will hold the string
150 (not forgetting the NUL at the end)
152 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
153 -- fill in packed string from "addr"
154 fill_in ch_array 0# `seqStrictlyST`
156 freeze_ps_array ch_array length#)
158 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
162 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
165 = case (indexCharArray# barr# (start# +# idx)) of { ch ->
166 write_ps_array arr_in# idx ch `seqStrictlyST`
167 fill_in arr_in# (idx +# 1#) }
171 (Very :-) ``Specialised'' versions of some CharArray things...
172 [Copied from PackBase; no real reason -- UGH]
175 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
176 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
177 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
179 new_ps_array size = ST $ \ s ->
180 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
181 STret s2# (MutableByteArray bot barr#) }
183 bot = error "new_ps_array"
185 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
186 case writeCharArray# barr# n ch s# of { s2# ->
189 -- same as unsafeFreezeByteArray
190 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
191 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
192 STret s2# (ByteArray (0,I# len#) frozen#) }
196 Compare two equal-length strings for equality:
199 eqStrPrefix :: Addr# -> ByteArray# -> Int# -> Bool
200 eqStrPrefix a# barr# len# =
202 _ccall_ strncmp (A# a#) (ByteArray bottom barr#) (I# len#) >>= \ (I# x#) ->
206 bottom = error "eqStrPrefix"
208 eqCharStrPrefix :: Addr# -> Addr# -> Int# -> Bool
209 eqCharStrPrefix a1# a2# len# =
211 _ccall_ strncmp (A# a1#) (A# a2#) (I# len#) >>= \ (I# x#) ->
215 bottom = error "eqStrPrefix"
217 eqStrPrefixBA :: ByteArray# -> ByteArray# -> Int# -> Int# -> Bool
218 eqStrPrefixBA b1# b2# start# len# =
220 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
221 (ByteArray bottom b2#)
223 (ByteArray bottom b1#)
224 (I# len#) >>= \ (I# x#) ->
228 bottom = error "eqStrPrefixBA"
230 eqCharStrPrefixBA :: Addr# -> ByteArray# -> Int# -> Int# -> Bool
231 eqCharStrPrefixBA a# b2# start# len# =
233 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
234 (ByteArray bottom b2#)
237 (I# len#) >>= \ (I# x#) ->
241 bottom = error "eqCharStrPrefixBA"
243 eqStrPrefixFO :: ForeignObj# -> ByteArray# -> Int# -> Int# -> Bool
244 eqStrPrefixFO fo# barr# start# len# =
246 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
249 (ByteArray bottom barr#)
250 (I# len#) >>= \ (I# x#) ->
254 bottom = error "eqStrPrefixFO"
258 byteArrayToString :: ByteArray Int -> String
259 byteArrayToString = unpackCStringBA
264 stringToByteArray :: String -> (ByteArray Int)
265 stringToByteArray = packString