2 % (c) The GRASP/AQUA Project, Glasgow University, 1997-1998
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#
28 -- This #define suppresses the "import FastString" that
29 -- HsVersions otherwise produces
30 #define COMPILING_FAST_STRING
31 #include "HsVersions.h"
34 import PrelAddr ( Addr(..) )
37 -- ForeignObj is now exported abstractly.
38 #if __GLASGOW_HASKELL__ >= 303
39 import PrelForeign ( ForeignObj(..) )
42 #if __GLASGOW_HASKELL__ < 301
43 import ArrBase ( StateAndMutableByteArray#(..),
44 StateAndByteArray#(..) )
46 #elif __GLASGOW_HASKELL__ < 400
47 import PrelArr ( StateAndMutableByteArray#(..),
48 StateAndByteArray#(..) )
56 Return the length of a @\\NUL@ terminated character string:
59 strLength :: Addr -> Int
62 _ccall_ strlen a >>= \ len@(I# _) ->
65 {-# NOINLINE strLength #-}
68 Copying a char string prefix into a byte array,
69 {\em assuming} the prefix does not contain any
73 copyPrefixStr :: Addr -> Int -> ByteArray Int
74 copyPrefixStr (A# a) len@(I# length#) =
76 {- allocate an array that will hold the string
77 (not forgetting the NUL at the end)
79 (new_ps_array (length# +# 1#)) >>= \ ch_array ->
80 {- Revert back to Haskell-only solution for the moment.
81 _ccall_ memcpy ch_array (A# a) len >>= \ () ->
82 write_ps_array ch_array length# (chr# 0#) >>
84 -- fill in packed string from "addr"
85 fill_in ch_array 0# >>
87 freeze_ps_array ch_array length# `thenStrictlyST` \ barr ->
88 returnStrictlyST barr )
90 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
94 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
97 = case (indexCharOffAddr# a idx) of { ch ->
98 write_ps_array arr_in# idx ch `seqStrictlyST`
99 fill_in arr_in# (idx +# 1#) }
103 Copying out a substring, assume a 0-indexed string:
104 (and positive lengths, thank you).
107 copySubStr :: Addr -> Int -> Int -> ByteArray Int
108 copySubStr a start length =
110 _casm_ `` %r= (char *)((char *)%0 + (int)%1); '' a start
112 return (copyPrefixStr a_start length))
115 pCopying a sub-string out of a ForeignObj
118 copySubStrFO :: ForeignObj -> Int -> Int -> ByteArray Int
119 copySubStrFO (ForeignObj fo) (I# start#) len@(I# length#) =
121 {- allocate an array that will hold the string
122 (not forgetting the NUL at the end)
124 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
125 -- fill in packed string from "addr"
126 fill_in ch_array 0# `seqStrictlyST`
128 freeze_ps_array ch_array length#)
130 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
134 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
137 = case (indexCharOffForeignObj# fo (idx +# start#)) of { ch ->
138 write_ps_array arr_in# idx ch `seqStrictlyST`
139 fill_in arr_in# (idx +# 1#) }
141 -- step on (char *) pointer by x units.
142 addrOffset# :: Addr# -> Int# -> Addr#
144 case unsafePerformIO (_casm_ ``%r=(char *)((char *)%0 + (int)%1); '' (A# a#) (I# i#)) of
147 copySubStrBA :: ByteArray Int -> Int -> Int -> ByteArray Int
148 #if __GLASGOW_HASKELL__ >= 405
149 copySubStrBA (ByteArray _ _ barr#) (I# start#) len@(I# length#) =
151 copySubStrBA (ByteArray _ barr#) (I# start#) len@(I# length#) =
154 {- allocate an array that will hold the string
155 (not forgetting the NUL at the end)
157 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
158 -- fill in packed string from "addr"
159 fill_in ch_array 0# `seqStrictlyST`
161 freeze_ps_array ch_array length#)
163 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
167 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
170 = case (indexCharArray# barr# (start# +# idx)) of { ch ->
171 write_ps_array arr_in# idx ch `seqStrictlyST`
172 fill_in arr_in# (idx +# 1#) }
176 (Very :-) ``Specialised'' versions of some CharArray things...
177 [Copied from PackBase; no real reason -- UGH]
180 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
181 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
182 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
184 new_ps_array size = ST $ \ s ->
185 #if __GLASGOW_HASKELL__ < 400
186 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
187 STret s2# (MutableByteArray bot barr#) }
188 #elif __GLASGOW_HASKELL__ < 405
189 case (newCharArray# size s) of { (# s2#, barr# #) ->
190 (# s2#, MutableByteArray bot barr# #) }
192 case (newCharArray# size s) of { (# s2#, barr# #) ->
193 (# s2#, MutableByteArray bot bot barr# #) }
196 bot = error "new_ps_array"
198 #if __GLASGOW_HASKELL__ < 400
199 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
200 case writeCharArray# barr# n ch s# of { s2# ->
202 #elif __GLASGOW_HASKELL__ < 405
203 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
204 case writeCharArray# barr# n ch s# of { s2# ->
207 write_ps_array (MutableByteArray _ _ barr#) n ch = ST $ \ s# ->
208 case writeCharArray# barr# n ch s# of { s2# ->
212 -- same as unsafeFreezeByteArray
213 #if __GLASGOW_HASKELL__ < 400
214 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
215 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
216 STret s2# (ByteArray (0,I# len#) frozen#) }
217 #elif __GLASGOW_HASKELL__ < 405
218 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
219 case unsafeFreezeByteArray# arr# s# of { (# s2#, frozen# #) ->
220 (# s2#, ByteArray (0,I# len#) frozen# #) }
222 freeze_ps_array (MutableByteArray _ _ arr#) len# = ST $ \ s# ->
223 case unsafeFreezeByteArray# arr# s# of { (# s2#, frozen# #) ->
224 (# s2#, ByteArray 0 (I# len#) frozen# #) }
229 Compare two equal-length strings for equality:
232 eqStrPrefix :: Addr# -> ByteArray# -> Int# -> Bool
233 eqStrPrefix a# barr# len# =
235 #if __GLASGOW_HASKELL__ < 405
236 _ccall_ strncmp (A# a#) (ByteArray bot barr#) (I# len#) >>= \ (I# x#) ->
238 _ccall_ strncmp (A# a#) (ByteArray bot bot barr#) (I# len#) >>= \ (I# x#) ->
242 #if __GLASGOW_HASKELL__ < 405
247 bot = error "eqStrPrefix"
249 eqCharStrPrefix :: Addr# -> Addr# -> Int# -> Bool
250 eqCharStrPrefix a1# a2# len# =
252 _ccall_ strncmp (A# a1#) (A# a2#) (I# len#) >>= \ (I# x#) ->
255 eqStrPrefixBA :: ByteArray# -> ByteArray# -> Int# -> Int# -> Bool
256 eqStrPrefixBA b1# b2# start# len# =
258 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
259 #if __GLASGOW_HASKELL__ < 405
262 (ByteArray bot bot b2#)
265 #if __GLASGOW_HASKELL__ < 405
268 (ByteArray bot bot b1#)
270 (I# len#) >>= \ (I# x#) ->
273 #if __GLASGOW_HASKELL__ < 405
278 bot = error "eqStrPrefixBA"
280 eqCharStrPrefixBA :: Addr# -> ByteArray# -> Int# -> Int# -> Bool
281 eqCharStrPrefixBA a# b2# start# len# =
283 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
284 #if __GLASGOW_HASKELL__ < 405
287 (ByteArray bot bot b2#)
291 (I# len#) >>= \ (I# x#) ->
294 #if __GLASGOW_HASKELL__ < 405
299 bot = error "eqCharStrPrefixBA"
301 eqStrPrefixFO :: ForeignObj# -> ByteArray# -> Int# -> Int# -> Bool
302 eqStrPrefixFO fo# barr# start# len# =
304 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
307 #if __GLASGOW_HASKELL__ < 405
308 (ByteArray bot barr#)
310 (ByteArray bot bot barr#)
312 (I# len#) >>= \ (I# x#) ->
315 #if __GLASGOW_HASKELL__ < 405
320 bot = error "eqStrPrefixFO"