2 % (c) The GRASP/AQUA Project, Glasgow University, 1997-1998
4 \section{Basic ops on packed representations}
6 Some basic operations for working on packed representations of series
7 of bytes (character strings). Used by the interface lexer input
11 {-# OPTIONS -monly-3-regs -optc-DNON_POSIX_SOURCE #-}
14 strLength, -- :: _Addr -> Int
15 copyPrefixStr, -- :: _Addr -> Int -> ByteArray Int
16 copySubStr, -- :: _Addr -> Int -> Int -> ByteArray Int
17 copySubStrBA, -- :: ByteArray Int -> Int -> Int -> ByteArray Int
19 eqStrPrefix, -- :: Addr# -> ByteArray# -> Int# -> Bool
20 eqCharStrPrefix, -- :: Addr# -> Addr# -> Int# -> Bool
21 eqStrPrefixBA, -- :: ByteArray# -> ByteArray# -> Int# -> Int# -> Bool
22 eqCharStrPrefixBA, -- :: Addr# -> ByteArray# -> Int# -> Int# -> Bool
24 addrOffset# -- :: Addr# -> Int# -> Addr#
27 -- This #define suppresses the "import FastString" that
28 -- HsVersions otherwise produces
29 #define COMPILING_FAST_STRING
30 #include "HsVersions.h"
33 #if __GLASGOW_HASKELL__ < 411
34 import PrelAddr ( Addr(..) )
36 import Addr ( Addr(..) )
41 #if __GLASGOW_HASKELL__ < 503
49 Return the length of a @\\NUL@ terminated character string:
52 strLength :: Addr -> Int
55 _ccall_ strlen a >>= \ len@(I# _) ->
58 {-# NOINLINE strLength #-}
61 Copying a char string prefix into a byte array,
62 {\em assuming} the prefix does not contain any
66 copyPrefixStr :: Addr -> Int -> ByteArray Int
67 copyPrefixStr (A# a) len@(I# length#) =
69 {- allocate an array that will hold the string
70 (not forgetting the NUL at the end)
72 (new_ps_array (length# +# 1#)) >>= \ ch_array ->
73 {- Revert back to Haskell-only solution for the moment.
74 _ccall_ memcpy ch_array (A# a) len >>= \ () ->
75 write_ps_array ch_array length# (chr# 0#) >>
77 -- fill in packed string from "addr"
78 fill_in ch_array 0# >>
80 freeze_ps_array ch_array length# >>= \ barr ->
83 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
87 = write_ps_array arr_in# idx (chr# 0#) >>
90 = case (indexCharOffAddr# a idx) of { ch ->
91 write_ps_array arr_in# idx ch >>
92 fill_in arr_in# (idx +# 1#) }
96 Copying out a substring, assume a 0-indexed string:
97 (and positive lengths, thank you).
100 copySubStr :: Addr -> Int -> Int -> ByteArray Int
101 copySubStr a start length =
103 _casm_ `` %r= (char *)((char *)%0 + (int)%1); '' a start
105 return (copyPrefixStr a_start length))
107 -- step on (char *) pointer by x units.
108 addrOffset# :: Addr# -> Int# -> Addr#
110 case unsafePerformIO (_casm_ ``%r=(char *)((char *)%0 + (int)%1); '' (A# a#) (I# i#)) of
113 copySubStrBA :: ByteArray Int -> Int -> Int -> ByteArray Int
114 copySubStrBA (ByteArray _ _ barr#) (I# start#) len@(I# length#) =
116 {- allocate an array that will hold the string
117 (not forgetting the NUL at the end)
119 new_ps_array (length# +# 1#) >>= \ ch_array ->
120 -- fill in packed string from "addr"
121 fill_in ch_array 0# >>
123 freeze_ps_array ch_array length#)
125 fill_in :: MutableByteArray s Int -> Int# -> ST s ()
129 = write_ps_array arr_in# idx (chr# 0#) >>
132 = case (indexCharArray# barr# (start# +# idx)) of { ch ->
133 write_ps_array arr_in# idx ch >>
134 fill_in arr_in# (idx +# 1#) }
137 (Very :-) ``Specialised'' versions of some CharArray things...
138 [Copied from PackBase; no real reason -- UGH]
141 new_ps_array :: Int# -> ST s (MutableByteArray s Int)
142 write_ps_array :: MutableByteArray s Int -> Int# -> Char# -> ST s ()
143 freeze_ps_array :: MutableByteArray s Int -> Int# -> ST s (ByteArray Int)
145 new_ps_array size = ST $ \ s ->
146 #if __GLASGOW_HASKELL__ < 411
147 case (newCharArray# size s) of { (# s2#, barr# #) ->
148 (# s2#, MutableByteArray bot bot barr# #) }
149 #else /* 411 and higher */
150 case (newByteArray# size s) of { (# s2#, barr# #) ->
151 (# s2#, MutableByteArray bot bot barr# #) }
154 bot = error "new_ps_array"
156 write_ps_array (MutableByteArray _ _ barr#) n ch = ST $ \ s# ->
157 case writeCharArray# barr# n ch s# of { s2# ->
160 -- same as unsafeFreezeByteArray
161 freeze_ps_array (MutableByteArray _ _ arr#) len# = ST $ \ s# ->
162 case unsafeFreezeByteArray# arr# s# of { (# s2#, frozen# #) ->
163 (# s2#, ByteArray 0 (I# len#) frozen# #) }
167 Compare two equal-length strings for equality:
170 eqStrPrefix :: Addr# -> ByteArray# -> Int# -> Bool
171 eqStrPrefix a# barr# len# =
173 _ccall_ memcmp (A# a#) (ByteArray bot bot barr#) (I# len#) >>= \ (I# x#) ->
177 bot = error "eqStrPrefix"
179 eqCharStrPrefix :: Addr# -> Addr# -> Int# -> Bool
180 eqCharStrPrefix a1# a2# len# =
182 _ccall_ memcmp (A# a1#) (A# a2#) (I# len#) >>= \ (I# x#) ->
185 eqStrPrefixBA :: ByteArray# -> ByteArray# -> Int# -> Int# -> Bool
186 eqStrPrefixBA b1# b2# start# len# =
188 _casm_ ``%r=(int)memcmp((char *)%0+(int)%1,%2,%3); ''
189 (ByteArray bot bot b2#)
191 (ByteArray bot bot b1#)
192 (I# len#) >>= \ (I# x#) ->
196 bot = error "eqStrPrefixBA"
198 eqCharStrPrefixBA :: Addr# -> ByteArray# -> Int# -> Int# -> Bool
199 eqCharStrPrefixBA a# b2# start# len# =
201 _casm_ ``%r=(int)memcmp((char *)%0+(int)%1,%2,%3); ''
202 (ByteArray bot bot b2#)
205 (I# len#) >>= \ (I# x#) ->
209 bot = error "eqCharStrPrefixBA"