2 % (c) The GRASP/AQUA Project, Glasgow University, 1997
4 \section{Primitive operations for packed strings}
6 Core operations for working on a chunk of bytes.
7 These operations is the core set needed by the
8 GHC internally, the code generator and the prelude
12 #include "HsVersions.h"
16 strLength, -- :: _Addr -> Int
17 copyPrefixStr, -- :: _Addr -> Int -> _ByteArray Int
18 copySubStr, -- :: _Addr -> Int -> Int -> _ByteArray Int
19 copySubStrFO, -- :: ForeignObj -> Int -> Int -> _ByteArray Int
20 copySubStrBA, -- :: _ByteArray Int -> Int -> Int -> _ByteArray Int
21 --packString2, -- :: Addr -> Int -> _ByteArray Int
22 stringToByteArray, -- :: String -> _ByteArray Int
23 byteArrayToString, -- :: _ByteArray Int -> String
25 eqStrPrefix, -- :: Addr# -> ByteArray# -> Int# -> Bool
26 eqCharStrPrefix, -- :: Addr# -> Addr# -> Int# -> Bool
27 eqStrPrefixBA, -- :: ByteArray# -> ByteArray# -> Int# -> Int# -> Bool
28 eqCharStrPrefixBA, -- :: Addr# -> ByteArray# -> Int# -> Int# -> Bool
29 eqStrPrefixFO, -- :: ForeignObj# -> ByteArray# -> Int# -> Int# -> Bool
31 addrOffset#, -- :: Addr# -> Int# -> Addr#
32 indexCharOffFO# -- :: ForeignObj# -> Int# -> Char#
35 #if __GLASGOW_HASKELL__ <= 201
49 Return the length of a @\\NUL@ terminated character string:
52 strLength :: _Addr -> Int
55 _ccall_ strlen a `thenPrimIO` \ len@(I# _) ->
61 Copying a char string prefix into a byte array,
62 {\em assuming} the prefix does not contain any
66 copyPrefixStr :: _Addr -> Int -> _ByteArray Int
67 copyPrefixStr (A# a) len@(I# length#) =
69 {- allocate an array that will hold the string
70 (not forgetting the NUL at the end)
72 (new_ps_array (length# +# 1#)) `thenPrimIO` \ ch_array ->
73 _ccall_ memcpy ch_array (A# a) len `thenPrimIO` \ () ->
74 write_ps_array ch_array length# (chr# 0#) `seqPrimIO`
75 -- fill in packed string from "addr"
76 --fill_in ch_array 0# `seqPrimIO`
78 freeze_ps_array ch_array)
80 fill_in :: _MutableByteArray s Int -> Int# -> _ST s ()
84 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
87 = case (indexCharOffAddr# a idx) of { ch ->
88 write_ps_array arr_in# idx ch `seqStrictlyST`
89 fill_in arr_in# (idx +# 1#) }
93 Copying out a substring, assume a 0-indexed string:
94 (and positive lengths, thank you).
97 copySubStr :: _Addr -> Int -> Int -> _ByteArray Int
98 copySubStr a start length =
100 _casm_ `` %r= (char *)((char *)%0 + (int)%1); '' a start `thenPrimIO` \ a_start ->
101 returnPrimIO (copyPrefixStr a_start length))
104 Copying a sub-string out of a ForeignObj
107 copySubStrFO :: _ForeignObj -> Int -> Int -> _ByteArray Int
108 copySubStrFO (_ForeignObj fo) (I# start#) len@(I# length#) =
109 unsafePerformPrimIO (
110 {- allocate an array that will hold the string
111 (not forgetting the NUL at the end)
113 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
114 -- fill in packed string from "addr"
115 fill_in ch_array 0# `seqStrictlyST`
117 freeze_ps_array ch_array)
119 fill_in :: _MutableByteArray s Int -> Int# -> _ST s ()
123 = write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
126 = case (indexCharOffFO# fo (idx +# start#)) of { ch ->
127 write_ps_array arr_in# idx ch `seqStrictlyST`
128 fill_in arr_in# (idx +# 1#) }
130 {- ToDo: add FO primitives.. -}
131 indexCharOffFO# :: ForeignObj# -> Int# -> Char#
132 indexCharOffFO# fo# i# =
133 case unsafePerformPrimIO (_casm_ ``%r=(char)*((char *)%0 + (int)%1); '' (_ForeignObj fo#) (I# i#)) of
136 addrOffset# :: Addr# -> Int# -> Addr#
138 case unsafePerformPrimIO (_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#) =
143 unsafePerformPrimIO (
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)
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...
169 new_ps_array :: Int# -> _ST s (_MutableByteArray s Int)
170 write_ps_array :: _MutableByteArray s Int -> Int# -> Char# -> _ST s ()
171 freeze_ps_array :: _MutableByteArray s Int -> _ST s (_ByteArray Int)
175 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
176 (_MutableByteArray (0, max 0 (I# (size -# 1#))) barr#, S# s2#)})
178 write_ps_array (_MutableByteArray _ barr#) n ch =
180 case writeCharArray# barr# n ch s# of { s2# ->
183 -- same as unsafeFreezeByteArray
184 freeze_ps_array (_MutableByteArray ixs arr#) =
186 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
187 (_ByteArray ixs frozen#, S# s2#) })
190 Compare two equal-length strings for equality:
193 eqStrPrefix :: Addr# -> ByteArray# -> Int# -> Bool
194 eqStrPrefix a# barr# len# =
195 unsafePerformPrimIO (
196 _ccall_ strncmp (A# a#) (_ByteArray bottom barr#) (I# len#) `thenPrimIO` \ (I# x#) ->
197 returnPrimIO (x# ==# 0#))
200 bottom = error "eqStrPrefix"
202 eqCharStrPrefix :: Addr# -> Addr# -> Int# -> Bool
203 eqCharStrPrefix a1# a2# len# =
204 unsafePerformPrimIO (
205 _ccall_ strncmp (A# a1#) (A# a2#) (I# len#) `thenPrimIO` \ (I# x#) ->
206 returnPrimIO (x# ==# 0#))
209 bottom = error "eqStrPrefix"
211 eqStrPrefixBA :: ByteArray# -> ByteArray# -> Int# -> Int# -> Bool
212 eqStrPrefixBA b1# b2# start# len# =
213 unsafePerformPrimIO (
214 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
215 (_ByteArray bottom b2#)
217 (_ByteArray bottom b1#)
218 (I# len#) `thenPrimIO` \ (I# x#) ->
219 returnPrimIO (x# ==# 0#))
222 bottom = error "eqStrPrefixBA"
224 eqCharStrPrefixBA :: Addr# -> ByteArray# -> Int# -> Int# -> Bool
225 eqCharStrPrefixBA a# b2# start# len# =
226 unsafePerformPrimIO (
227 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
228 (_ByteArray bottom b2#)
231 (I# len#) `thenPrimIO` \ (I# x#) ->
232 returnPrimIO (x# ==# 0#))
235 bottom = error "eqCharStrPrefixBA"
237 eqStrPrefixFO :: ForeignObj# -> ByteArray# -> Int# -> Int# -> Bool
238 eqStrPrefixFO fo# barr# start# len# =
239 unsafePerformPrimIO (
240 _casm_ ``%r=(int)strncmp((char *)%0+(int)%1,%2,%3); ''
243 (_ByteArray bottom barr#)
244 (I# len#) `thenPrimIO` \ (I# x#) ->
245 returnPrimIO (x# ==# 0#))
248 bottom = error "eqStrPrefixFO"
252 byteArrayToString :: _ByteArray Int -> String
253 byteArrayToString (_ByteArray (I# start#,I# end#) barr#) =
258 | otherwise = C# ch : unpack (nh# +# 1#)
260 ch = indexCharArray# barr# nh#
266 stringToByteArray :: String -> (_ByteArray Int)
267 stringToByteArray str = _runST (packStringST str)
269 packStringST :: [Char] -> _ST s (_ByteArray Int)
271 let len = length str in
274 packNCharsST :: Int -> [Char] -> _ST s (_ByteArray Int)
275 packNCharsST len@(I# length#) str =
277 allocate an array that will hold the string
278 (not forgetting the NUL byte at the end)
280 new_ps_array (length# +# 1#) `thenStrictlyST` \ ch_array ->
281 -- fill in packed string from "str"
282 fill_in ch_array 0# str `seqStrictlyST`
284 freeze_ps_array ch_array `thenStrictlyST` \ (_ByteArray _ frozen#) ->
285 returnStrictlyST (_ByteArray (0,len) frozen#)
287 fill_in :: _MutableByteArray s Int -> Int# -> [Char] -> _ST s ()
288 fill_in arr_in# idx [] =
289 write_ps_array arr_in# idx (chr# 0#) `seqStrictlyST`
292 fill_in arr_in# idx (C# c : cs) =
293 write_ps_array arr_in# idx c `seqStrictlyST`
294 fill_in arr_in# (idx +# 1#) cs