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 )
49 import UnsafeST ( runST )
53 %*********************************************************
55 \subsection{Unpacking Addrs}
57 %*********************************************************
59 Primitives for converting Addrs pointing to external
60 sequence of bytes into a list of @Char@s:
63 unpackCString :: Addr{- ptr. to NUL terminated string-} -> [Char]
64 unpackCString a@(A# addr) =
70 unpackCString# :: Addr# -> [Char]
75 | ch `eqChar#` '\0'# = []
76 | otherwise = C# ch : unpack (nh +# 1#)
78 ch = indexCharOffAddr# addr nh
80 unpackNBytes :: Addr -> Int -> [Char]
81 unpackNBytes (A# addr) (I# l) = unpackNBytes# addr l
83 unpackNBytesST :: Addr -> Int -> ST s [Char]
84 unpackNBytesST (A# addr) (I# l) = unpackNBytesST# addr l
86 unpackNBytes# :: Addr# -> Int# -> [Char]
87 -- This one is called by the compiler to unpack literal strings with NULs in them; rare.
88 unpackNBytes# addr len
93 | otherwise = C# ch : unpack (i +# 1#)
95 ch = indexCharOffAddr# addr i
97 unpackNBytesST# :: Addr# -> Int# -> ST s [Char]
98 unpackNBytesST# addr len
102 | i >=# len = return []
104 case indexCharOffAddr# addr i of
105 ch -> unpack (i +# 1#) >>= \ ls -> return (C# ch : ls)
109 %********************************************************
111 \subsection{Unpacking ByteArrays}
113 %********************************************************
115 Converting byte arrays into list of chars:
118 unpackCStringBA :: ByteArray Int -> [Char]
119 unpackCStringBA (ByteArray (l@(I# l#),u@(I# u#)) bytes)
121 | otherwise = unpackCStringBA# bytes (u# -# l# +# 1#)
124 unpack until NUL or end of BA is reached, whatever comes first.
126 unpackCStringBA# :: ByteArray# -> Int# -> [Char]
127 unpackCStringBA# bytes len
132 ch `eqChar#` '\0'# = []
133 | otherwise = C# ch : unpack (nh +# 1#)
135 ch = indexCharArray# bytes nh
137 unpackNBytesBA :: ByteArray Int -> Int -> [Char]
138 unpackNBytesBA (ByteArray (l,u) bytes) i
139 = unpackNBytesBA# bytes len#
141 len# = case max 0 (min i len) of I# v# -> v#
145 unpackNBytesBA# :: ByteArray# -> Int# -> [Char]
146 unpackNBytesBA# bytes nh
151 | otherwise = C# ch : unpack (i +# 1#)
153 ch = indexCharArray# bytes i
157 %********************************************************
159 \subsection{Packing Strings}
161 %********************************************************
163 Converting a list of chars into a packed @ByteArray@ representation.
166 packCString# :: [Char] -> ByteArray#
167 packCString# str = case (packString str) of { ByteArray _ bytes -> bytes }
169 packString :: [Char] -> ByteArray Int
170 packString str = runST (packStringST str)
172 packStringST :: [Char] -> ST s (ByteArray Int)
174 let len = length str in
177 packNBytesST :: Int -> [Char] -> ST s (ByteArray Int)
178 packNBytesST len@(I# length#) str =
180 allocate an array that will hold the string
181 (not forgetting the NUL byte at the end)
183 new_ps_array (length# +# 1#) >>= \ ch_array ->
184 -- fill in packed string from "str"
185 fill_in ch_array 0# str >>
187 freeze_ps_array ch_array length#
189 fill_in :: MutableByteArray s Int -> Int# -> [Char] -> ST s ()
190 fill_in arr_in# idx [] =
191 write_ps_array arr_in# idx (chr# 0#) >>
194 fill_in arr_in# idx (C# c : cs) =
195 write_ps_array arr_in# idx c >>
196 fill_in arr_in# (idx +# 1#) cs
200 (Very :-) ``Specialised'' versions of some CharArray things...
203 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
204 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
205 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
207 new_ps_array size = ST $ \ s ->
208 case (newCharArray# size s) of { StateAndMutableByteArray# s2# barr# ->
209 STret s2# (MutableByteArray bot barr#) }
211 bot = error "new_ps_array"
213 write_ps_array (MutableByteArray _ barr#) n ch = ST $ \ s# ->
214 case writeCharArray# barr# n ch s# of { s2# ->
217 -- same as unsafeFreezeByteArray
218 freeze_ps_array (MutableByteArray _ arr#) len# = ST $ \ s# ->
219 case unsafeFreezeByteArray# arr# s# of { StateAndByteArray# s2# frozen# ->
220 STret s2# (ByteArray (0,I# len#) frozen#) }
224 %********************************************************
228 %********************************************************
230 The compiler may emit these two
233 unpackAppendCString# :: Addr# -> [Char] -> [Char]
234 unpackAppendCString# addr rest
238 | ch `eqChar#` '\0'# = rest
239 | otherwise = C# ch : unpack (nh +# 1#)
241 ch = indexCharOffAddr# addr nh
243 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
244 unpackFoldrCString# addr f z
248 | ch `eqChar#` '\0'# = z
249 | otherwise = C# ch `f` unpack (nh +# 1#)
251 ch = indexCharOffAddr# addr nh