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 unpackCStringST, -- :: Addr -> ST s [Char]
27 unpackNBytes, -- :: Addr -> Int -> [Char]
28 unpackNBytesST, -- :: Addr -> Int -> ST s [Char]
29 unpackNBytesAccST, -- :: Addr -> Int -> [Char] -> ST s [Char]
30 unpackCString#, -- :: Addr# -> [Char] **
31 unpackNBytes#, -- :: Addr# -> Int# -> [Char] **
32 unpackNBytesST#, -- :: Addr# -> Int# -> ST s [Char]
34 unpackCStringBA, -- :: ByteArray Int -> [Char]
35 unpackNBytesBA, -- :: ByteArray Int -> Int -> [Char]
36 unpackCStringBA#, -- :: ByteArray# -> Int# -> [Char]
37 unpackNBytesBA#, -- :: ByteArray# -> Int# -> [Char]
40 unpackFoldrCString#, -- **
41 unpackAppendCString#, -- **
43 new_ps_array, -- Int# -> ST s (MutableByteArray s Int)
44 write_ps_array, -- MutableByteArray s Int -> Int# -> Char# -> ST s ()
45 freeze_ps_array -- MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
51 import {-# SOURCE #-} PrelErr ( error )
52 import PrelList ( length )
61 %*********************************************************
63 \subsection{Unpacking Addrs}
65 %*********************************************************
67 Primitives for converting Addrs pointing to external
68 sequence of bytes into a list of @Char@s:
71 unpackCString :: Addr -> [Char]
72 unpackCString a@(A# addr)
74 | otherwise = unpackCString# addr
76 unpackNBytes :: Addr -> Int -> [Char]
77 unpackNBytes (A# addr) (I# l) = unpackNBytes# addr l
79 unpackCStringST :: Addr{- ptr. to NUL terminated string-} -> ST s [Char]
80 unpackCStringST a@(A# addr)
81 | a == nullAddr = return []
82 | otherwise = unpack 0#
85 | ch `eqChar#` '\0'# = return []
87 ls <- unpack (nh +# 1#)
88 return ((C# ch ) : ls)
90 ch = indexCharOffAddr# addr nh
92 unpackNBytesST :: Addr -> Int -> ST s [Char]
93 unpackNBytesST (A# addr) (I# l) = unpackNBytesAccST# addr l []
95 unpackNBytesAccST :: Addr -> Int -> [Char] -> ST s [Char]
96 unpackNBytesAccST (A# addr) (I# l) rest = unpackNBytesAccST# addr l rest
98 unpackNBytesST# :: Addr# -> Int# -> ST s [Char]
99 unpackNBytesST# addr# l# = unpackNBytesAccST# addr# l# []
101 unpackNBytesAccST# :: Addr# -> Int# -> [Char] -> ST s [Char]
102 unpackNBytesAccST# _addr 0# rest = return rest
103 unpackNBytesAccST# addr len# rest = unpack rest (len# -# 1#)
106 | i# <# 0# = return acc
108 case indexCharOffAddr# addr i# of
109 ch -> unpack (C# ch : acc) (i# -# 1#)
113 %********************************************************
115 \subsection{Unpacking ByteArrays}
117 %********************************************************
119 Converting byte arrays into list of chars:
122 unpackCStringBA :: ByteArray Int -> [Char]
123 unpackCStringBA (ByteArray l@(I# l#) u@(I# u#) bytes)
125 | otherwise = unpackCStringBA# bytes (u# -# l# +# 1#)
128 unpack until NUL or end of BA is reached, whatever comes first.
130 unpackCStringBA# :: ByteArray# -> Int# -> [Char]
131 unpackCStringBA# bytes len
136 ch `eqChar#` '\0'# = []
137 | otherwise = C# ch : unpack (nh +# 1#)
139 ch = indexCharArray# bytes nh
141 unpackNBytesBA :: ByteArray Int -> Int -> [Char]
142 unpackNBytesBA (ByteArray l u bytes) i
143 = unpackNBytesBA# bytes len#
145 len# = case max 0 (min i len) of I# v# -> v#
149 unpackNBytesBA# :: ByteArray# -> Int# -> [Char]
150 unpackNBytesBA# _bytes 0# = []
151 unpackNBytesBA# bytes len# = unpack [] (len# -# 1#)
156 case indexCharArray# bytes i# of
157 ch -> unpack (C# ch : acc) (i# -# 1#)
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 (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 { (# s2#, barr# #) ->
214 (# s2#, MutableByteArray bot 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 { (# s2#, frozen# #) ->
225 (# s2#, ByteArray 0 (I# len#) frozen# #) }