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 indexCharOffFO# :: ForeignObj# -> Int# -> Char#
131 indexCharOffFO# fo i = indexCharOffForeignObj# fo i
133 -- step on (char *) pointer by x units.
134 addrOffset# :: Addr# -> Int# -> Addr#
136 case unsafePerformIO (_casm_ ``%r=(char *)((char *)%0 + (int)%1); '' (A# a#) (I# i#)) of
139 copySubStrBA :: ByteArray Int -> Int -> Int -> ByteArray Int
140 copySubStrBA (ByteArray _ barr#) (I# start#) len@(I# length#) =
142 {- allocate an array that will hold the string
143 (not forgetting the NUL at the end)
145 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
146 -- fill in packed string from "addr"
147 fill_in ch_array 0# `seqStrictlyST`
149 freeze_ps_array ch_array length#)
151 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
155 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
158 = case (indexCharArray# barr# (start# +# idx)) of { ch ->
159 write_ps_array arr_in# idx ch `seqStrictlyST`
160 fill_in arr_in# (idx +# 1#) }
164 (Very :-) ``Specialised'' versions of some CharArray things...
165 [Copied from PackBase; no real reason -- UGH]
168 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
169 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
170 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
172 new_ps_array size = ST $ \ s ->
173 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
174 STret s2# (MutableByteArray bot barr#) }
176 bot = error "new_ps_array"
178 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
179 case writeCharArray# barr# n ch s# of { s2# ->
182 -- same as unsafeFreezeByteArray
183 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
184 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
185 STret s2# (ByteArray (0,I# len#) frozen#) }
189 Compare two equal-length strings for equality:
192 eqStrPrefix :: Addr# -> ByteArray# -> Int# -> Bool
193 eqStrPrefix a# barr# len# =
195 _ccall_ strncmp (A# a#) (ByteArray bottom barr#) (I# len#) >>= \ (I# x#) ->
199 bottom = error "eqStrPrefix"
201 eqCharStrPrefix :: Addr# -> Addr# -> Int# -> Bool
202 eqCharStrPrefix a1# a2# len# =
204 _ccall_ strncmp (A# a1#) (A# a2#) (I# len#) >>= \ (I# x#) ->
208 bottom = error "eqStrPrefix"
210 eqStrPrefixBA :: ByteArray# -> ByteArray# -> Int# -> Int# -> Bool
211 eqStrPrefixBA b1# b2# start# len# =
213 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
214 (ByteArray bottom b2#)
216 (ByteArray bottom b1#)
217 (I# len#) >>= \ (I# x#) ->
221 bottom = error "eqStrPrefixBA"
223 eqCharStrPrefixBA :: Addr# -> ByteArray# -> Int# -> Int# -> Bool
224 eqCharStrPrefixBA a# b2# start# len# =
226 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
227 (ByteArray bottom b2#)
230 (I# len#) >>= \ (I# x#) ->
234 bottom = error "eqCharStrPrefixBA"
236 eqStrPrefixFO :: ForeignObj# -> ByteArray# -> Int# -> Int# -> Bool
237 eqStrPrefixFO fo# barr# start# len# =
239 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
242 (ByteArray bottom barr#)
243 (I# len#) >>= \ (I# x#) ->
247 bottom = error "eqStrPrefixFO"
251 byteArrayToString :: ByteArray Int -> String
252 byteArrayToString = unpackCStringBA
257 stringToByteArray :: String -> (ByteArray Int)
258 stringToByteArray = packString