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 )
55 import PrelUnsafeST ( runST )
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) = unpackNBytesST# addr l
92 unpackNBytes# :: Addr# -> Int# -> [Char]
93 -- This one is called by the compiler to unpack literal strings with NULs in them; rare.
94 unpackNBytes# addr len
99 | otherwise = C# ch : unpack (i +# 1#)
101 ch = indexCharOffAddr# addr i
103 unpackNBytesST# :: Addr# -> Int# -> ST s [Char]
104 unpackNBytesST# addr len
108 | i >=# len = return []
110 case indexCharOffAddr# addr i of
111 ch -> unpack (i +# 1#) >>= \ ls -> return (C# ch : ls)
115 %********************************************************
117 \subsection{Unpacking ByteArrays}
119 %********************************************************
121 Converting byte arrays into list of chars:
124 unpackCStringBA :: ByteArray Int -> [Char]
125 unpackCStringBA (ByteArray (l@(I# l#),u@(I# u#)) bytes)
127 | otherwise = unpackCStringBA# bytes (u# -# l# +# 1#)
130 unpack until NUL or end of BA is reached, whatever comes first.
132 unpackCStringBA# :: ByteArray# -> Int# -> [Char]
133 unpackCStringBA# bytes len
138 ch `eqChar#` '\0'# = []
139 | otherwise = C# ch : unpack (nh +# 1#)
141 ch = indexCharArray# bytes nh
143 unpackNBytesBA :: ByteArray Int -> Int -> [Char]
144 unpackNBytesBA (ByteArray (l,u) bytes) i
145 = unpackNBytesBA# bytes len#
147 len# = case max 0 (min i len) of I# v# -> v#
151 unpackNBytesBA# :: ByteArray# -> Int# -> [Char]
152 unpackNBytesBA# bytes nh
157 | otherwise = C# ch : unpack (i +# 1#)
159 ch = indexCharArray# bytes i
163 %********************************************************
165 \subsection{Packing Strings}
167 %********************************************************
169 Converting a list of chars into a packed @ByteArray@ representation.
172 packCString# :: [Char] -> ByteArray#
173 packCString# str = case (packString str) of { ByteArray _ bytes -> bytes }
175 packString :: [Char] -> ByteArray Int
176 packString str = runST (packStringST str)
178 packStringST :: [Char] -> ST s (ByteArray Int)
180 let len = length str in
183 packNBytesST :: Int -> [Char] -> ST s (ByteArray Int)
184 packNBytesST len@(I# length#) str =
186 allocate an array that will hold the string
187 (not forgetting the NUL byte at the end)
189 new_ps_array (length# +# 1#) >>= \ ch_array ->
190 -- fill in packed string from "str"
191 fill_in ch_array 0# str >>
193 freeze_ps_array ch_array length#
195 fill_in :: MutableByteArray s Int -> Int# -> [Char] -> ST s ()
196 fill_in arr_in# idx [] =
197 write_ps_array arr_in# idx (chr# 0#) >>
200 fill_in arr_in# idx (C# c : cs) =
201 write_ps_array arr_in# idx c >>
202 fill_in arr_in# (idx +# 1#) cs
206 (Very :-) ``Specialised'' versions of some CharArray things...
209 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
210 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
211 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
213 new_ps_array size = ST $ \ s ->
214 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
215 STret s2# (MutableByteArray bot barr#) }
217 bot = error "new_ps_array"
219 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
220 case writeCharArray# barr# n ch s# of { s2# ->
223 -- same as unsafeFreezeByteArray
224 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
225 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
226 STret s2# (ByteArray (0,I# len#) frozen#) }
230 %********************************************************
234 %********************************************************
236 The compiler may emit these two
239 unpackAppendCString# :: Addr# -> [Char] -> [Char]
240 unpackAppendCString# addr rest
244 | ch `eqChar#` '\0'# = rest
245 | otherwise = C# ch : unpack (nh +# 1#)
247 ch = indexCharOffAddr# addr nh
249 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
250 unpackFoldrCString# addr f z
254 | ch `eqChar#` '\0'# = z
255 | otherwise = C# ch `f` unpack (nh +# 1#)
257 ch = indexCharOffAddr# addr nh