2 % (c) The GRASP/AQUA Project, Glasgow University, 1997
4 \section[PackBase]{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 system library
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]
37 unpackCStringFO, -- :: ForeignObj -> [Char]
38 unpackNBytesFO, -- :: ForeignObj -> Int -> [Char]
39 unpackCStringFO#, -- :: ForeignObj# -> [Char]
40 unpackNBytesFO#, -- :: ForeignObj# -> Int# -> [Char]
42 unpackFoldrCString#, -- **
43 unpackAppendCString# -- **
48 import {-# SOURCE #-} IOBase ( error )
49 import PrelList ( length )
56 %*********************************************************
58 \subsection{Unpacking Addrs}
60 %*********************************************************
62 Primitives for converting Addrs pointing to external
63 sequence of bytes into a list of @Char@s:
66 unpackCString :: Addr{- ptr. to NUL terminated string-} -> [Char]
67 unpackCString a@(A# addr) =
73 unpackCString# :: Addr# -> [Char]
78 | ch `eqChar#` '\0'# = []
79 | otherwise = C# ch : unpack (nh +# 1#)
81 ch = indexCharOffAddr# addr nh
83 unpackNBytes :: Addr -> Int -> [Char]
84 unpackNBytes (A# addr) (I# l) = unpackNBytes# addr l
86 unpackNBytesST :: Addr -> Int -> ST s [Char]
87 unpackNBytesST (A# addr) (I# l) = unpackNBytesST# addr l
89 unpackNBytes# :: Addr# -> Int# -> [Char]
90 -- This one is called by the compiler to unpack literal strings with NULs in them; rare.
91 unpackNBytes# addr len
96 | otherwise = C# ch : unpack (i +# 1#)
98 ch = indexCharOffAddr# addr i
100 unpackNBytesST# :: Addr# -> Int# -> ST s [Char]
101 unpackNBytesST# addr len
105 | i >=# len = return []
107 case indexCharOffAddr# addr i of
108 ch -> unpack (i +# 1#) >>= \ ls -> return (C# ch : ls)
113 %*********************************************************
115 \subsection{Unpacking Foreigns}
117 %*********************************************************
119 Primitives for converting Foreigns pointing to external
120 sequence of bytes into a list of @Char@s (a renamed version
124 unpackCStringFO :: ForeignObj -> [Char]
125 unpackCStringFO (ForeignObj fo#) = unpackCStringFO# fo#
127 unpackCStringFO# :: ForeignObj# -> [Char]
128 unpackCStringFO# fo {- ptr. to NUL terminated string-}
132 | ch `eqChar#` '\0'# = []
133 | otherwise = C# ch : unpack (nh +# 1#)
135 ch = indexCharOffForeignObj# fo nh
137 unpackNBytesFO :: ForeignObj -> Int -> [Char]
138 unpackNBytesFO (ForeignObj fo) (I# l) = unpackNBytesFO# fo l
140 unpackNBytesFO# :: ForeignObj# -> Int# -> [Char]
141 -- This one is called by the compiler to unpack literal strings with NULs in them; rare.
142 unpackNBytesFO# fo len
147 | otherwise = C# ch : unpack (i +# 1#)
149 ch = indexCharOffForeignObj# fo i
153 %********************************************************
155 \subsection{Unpacking ByteArrays}
157 %********************************************************
159 Converting byte arrays into list of chars:
162 unpackCStringBA :: ByteArray Int -> [Char]
163 unpackCStringBA (ByteArray (l@(I# l#),u@(I# u#)) bytes)
165 | otherwise = unpackCStringBA# bytes (u# -# l# +# 1#)
168 unpack until NUL or end of BA is reached, whatever comes first.
170 unpackCStringBA# :: ByteArray# -> Int# -> [Char]
171 unpackCStringBA# bytes len
176 ch `eqChar#` '\0'# = []
177 | otherwise = C# ch : unpack (nh +# 1#)
179 ch = indexCharArray# bytes nh
181 unpackNBytesBA :: ByteArray Int -> Int -> [Char]
182 unpackNBytesBA (ByteArray (l,u) bytes) i
183 = unpackNBytesBA# bytes len#
185 len# = case max 0 (min i len) of I# v# -> v#
189 unpackNBytesBA# :: ByteArray# -> Int# -> [Char]
190 unpackNBytesBA# bytes nh
195 | otherwise = C# ch : unpack (i +# 1#)
197 ch = indexCharArray# bytes i
201 %********************************************************
203 \subsection{Packing Strings}
205 %********************************************************
207 Converting a list of chars into a packed @ByteArray@ representation.
210 packCString# :: [Char] -> ByteArray#
211 packCString# str = case (packString str) of { ByteArray _ bytes -> bytes }
213 packString :: [Char] -> ByteArray Int
214 packString str = runST (packStringST str)
216 packStringST :: [Char] -> ST s (ByteArray Int)
218 let len = length str in
221 packNBytesST :: Int -> [Char] -> ST s (ByteArray Int)
222 packNBytesST len@(I# length#) str =
224 allocate an array that will hold the string
225 (not forgetting the NUL byte at the end)
227 new_ps_array (length# +# 1#) >>= \ ch_array ->
228 -- fill in packed string from "str"
229 fill_in ch_array 0# str >>
231 freeze_ps_array ch_array length#
233 fill_in :: MutableByteArray s Int -> Int# -> [Char] -> ST s ()
234 fill_in arr_in# idx [] =
235 write_ps_array arr_in# idx (chr# 0#) >>
238 fill_in arr_in# idx (C# c : cs) =
239 write_ps_array arr_in# idx c >>
240 fill_in arr_in# (idx +# 1#) cs
244 (Very :-) ``Specialised'' versions of some CharArray things...
247 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
248 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
249 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
251 new_ps_array size = ST $ \ s ->
252 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
253 STret s2# (MutableByteArray bot barr#) }
255 bot = error "new_ps_array"
257 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
258 case writeCharArray# barr# n ch s# of { s2# ->
261 -- same as unsafeFreezeByteArray
262 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
263 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
264 STret s2# (ByteArray (0,I# len#) frozen#) }
268 %********************************************************
272 %********************************************************
274 The compiler may emit these two
277 unpackAppendCString# :: Addr# -> [Char] -> [Char]
278 unpackAppendCString# addr rest
282 | ch `eqChar#` '\0'# = rest
283 | otherwise = C# ch : unpack (nh +# 1#)
285 ch = indexCharOffAddr# addr nh
287 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
288 unpackFoldrCString# addr f z
292 | ch `eqChar#` '\0'# = z
293 | otherwise = C# ch `f` unpack (nh +# 1#)
295 ch = indexCharOffAddr# addr nh