2 % (c) The GRASP/AQUA Project, Glasgow University, 1997
4 \section[PrelPack]{Packing/unpacking bytes}
6 This module provides a small set of low-level functions for packing
7 and unpacking a chunk of bytes. Used by code emitted by the compiler
8 plus the prelude libraries.
10 The programmer level view of packed strings is provided by a GHC
11 system library PackedString.
14 {-# OPTIONS -fno-implicit-prelude #-}
18 -- (**) - emitted by compiler.
20 packCString#, -- :: [Char] -> ByteArray# **
21 packString, -- :: [Char] -> ByteArray Int
22 packStringST, -- :: [Char] -> ST s (ByteArray Int)
23 packNBytesST, -- :: Int -> [Char] -> ST s (ByteArray Int)
25 unpackCString, -- :: Addr -> [Char]
26 unpackCStringST, -- :: Addr -> ST s [Char]
27 unpackNBytes, -- :: Addr -> Int -> [Char]
28 unpackNBytesST, -- :: Addr -> Int -> ST s [Char]
29 unpackNBytesAccST, -- :: Addr -> Int -> [Char] -> ST s [Char]
30 unpackCString#, -- :: Addr# -> [Char] **
31 unpackNBytes#, -- :: Addr# -> Int# -> [Char] **
32 unpackNBytesST#, -- :: Addr# -> Int# -> ST s [Char]
34 unpackCStringBA, -- :: ByteArray Int -> [Char]
35 unpackNBytesBA, -- :: ByteArray Int -> Int -> [Char]
36 unpackCStringBA#, -- :: ByteArray# -> Int# -> [Char]
37 unpackNBytesBA#, -- :: ByteArray# -> Int# -> [Char]
40 unpackFoldrCString#, -- **
41 unpackAppendCString#, -- **
43 new_ps_array, -- Int# -> ST s (MutableByteArray s Int)
44 write_ps_array, -- MutableByteArray s Int -> Int# -> Char# -> ST s ()
45 freeze_ps_array -- MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
51 import {-# SOURCE #-} PrelErr ( error )
52 import PrelList ( length )
59 %*********************************************************
61 \subsection{Unpacking Addrs}
63 %*********************************************************
65 Primitives for converting Addrs pointing to external
66 sequence of bytes into a list of @Char@s:
69 unpackCString :: Addr{- ptr. to NUL terminated string-} -> [Char]
70 unpackCString a@(A# addr)
72 | otherwise = unpackCString# addr
74 unpackCStringST :: Addr{- ptr. to NUL terminated string-} -> ST s [Char]
75 unpackCStringST a@(A# addr)
76 | a == nullAddr = return []
77 | otherwise = unpack 0#
80 | ch `eqChar#` '\0'# = return []
82 ls <- unpack (nh +# 1#)
83 return ((C# ch ) : ls)
85 ch = indexCharOffAddr# addr nh
87 unpackCString# :: Addr# -> [Char]
92 | ch `eqChar#` '\0'# = []
93 | otherwise = C# ch : unpack (nh +# 1#)
95 ch = indexCharOffAddr# addr nh
97 unpackNBytes :: Addr -> Int -> [Char]
98 unpackNBytes (A# addr) (I# l) = unpackNBytes# addr l
100 unpackNBytesST :: Addr -> Int -> ST s [Char]
101 unpackNBytesST (A# addr) (I# l) = unpackNBytesAccST# addr l []
103 unpackNBytesAccST :: Addr -> Int -> [Char] -> ST s [Char]
104 unpackNBytesAccST (A# addr) (I# l) rest = unpackNBytesAccST# addr l rest
106 unpackNBytes# :: Addr# -> Int# -> [Char]
107 -- This one is called by the compiler to unpack literal strings with NULs in them; rare.
109 unpackNBytes# _addr 0# = []
110 unpackNBytes# addr len# = unpack [] (len# -# 1#)
115 case indexCharOffAddr# addr i# of
116 ch -> unpack (C# ch : acc) (i# -# 1#)
118 unpackNBytesST# :: Addr# -> Int# -> ST s [Char]
119 unpackNBytesST# addr# l# = unpackNBytesAccST# addr# l# []
121 unpackNBytesAccST# :: Addr# -> Int# -> [Char] -> ST s [Char]
122 unpackNBytesAccST# _addr 0# rest = return rest
123 unpackNBytesAccST# addr len# rest = unpack rest (len# -# 1#)
126 | i# <# 0# = return acc
128 case indexCharOffAddr# addr i# of
129 ch -> unpack (C# ch : acc) (i# -# 1#)
133 %********************************************************
135 \subsection{Unpacking ByteArrays}
137 %********************************************************
139 Converting byte arrays into list of chars:
142 unpackCStringBA :: ByteArray Int -> [Char]
143 unpackCStringBA (ByteArray (l@(I# l#),u@(I# u#)) bytes)
145 | otherwise = unpackCStringBA# bytes (u# -# l# +# 1#)
148 unpack until NUL or end of BA is reached, whatever comes first.
150 unpackCStringBA# :: ByteArray# -> Int# -> [Char]
151 unpackCStringBA# bytes len
156 ch `eqChar#` '\0'# = []
157 | otherwise = C# ch : unpack (nh +# 1#)
159 ch = indexCharArray# bytes nh
161 unpackNBytesBA :: ByteArray Int -> Int -> [Char]
162 unpackNBytesBA (ByteArray (l,u) bytes) i
163 = unpackNBytesBA# bytes len#
165 len# = case max 0 (min i len) of I# v# -> v#
169 unpackNBytesBA# :: ByteArray# -> Int# -> [Char]
170 unpackNBytesBA# _bytes 0# = []
171 unpackNBytesBA# bytes len# = unpack [] (len# -# 1#)
176 case indexCharArray# bytes i# of
177 ch -> unpack (C# ch : acc) (i# -# 1#)
182 %********************************************************
184 \subsection{Packing Strings}
186 %********************************************************
188 Converting a list of chars into a packed @ByteArray@ representation.
191 packCString# :: [Char] -> ByteArray#
192 packCString# str = case (packString str) of { ByteArray _ bytes -> bytes }
194 packString :: [Char] -> ByteArray Int
195 packString str = runST (packStringST str)
197 packStringST :: [Char] -> ST s (ByteArray Int)
199 let len = length str in
202 packNBytesST :: Int -> [Char] -> ST s (ByteArray Int)
203 packNBytesST (I# length#) str =
205 allocate an array that will hold the string
206 (not forgetting the NUL byte at the end)
208 new_ps_array (length# +# 1#) >>= \ ch_array ->
209 -- fill in packed string from "str"
210 fill_in ch_array 0# str >>
212 freeze_ps_array ch_array length#
214 fill_in :: MutableByteArray s Int -> Int# -> [Char] -> ST s ()
215 fill_in arr_in# idx [] =
216 write_ps_array arr_in# idx (chr# 0#) >>
219 fill_in arr_in# idx (C# c : cs) =
220 write_ps_array arr_in# idx c >>
221 fill_in arr_in# (idx +# 1#) cs
225 (Very :-) ``Specialised'' versions of some CharArray things...
228 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
229 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
230 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
232 new_ps_array size = ST $ \ s ->
233 case (newCharArray# size s) of { (# s2#, barr# #) ->
234 (# s2#, MutableByteArray bot barr# #) }
236 bot = error "new_ps_array"
238 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
239 case writeCharArray# barr# n ch s# of { s2# ->
242 -- same as unsafeFreezeByteArray
243 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
244 case unsafeFreezeByteArray# arr# s# of { (# s2#, frozen# #) ->
245 (# s2#, ByteArray (0,I# len#) frozen# #) }
249 %********************************************************
253 %********************************************************
255 The compiler may emit these two
258 unpackAppendCString# :: Addr# -> [Char] -> [Char]
259 unpackAppendCString# addr rest
263 | ch `eqChar#` '\0'# = rest
264 | otherwise = C# ch : unpack (nh +# 1#)
266 ch = indexCharOffAddr# addr nh
268 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
269 unpackFoldrCString# addr f z
273 | ch `eqChar#` '\0'# = z
274 | otherwise = C# ch `f` unpack (nh +# 1#)
276 ch = indexCharOffAddr# addr nh