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(..) )
38 #if __GLASGOW_HASKELL__ < 301
39 import ArrBase ( StateAndMutableByteArray#(..),
40 StateAndByteArray#(..) )
43 import PrelArr ( StateAndMutableByteArray#(..),
44 StateAndByteArray#(..) )
50 Return the length of a @\\NUL@ terminated character string:
53 strLength :: Addr -> Int
56 _ccall_ strlen a >>= \ len@(I# _) ->
62 Copying a char string prefix into a byte array,
63 {\em assuming} the prefix does not contain any
67 copyPrefixStr :: Addr -> Int -> ByteArray Int
68 copyPrefixStr (A# a) len@(I# length#) =
70 {- allocate an array that will hold the string
71 (not forgetting the NUL at the end)
73 (new_ps_array (length# +# 1#)) >>= \ ch_array ->
74 {- Revert back to Haskell-only solution for the moment.
75 _ccall_ memcpy ch_array (A# a) len >>= \ () ->
76 write_ps_array ch_array length# (chr# 0#) >>
78 -- fill in packed string from "addr"
79 fill_in ch_array 0# >>
81 freeze_ps_array ch_array length# `thenStrictlyST` \ barr ->
82 returnStrictlyST barr )
84 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
88 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
91 = case (indexCharOffAddr# a idx) of { ch ->
92 write_ps_array arr_in# idx ch `seqStrictlyST`
93 fill_in arr_in# (idx +# 1#) }
97 Copying out a substring, assume a 0-indexed string:
98 (and positive lengths, thank you).
101 copySubStr :: Addr -> Int -> Int -> ByteArray Int
102 copySubStr a start length =
104 _casm_ `` %r= (char *)((char *)%0 + (int)%1); '' a start
106 return (copyPrefixStr a_start length))
109 pCopying a sub-string out of a ForeignObj
112 copySubStrFO :: ForeignObj -> Int -> Int -> ByteArray Int
113 copySubStrFO (ForeignObj fo) (I# start#) len@(I# length#) =
115 {- allocate an array that will hold the string
116 (not forgetting the NUL at the end)
118 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
119 -- fill in packed string from "addr"
120 fill_in ch_array 0# `seqStrictlyST`
122 freeze_ps_array ch_array length#)
124 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
128 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
131 = case (indexCharOffForeignObj# fo (idx +# start#)) of { ch ->
132 write_ps_array arr_in# idx ch `seqStrictlyST`
133 fill_in arr_in# (idx +# 1#) }
135 -- step on (char *) pointer by x units.
136 addrOffset# :: Addr# -> Int# -> Addr#
138 case unsafePerformIO (_casm_ ``%r=(char *)((char *)%0 + (int)%1); '' (A# a#) (I# i#)) of
141 copySubStrBA :: ByteArray Int -> Int -> Int -> ByteArray Int
142 copySubStrBA (ByteArray _ barr#) (I# start#) len@(I# length#) =
144 {- allocate an array that will hold the string
145 (not forgetting the NUL at the end)
147 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
148 -- fill in packed string from "addr"
149 fill_in ch_array 0# `seqStrictlyST`
151 freeze_ps_array ch_array length#)
153 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
157 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
160 = case (indexCharArray# barr# (start# +# idx)) of { ch ->
161 write_ps_array arr_in# idx ch `seqStrictlyST`
162 fill_in arr_in# (idx +# 1#) }
166 (Very :-) ``Specialised'' versions of some CharArray things...
167 [Copied from PackBase; no real reason -- UGH]
170 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
171 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
172 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
174 new_ps_array size = ST $ \ s ->
175 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
176 STret s2# (MutableByteArray bot barr#) }
178 bot = error "new_ps_array"
180 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
181 case writeCharArray# barr# n ch s# of { s2# ->
184 -- same as unsafeFreezeByteArray
185 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
186 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
187 STret s2# (ByteArray (0,I# len#) frozen#) }
191 Compare two equal-length strings for equality:
194 eqStrPrefix :: Addr# -> ByteArray# -> Int# -> Bool
195 eqStrPrefix a# barr# len# =
197 _ccall_ strncmp (A# a#) (ByteArray bottom barr#) (I# len#) >>= \ (I# x#) ->
201 bottom = error "eqStrPrefix"
203 eqCharStrPrefix :: Addr# -> Addr# -> Int# -> Bool
204 eqCharStrPrefix a1# a2# len# =
206 _ccall_ strncmp (A# a1#) (A# a2#) (I# len#) >>= \ (I# x#) ->
209 eqStrPrefixBA :: ByteArray# -> ByteArray# -> Int# -> Int# -> Bool
210 eqStrPrefixBA b1# b2# start# len# =
212 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
213 (ByteArray bottom b2#)
215 (ByteArray bottom b1#)
216 (I# len#) >>= \ (I# x#) ->
220 bottom = error "eqStrPrefixBA"
222 eqCharStrPrefixBA :: Addr# -> ByteArray# -> Int# -> Int# -> Bool
223 eqCharStrPrefixBA a# b2# start# len# =
225 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
226 (ByteArray bottom b2#)
229 (I# len#) >>= \ (I# x#) ->
233 bottom = error "eqCharStrPrefixBA"
235 eqStrPrefixFO :: ForeignObj# -> ByteArray# -> Int# -> Int# -> Bool
236 eqStrPrefixFO fo# barr# start# len# =
238 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
241 (ByteArray bottom barr#)
242 (I# len#) >>= \ (I# x#) ->
246 bottom = error "eqStrPrefixFO"