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(..) )
39 import IOBase ( ForeignObj(..) )
40 import PackBase ( unpackCStringBA, packString )
43 Return the length of a @\\NUL@ terminated character string:
46 strLength :: Addr -> Int
49 _ccall_ strlen a >>= \ len@(I# _) ->
55 Copying a char string prefix into a byte array,
56 {\em assuming} the prefix does not contain any
60 copyPrefixStr :: Addr -> Int -> ByteArray Int
61 copyPrefixStr (A# a) len@(I# length#) =
63 {- allocate an array that will hold the string
64 (not forgetting the NUL at the end)
66 (new_ps_array (length# +# 1#)) >>= \ ch_array ->
67 {- Revert back to Haskell-only solution for the moment.
68 _ccall_ memcpy ch_array (A# a) len >>= \ () ->
69 write_ps_array ch_array length# (chr# 0#) >>
71 -- fill in packed string from "addr"
72 fill_in ch_array 0# >>
74 freeze_ps_array ch_array length# `thenStrictlyST` \ barr ->
75 returnStrictlyST barr )
77 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
81 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
84 = case (indexCharOffAddr# a idx) of { ch ->
85 write_ps_array arr_in# idx ch `seqStrictlyST`
86 fill_in arr_in# (idx +# 1#) }
90 Copying out a substring, assume a 0-indexed string:
91 (and positive lengths, thank you).
94 copySubStr :: Addr -> Int -> Int -> ByteArray Int
95 copySubStr a start length =
97 _casm_ `` %r= (char *)((char *)%0 + (int)%1); '' a start
99 return (copyPrefixStr a_start length))
102 pCopying a sub-string out of a ForeignObj
105 copySubStrFO :: ForeignObj -> Int -> Int -> ByteArray Int
106 copySubStrFO (ForeignObj fo) (I# start#) len@(I# length#) =
108 {- allocate an array that will hold the string
109 (not forgetting the NUL at the end)
111 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
112 -- fill in packed string from "addr"
113 fill_in ch_array 0# `seqStrictlyST`
115 freeze_ps_array ch_array length#)
117 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
121 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
124 = case (indexCharOffForeignObj# fo (idx +# start#)) of { ch ->
125 write_ps_array arr_in# idx ch `seqStrictlyST`
126 fill_in arr_in# (idx +# 1#) }
128 -- step on (char *) pointer by x units.
129 addrOffset# :: Addr# -> Int# -> Addr#
131 case unsafePerformIO (_casm_ ``%r=(char *)((char *)%0 + (int)%1); '' (A# a#) (I# i#)) of
134 copySubStrBA :: ByteArray Int -> Int -> Int -> ByteArray Int
135 copySubStrBA (ByteArray _ barr#) (I# start#) len@(I# length#) =
137 {- allocate an array that will hold the string
138 (not forgetting the NUL at the end)
140 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
141 -- fill in packed string from "addr"
142 fill_in ch_array 0# `seqStrictlyST`
144 freeze_ps_array ch_array length#)
146 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
150 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
153 = case (indexCharArray# barr# (start# +# idx)) of { ch ->
154 write_ps_array arr_in# idx ch `seqStrictlyST`
155 fill_in arr_in# (idx +# 1#) }
159 (Very :-) ``Specialised'' versions of some CharArray things...
160 [Copied from PackBase; no real reason -- UGH]
163 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
164 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
165 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
167 new_ps_array size = ST $ \ s ->
168 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
169 STret s2# (MutableByteArray bot barr#) }
171 bot = error "new_ps_array"
173 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
174 case writeCharArray# barr# n ch s# of { s2# ->
177 -- same as unsafeFreezeByteArray
178 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
179 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
180 STret s2# (ByteArray (0,I# len#) frozen#) }
184 Compare two equal-length strings for equality:
187 eqStrPrefix :: Addr# -> ByteArray# -> Int# -> Bool
188 eqStrPrefix a# barr# len# =
190 _ccall_ strncmp (A# a#) (ByteArray bottom barr#) (I# len#) >>= \ (I# x#) ->
194 bottom = error "eqStrPrefix"
196 eqCharStrPrefix :: Addr# -> Addr# -> Int# -> Bool
197 eqCharStrPrefix a1# a2# len# =
199 _ccall_ strncmp (A# a1#) (A# a2#) (I# len#) >>= \ (I# x#) ->
203 bottom = error "eqStrPrefix"
205 eqStrPrefixBA :: ByteArray# -> ByteArray# -> Int# -> Int# -> Bool
206 eqStrPrefixBA b1# b2# start# len# =
208 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
209 (ByteArray bottom b2#)
211 (ByteArray bottom b1#)
212 (I# len#) >>= \ (I# x#) ->
216 bottom = error "eqStrPrefixBA"
218 eqCharStrPrefixBA :: Addr# -> ByteArray# -> Int# -> Int# -> Bool
219 eqCharStrPrefixBA a# b2# start# len# =
221 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
222 (ByteArray bottom b2#)
225 (I# len#) >>= \ (I# x#) ->
229 bottom = error "eqCharStrPrefixBA"
231 eqStrPrefixFO :: ForeignObj# -> ByteArray# -> Int# -> Int# -> Bool
232 eqStrPrefixFO fo# barr# start# len# =
234 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
237 (ByteArray bottom barr#)
238 (I# len#) >>= \ (I# x#) ->
242 bottom = error "eqStrPrefixFO"
246 byteArrayToString :: ByteArray Int -> String
247 byteArrayToString = unpackCStringBA
252 stringToByteArray :: String -> (ByteArray Int)
253 stringToByteArray = packString