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 unpackCString#, -- :: Addr# -> [Char] **
29 unpackNBytes#, -- :: Addr# -> Int# -> [Char] **
30 unpackNBytesST#, -- :: Addr# -> Int# -> ST s [Char]
32 unpackCStringBA, -- :: ByteArray Int -> [Char]
33 unpackNBytesBA, -- :: ByteArray Int -> Int -> [Char]
34 unpackCStringBA#, -- :: ByteArray# -> Int# -> [Char]
35 unpackNBytesBA#, -- :: ByteArray# -> Int# -> [Char]
38 unpackFoldrCString#, -- **
39 unpackAppendCString#, -- **
41 new_ps_array, -- Int# -> ST s (MutableByteArray s Int)
42 write_ps_array, -- MutableByteArray s Int -> Int# -> Char# -> ST s ()
43 freeze_ps_array -- MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
50 import {-# SOURCE #-} PrelErr ( error )
51 import PrelList ( length )
58 %*********************************************************
60 \subsection{Unpacking Addrs}
62 %*********************************************************
64 Primitives for converting Addrs pointing to external
65 sequence of bytes into a list of @Char@s:
68 unpackCString :: Addr{- ptr. to NUL terminated string-} -> [Char]
69 unpackCString a@(A# addr) =
75 unpackCString# :: Addr# -> [Char]
80 | ch `eqChar#` '\0'# = []
81 | otherwise = C# ch : unpack (nh +# 1#)
83 ch = indexCharOffAddr# addr nh
85 unpackNBytes :: Addr -> Int -> [Char]
86 unpackNBytes (A# addr) (I# l) = unpackNBytes# addr l
88 unpackNBytesST :: Addr -> Int -> ST s [Char]
89 unpackNBytesST (A# addr) (I# l) = unpackNBytesST# addr l
91 unpackNBytes# :: Addr# -> Int# -> [Char]
92 -- This one is called by the compiler to unpack literal strings with NULs in them; rare.
93 unpackNBytes# addr len
98 | otherwise = C# ch : unpack (i +# 1#)
100 ch = indexCharOffAddr# addr i
102 unpackNBytesST# :: Addr# -> Int# -> ST s [Char]
103 unpackNBytesST# addr len
107 | i >=# len = return []
109 case indexCharOffAddr# addr i of
110 ch -> unpack (i +# 1#) >>= \ ls -> return (C# ch : ls)
114 %********************************************************
116 \subsection{Unpacking ByteArrays}
118 %********************************************************
120 Converting byte arrays into list of chars:
123 unpackCStringBA :: ByteArray Int -> [Char]
124 unpackCStringBA (ByteArray (l@(I# l#),u@(I# u#)) bytes)
126 | otherwise = unpackCStringBA# bytes (u# -# l# +# 1#)
129 unpack until NUL or end of BA is reached, whatever comes first.
131 unpackCStringBA# :: ByteArray# -> Int# -> [Char]
132 unpackCStringBA# bytes len
137 ch `eqChar#` '\0'# = []
138 | otherwise = C# ch : unpack (nh +# 1#)
140 ch = indexCharArray# bytes nh
142 unpackNBytesBA :: ByteArray Int -> Int -> [Char]
143 unpackNBytesBA (ByteArray (l,u) bytes) i
144 = unpackNBytesBA# bytes len#
146 len# = case max 0 (min i len) of I# v# -> v#
150 unpackNBytesBA# :: ByteArray# -> Int# -> [Char]
151 unpackNBytesBA# bytes nh
156 | otherwise = C# ch : unpack (i +# 1#)
158 ch = indexCharArray# bytes i
162 %********************************************************
164 \subsection{Packing Strings}
166 %********************************************************
168 Converting a list of chars into a packed @ByteArray@ representation.
171 packCString# :: [Char] -> ByteArray#
172 packCString# str = case (packString str) of { ByteArray _ bytes -> bytes }
174 packString :: [Char] -> ByteArray Int
175 packString str = runST (packStringST str)
177 packStringST :: [Char] -> ST s (ByteArray Int)
179 let len = length str in
182 packNBytesST :: Int -> [Char] -> ST s (ByteArray Int)
183 packNBytesST len@(I# length#) str =
185 allocate an array that will hold the string
186 (not forgetting the NUL byte at the end)
188 new_ps_array (length# +# 1#) >>= \ ch_array ->
189 -- fill in packed string from "str"
190 fill_in ch_array 0# str >>
192 freeze_ps_array ch_array length#
194 fill_in :: MutableByteArray s Int -> Int# -> [Char] -> ST s ()
195 fill_in arr_in# idx [] =
196 write_ps_array arr_in# idx (chr# 0#) >>
199 fill_in arr_in# idx (C# c : cs) =
200 write_ps_array arr_in# idx c >>
201 fill_in arr_in# (idx +# 1#) cs
205 (Very :-) ``Specialised'' versions of some CharArray things...
208 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
209 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
210 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
212 new_ps_array size = ST $ \ s ->
213 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
214 STret s2# (MutableByteArray bot barr#) }
216 bot = error "new_ps_array"
218 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
219 case writeCharArray# barr# n ch s# of { s2# ->
222 -- same as unsafeFreezeByteArray
223 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
224 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
225 STret s2# (ByteArray (0,I# len#) frozen#) }
229 %********************************************************
233 %********************************************************
235 The compiler may emit these two
238 unpackAppendCString# :: Addr# -> [Char] -> [Char]
239 unpackAppendCString# addr rest
243 | ch `eqChar#` '\0'# = rest
244 | otherwise = C# ch : unpack (nh +# 1#)
246 ch = indexCharOffAddr# addr nh
248 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
249 unpackFoldrCString# addr f z
253 | ch `eqChar#` '\0'# = z
254 | otherwise = C# ch `f` unpack (nh +# 1#)
256 ch = indexCharOffAddr# addr nh