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#
28 -- This #define suppresses the "import FastString" that
29 -- HsVersions otherwise produces
30 #define COMPILING_FAST_STRING
31 #include "HsVersions.h"
34 import Addr ( 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#(..) )
47 import PrelArr ( StateAndMutableByteArray#(..),
48 StateAndByteArray#(..) )
54 Return the length of a @\\NUL@ terminated character string:
57 strLength :: Addr -> Int
60 _ccall_ strlen a >>= \ len@(I# _) ->
66 Copying a char string prefix into a byte array,
67 {\em assuming} the prefix does not contain any
71 copyPrefixStr :: Addr -> Int -> ByteArray Int
72 copyPrefixStr (A# a) len@(I# length#) =
74 {- allocate an array that will hold the string
75 (not forgetting the NUL at the end)
77 (new_ps_array (length# +# 1#)) >>= \ ch_array ->
78 {- Revert back to Haskell-only solution for the moment.
79 _ccall_ memcpy ch_array (A# a) len >>= \ () ->
80 write_ps_array ch_array length# (chr# 0#) >>
82 -- fill in packed string from "addr"
83 fill_in ch_array 0# >>
85 freeze_ps_array ch_array length# `thenStrictlyST` \ barr ->
86 returnStrictlyST barr )
88 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
92 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
95 = case (indexCharOffAddr# a idx) of { ch ->
96 write_ps_array arr_in# idx ch `seqStrictlyST`
97 fill_in arr_in# (idx +# 1#) }
101 Copying out a substring, assume a 0-indexed string:
102 (and positive lengths, thank you).
105 copySubStr :: Addr -> Int -> Int -> ByteArray Int
106 copySubStr a start length =
108 _casm_ `` %r= (char *)((char *)%0 + (int)%1); '' a start
110 return (copyPrefixStr a_start length))
113 pCopying a sub-string out of a ForeignObj
116 copySubStrFO :: ForeignObj -> Int -> Int -> ByteArray Int
117 copySubStrFO (ForeignObj fo) (I# start#) len@(I# length#) =
119 {- allocate an array that will hold the string
120 (not forgetting the NUL at the end)
122 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
123 -- fill in packed string from "addr"
124 fill_in ch_array 0# `seqStrictlyST`
126 freeze_ps_array ch_array length#)
128 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
132 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
135 = case (indexCharOffForeignObj# fo (idx +# start#)) of { ch ->
136 write_ps_array arr_in# idx ch `seqStrictlyST`
137 fill_in arr_in# (idx +# 1#) }
139 -- step on (char *) pointer by x units.
140 addrOffset# :: Addr# -> Int# -> Addr#
142 case unsafePerformIO (_casm_ ``%r=(char *)((char *)%0 + (int)%1); '' (A# a#) (I# i#)) of
145 copySubStrBA :: ByteArray Int -> Int -> Int -> ByteArray Int
146 copySubStrBA (ByteArray _ barr#) (I# start#) len@(I# length#) =
148 {- allocate an array that will hold the string
149 (not forgetting the NUL at the end)
151 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
152 -- fill in packed string from "addr"
153 fill_in ch_array 0# `seqStrictlyST`
155 freeze_ps_array ch_array length#)
157 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
161 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
164 = case (indexCharArray# barr# (start# +# idx)) of { ch ->
165 write_ps_array arr_in# idx ch `seqStrictlyST`
166 fill_in arr_in# (idx +# 1#) }
170 (Very :-) ``Specialised'' versions of some CharArray things...
171 [Copied from PackBase; no real reason -- UGH]
174 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
175 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
176 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
178 new_ps_array size = ST $ \ s ->
179 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
180 STret s2# (MutableByteArray bot barr#) }
182 bot = error "new_ps_array"
184 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
185 case writeCharArray# barr# n ch s# of { s2# ->
188 -- same as unsafeFreezeByteArray
189 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
190 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
191 STret s2# (ByteArray (0,I# len#) frozen#) }
195 Compare two equal-length strings for equality:
198 eqStrPrefix :: Addr# -> ByteArray# -> Int# -> Bool
199 eqStrPrefix a# barr# len# =
201 _ccall_ strncmp (A# a#) (ByteArray bottom barr#) (I# len#) >>= \ (I# x#) ->
205 bottom = error "eqStrPrefix"
207 eqCharStrPrefix :: Addr# -> Addr# -> Int# -> Bool
208 eqCharStrPrefix a1# a2# len# =
210 _ccall_ strncmp (A# a1#) (A# a2#) (I# len#) >>= \ (I# x#) ->
213 eqStrPrefixBA :: ByteArray# -> ByteArray# -> Int# -> Int# -> Bool
214 eqStrPrefixBA b1# b2# start# len# =
216 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
217 (ByteArray bottom b2#)
219 (ByteArray bottom b1#)
220 (I# len#) >>= \ (I# x#) ->
224 bottom = error "eqStrPrefixBA"
226 eqCharStrPrefixBA :: Addr# -> ByteArray# -> Int# -> Int# -> Bool
227 eqCharStrPrefixBA a# b2# start# len# =
229 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
230 (ByteArray bottom b2#)
233 (I# len#) >>= \ (I# x#) ->
237 bottom = error "eqCharStrPrefixBA"
239 eqStrPrefixFO :: ForeignObj# -> ByteArray# -> Int# -> Int# -> Bool
240 eqStrPrefixFO fo# barr# start# len# =
242 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
245 (ByteArray bottom barr#)
246 (I# len#) >>= \ (I# x#) ->
250 bottom = error "eqStrPrefixFO"