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 unpackNBytes, -- :: Addr -> Int -> [Char]
27 unpackNBytesST, -- :: Addr -> Int -> ST s [Char]
28 unpackNBytesAccST, -- :: Addr -> Int -> [Char] -> ST s [Char]
29 unpackCString#, -- :: Addr# -> [Char] **
30 unpackNBytes#, -- :: Addr# -> Int# -> [Char] **
31 unpackNBytesST#, -- :: Addr# -> Int# -> ST s [Char]
33 unpackCStringBA, -- :: ByteArray Int -> [Char]
34 unpackNBytesBA, -- :: ByteArray Int -> Int -> [Char]
35 unpackCStringBA#, -- :: ByteArray# -> Int# -> [Char]
36 unpackNBytesBA#, -- :: ByteArray# -> Int# -> [Char]
39 unpackFoldrCString#, -- **
40 unpackAppendCString#, -- **
42 new_ps_array, -- Int# -> ST s (MutableByteArray s Int)
43 write_ps_array, -- MutableByteArray s Int -> Int# -> Char# -> ST s ()
44 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) =
76 unpackCString# :: Addr# -> [Char]
81 | ch `eqChar#` '\0'# = []
82 | otherwise = C# ch : unpack (nh +# 1#)
84 ch = indexCharOffAddr# addr nh
86 unpackNBytes :: Addr -> Int -> [Char]
87 unpackNBytes (A# addr) (I# l) = unpackNBytes# addr l
89 unpackNBytesST :: Addr -> Int -> ST s [Char]
90 unpackNBytesST (A# addr) (I# l) = unpackNBytesAccST# addr l []
92 unpackNBytesAccST :: Addr -> Int -> [Char] -> ST s [Char]
93 unpackNBytesAccST (A# addr) (I# l) rest = unpackNBytesAccST# addr l rest
95 unpackNBytes# :: Addr# -> Int# -> [Char]
96 -- This one is called by the compiler to unpack literal strings with NULs in them; rare.
98 unpackNBytes# addr 0# = []
99 unpackNBytes# addr len# = unpack [] (len# -# 1#)
104 case indexCharOffAddr# addr i# of
105 ch -> unpack (C# ch : acc) (i# -# 1#)
107 unpackNBytesST# :: Addr# -> Int# -> ST s [Char]
108 unpackNBytesST# addr# l# = unpackNBytesAccST# addr# l# []
110 unpackNBytesAccST# :: Addr# -> Int# -> [Char] -> ST s [Char]
111 unpackNBytesAccST# addr 0# rest = return rest
112 unpackNBytesAccST# addr len# rest = unpack rest (len# -# 1#)
115 | i# <# 0# = return acc
117 case indexCharOffAddr# addr i# of
118 ch -> unpack (C# ch : acc) (i# -# 1#)
122 %********************************************************
124 \subsection{Unpacking ByteArrays}
126 %********************************************************
128 Converting byte arrays into list of chars:
131 unpackCStringBA :: ByteArray Int -> [Char]
132 unpackCStringBA (ByteArray (l@(I# l#),u@(I# u#)) bytes)
134 | otherwise = unpackCStringBA# bytes (u# -# l# +# 1#)
137 unpack until NUL or end of BA is reached, whatever comes first.
139 unpackCStringBA# :: ByteArray# -> Int# -> [Char]
140 unpackCStringBA# bytes len
145 ch `eqChar#` '\0'# = []
146 | otherwise = C# ch : unpack (nh +# 1#)
148 ch = indexCharArray# bytes nh
150 unpackNBytesBA :: ByteArray Int -> Int -> [Char]
151 unpackNBytesBA (ByteArray (l,u) bytes) i
152 = unpackNBytesBA# bytes len#
154 len# = case max 0 (min i len) of I# v# -> v#
158 unpackNBytesBA# :: ByteArray# -> Int# -> [Char]
159 unpackNBytesBA# bytes 0# = []
160 unpackNBytesBA# bytes len# = unpack [] (len# -# 1#)
165 case indexCharArray# bytes i# of
166 ch -> unpack (C# ch : acc) (i# -# 1#)
171 %********************************************************
173 \subsection{Packing Strings}
175 %********************************************************
177 Converting a list of chars into a packed @ByteArray@ representation.
180 packCString# :: [Char] -> ByteArray#
181 packCString# str = case (packString str) of { ByteArray _ bytes -> bytes }
183 packString :: [Char] -> ByteArray Int
184 packString str = runST (packStringST str)
186 packStringST :: [Char] -> ST s (ByteArray Int)
188 let len = length str in
191 packNBytesST :: Int -> [Char] -> ST s (ByteArray Int)
192 packNBytesST len@(I# length#) str =
194 allocate an array that will hold the string
195 (not forgetting the NUL byte at the end)
197 new_ps_array (length# +# 1#) >>= \ ch_array ->
198 -- fill in packed string from "str"
199 fill_in ch_array 0# str >>
201 freeze_ps_array ch_array length#
203 fill_in :: MutableByteArray s Int -> Int# -> [Char] -> ST s ()
204 fill_in arr_in# idx [] =
205 write_ps_array arr_in# idx (chr# 0#) >>
208 fill_in arr_in# idx (C# c : cs) =
209 write_ps_array arr_in# idx c >>
210 fill_in arr_in# (idx +# 1#) cs
214 (Very :-) ``Specialised'' versions of some CharArray things...
217 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
218 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
219 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
221 new_ps_array size = ST $ \ s ->
222 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
223 STret s2# (MutableByteArray bot barr#) }
225 bot = error "new_ps_array"
227 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
228 case writeCharArray# barr# n ch s# of { s2# ->
231 -- same as unsafeFreezeByteArray
232 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
233 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
234 STret s2# (ByteArray (0,I# len#) frozen#) }
238 %********************************************************
242 %********************************************************
244 The compiler may emit these two
247 unpackAppendCString# :: Addr# -> [Char] -> [Char]
248 unpackAppendCString# addr rest
252 | ch `eqChar#` '\0'# = rest
253 | otherwise = C# ch : unpack (nh +# 1#)
255 ch = indexCharOffAddr# addr nh
257 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
258 unpackFoldrCString# addr f z
262 | ch `eqChar#` '\0'# = z
263 | otherwise = C# ch `f` unpack (nh +# 1#)
265 ch = indexCharOffAddr# addr nh