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]
38 unpackFoldrCString#, -- **
39 unpackAppendCString# -- **
44 import {-# SOURCE #-} Error ( error )
45 import PrelList ( length )
52 %*********************************************************
54 \subsection{Unpacking Addrs}
56 %*********************************************************
58 Primitives for converting Addrs pointing to external
59 sequence of bytes into a list of @Char@s:
62 unpackCString :: Addr{- ptr. to NUL terminated string-} -> [Char]
63 unpackCString a@(A# addr) =
69 unpackCString# :: Addr# -> [Char]
74 | ch `eqChar#` '\0'# = []
75 | otherwise = C# ch : unpack (nh +# 1#)
77 ch = indexCharOffAddr# addr nh
79 unpackNBytes :: Addr -> Int -> [Char]
80 unpackNBytes (A# addr) (I# l) = unpackNBytes# addr l
82 unpackNBytesST :: Addr -> Int -> ST s [Char]
83 unpackNBytesST (A# addr) (I# l) = unpackNBytesST# addr l
85 unpackNBytes# :: Addr# -> Int# -> [Char]
86 -- This one is called by the compiler to unpack literal strings with NULs in them; rare.
87 unpackNBytes# addr len
92 | otherwise = C# ch : unpack (i +# 1#)
94 ch = indexCharOffAddr# addr i
96 unpackNBytesST# :: Addr# -> Int# -> ST s [Char]
97 unpackNBytesST# addr len
101 | i >=# len = return []
103 case indexCharOffAddr# addr i of
104 ch -> unpack (i +# 1#) >>= \ ls -> return (C# ch : ls)
108 %********************************************************
110 \subsection{Unpacking ByteArrays}
112 %********************************************************
114 Converting byte arrays into list of chars:
117 unpackCStringBA :: ByteArray Int -> [Char]
118 unpackCStringBA (ByteArray (l@(I# l#),u@(I# u#)) bytes)
120 | otherwise = unpackCStringBA# bytes (u# -# l# +# 1#)
123 unpack until NUL or end of BA is reached, whatever comes first.
125 unpackCStringBA# :: ByteArray# -> Int# -> [Char]
126 unpackCStringBA# bytes len
131 ch `eqChar#` '\0'# = []
132 | otherwise = C# ch : unpack (nh +# 1#)
134 ch = indexCharArray# bytes nh
136 unpackNBytesBA :: ByteArray Int -> Int -> [Char]
137 unpackNBytesBA (ByteArray (l,u) bytes) i
138 = unpackNBytesBA# bytes len#
140 len# = case max 0 (min i len) of I# v# -> v#
144 unpackNBytesBA# :: ByteArray# -> Int# -> [Char]
145 unpackNBytesBA# bytes nh
150 | otherwise = C# ch : unpack (i +# 1#)
152 ch = indexCharArray# bytes i
156 %********************************************************
158 \subsection{Packing Strings}
160 %********************************************************
162 Converting a list of chars into a packed @ByteArray@ representation.
165 packCString# :: [Char] -> ByteArray#
166 packCString# str = case (packString str) of { ByteArray _ bytes -> bytes }
168 packString :: [Char] -> ByteArray Int
169 packString str = runST (packStringST str)
171 packStringST :: [Char] -> ST s (ByteArray Int)
173 let len = length str in
176 packNBytesST :: Int -> [Char] -> ST s (ByteArray Int)
177 packNBytesST len@(I# length#) str =
179 allocate an array that will hold the string
180 (not forgetting the NUL byte at the end)
182 new_ps_array (length# +# 1#) >>= \ ch_array ->
183 -- fill in packed string from "str"
184 fill_in ch_array 0# str >>
186 freeze_ps_array ch_array length#
188 fill_in :: MutableByteArray s Int -> Int# -> [Char] -> ST s ()
189 fill_in arr_in# idx [] =
190 write_ps_array arr_in# idx (chr# 0#) >>
193 fill_in arr_in# idx (C# c : cs) =
194 write_ps_array arr_in# idx c >>
195 fill_in arr_in# (idx +# 1#) cs
199 (Very :-) ``Specialised'' versions of some CharArray things...
202 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
203 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
204 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
206 new_ps_array size = ST $ \ s ->
207 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
208 STret s2# (MutableByteArray bot barr#) }
210 bot = error "new_ps_array"
212 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
213 case writeCharArray# barr# n ch s# of { s2# ->
216 -- same as unsafeFreezeByteArray
217 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
218 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
219 STret s2# (ByteArray (0,I# len#) frozen#) }
223 %********************************************************
227 %********************************************************
229 The compiler may emit these two
232 unpackAppendCString# :: Addr# -> [Char] -> [Char]
233 unpackAppendCString# addr rest
237 | ch `eqChar#` '\0'# = rest
238 | otherwise = C# ch : unpack (nh +# 1#)
240 ch = indexCharOffAddr# addr nh
242 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
243 unpackFoldrCString# addr f z
247 | ch `eqChar#` '\0'# = z
248 | otherwise = C# ch `f` unpack (nh +# 1#)
250 ch = indexCharOffAddr# addr nh