@@ -2,7 +2,7 @@ module Data.Bytes
3import Data.ByteArray as BA
3import Data.ByteArray as BA
5−%access public
5+%access public export
6%default total
6%default total
8-- Structure of the allocated ByteArray
8-- Structure of the allocated ByteArray
@@ -10,7 +10,7 @@ import Data.ByteArray as BA
10-- used_size is an int and it takes up BA.bytesPerInt bytes
10-- used_size is an int and it takes up BA.bytesPerInt bytes
11-- at the beginning of the array
11-- at the beginning of the array
14record Bytes where
14record Bytes where
15 constructor B
15 constructor B
16 arr : ByteArray
16 arr : ByteArray
@@ -32,15 +32,15 @@ allocate capacity = do
32 BA.fill dataOfs capacity 0 arr -- zero the array
32 BA.fill dataOfs capacity 0 arr -- zero the array
33 return $ B arr dataOfs dataOfs
33 return $ B arr dataOfs dataOfs
36length : Bytes -> Int
36length : Bytes -> Int
37length (B arr ofs end) = end - ofs
37length (B arr ofs end) = end - ofs
40empty : Bytes
40empty : Bytes
41empty = unsafePerformIO $ allocate minimalCapacity
41empty = unsafePerformIO $ allocate minimalCapacity
44null : Bytes -> Bool
44null : Bytes -> Bool
45null (B arr ofs end) = (ofs == end)
45null (B arr ofs end) = (ofs == end)
@@ -62,7 +62,7 @@ grow factor (B arr ofs end) = do
62 return $ B arr' ofs' (ofs' + bytesUsed)
62 return $ B arr' ofs' (ofs' + bytesUsed)
64%assert_total
64%assert_total
66snoc : Bytes -> Byte -> Bytes
66snoc : Bytes -> Byte -> Bytes
67snoc bs@(B arr ofs end) byte
67snoc bs@(B arr ofs end) byte
68 = if end >= BA.size arr
68 = if end >= BA.size arr
@@ -81,6 +81,7 @@ snoc bs@(B arr ofs end) byte
81 return $ B arr ofs (end+1)
81 return $ B arr ofs (end+1)
83infixl 7 |>
83infixl 7 |>
84(|>) : Bytes -> Byte -> Bytes
85(|>) : Bytes -> Byte -> Bytes
85(|>) = snoc
86(|>) = snoc
@@ -89,7 +90,7 @@ namespace SnocView
89 Nil : SnocView
90 Nil : SnocView
90 Snoc : (bs : Bytes) -> (b : Byte) -> SnocView
91 Snoc : (bs : Bytes) -> (b : Byte) -> SnocView
93 snocView : Bytes -> SnocView
94 snocView : Bytes -> SnocView
94 snocView (B arr ofs end) =
95 snocView (B arr ofs end) =
95 if end == ofs
96 if end == ofs
@@ -103,7 +104,7 @@ namespace ConsView
103 Nil : ConsView
104 Nil : ConsView
104 Cons : (b : Byte) -> (bs : Bytes) -> ConsView
105 Cons : (b : Byte) -> (bs : Bytes) -> ConsView
107 consView : Bytes -> ConsView
108 consView : Bytes -> ConsView
108 consView (B arr ofs end) =
109 consView (B arr ofs end) =
109 if end == ofs
110 if end == ofs
@@ -114,7 +115,7 @@ namespace ConsView
115infixr 7 ++
116infixr 7 ++
116%assert_total
117%assert_total
118(++) : Bytes -> Bytes -> Bytes
119(++) : Bytes -> Bytes -> Bytes
119(++) bsL@(B arrL ofsL endL) bsR@(B arrR ofsR endR)
120(++) bsL@(B arrL ofsL endL) bsR@(B arrR ofsR endR)
120 = let countR = endR - ofsR in
121 = let countR = endR - ofsR in
@@ -133,12 +134,15 @@ abstract
133 BA.copy (arrR, ofsR) (arrL, endL) countR
134 BA.copy (arrR, ofsR) (arrL, endL) countR
134 return $ B arrL ofsL (endL + countR)
135 return $ B arrL ofsL (endL + countR)
136dropPrefix : Int -> Bytes -> Bytes
138dropPrefix : Int -> Bytes -> Bytes
137dropPrefix n (B arr ofs end) = B arr (((ofs + n) `min` end) `max` dataOfs) end
139dropPrefix n (B arr ofs end) = B arr (((ofs + n) `min` end) `max` dataOfs) end
139takePrefix : Int -> Bytes -> Bytes
142takePrefix : Int -> Bytes -> Bytes
140takePrefix n (B arr ofs end) = B arr ofs (((ofs + n) `min` end) `max` dataOfs)
143takePrefix n (B arr ofs end) = B arr ofs (((ofs + n) `min` end) `max` dataOfs)
142pack : List Byte -> Bytes
146pack : List Byte -> Bytes
143pack = fromList empty
147pack = fromList empty
@@ -146,11 +150,13 @@ pack = fromList empty
146 fromList bs [] = bs
150 fromList bs [] = bs
147 fromList bs (x :: xs) = fromList (bs `snoc` x) xs
151 fromList bs (x :: xs) = fromList (bs `snoc` x) xs
149unpack : Bytes -> List Byte
154unpack : Bytes -> List Byte
150unpack bs with (consView bs)
155unpack bs with (consView bs)
151 | Nil = []
156 | Nil = []
152 | Cons x xs = x :: unpack (assert_smaller bs xs)
157 | Cons x xs = x :: unpack (assert_smaller bs xs)
154slice : Int -> Int -> Bytes -> Bytes
160slice : Int -> Int -> Bytes -> Bytes
155slice ofs' end' (B arr ofs end)
161slice ofs' end' (B arr ofs end)
@@ -164,6 +170,7 @@ data Result : Type -> Type where
164 Stop : (result : a) -> Result a
170 Stop : (result : a) -> Result a
165 Cont : (acc : a) -> Result a
171 Cont : (acc : a) -> Result a
167iterateR : (Byte -> a -> Result a) -> a -> Bytes -> a
174iterateR : (Byte -> a -> Result a) -> a -> Bytes -> a
168iterateR f acc bs with (snocView bs)
175iterateR f acc bs with (snocView bs)
169 | Nil = acc
176 | Nil = acc
@@ -171,6 +178,7 @@ iterateR f acc bs with (snocView bs)
171 | Stop result = result
178 | Stop result = result
172 | Cont acc' = iterateR f acc' (assert_smaller bs ys)
179 | Cont acc' = iterateR f acc' (assert_smaller bs ys)
174iterateL : (a -> Byte -> Result a) -> a -> Bytes -> a
182iterateL : (a -> Byte -> Result a) -> a -> Bytes -> a
175iterateL f acc bs with (consView bs)
183iterateL f acc bs with (consView bs)
176 | Nil = acc
184 | Nil = acc
@@ -183,12 +191,15 @@ private
183(.:) : (a -> b) -> (c -> d -> a) -> (c -> d -> b)
191(.:) : (a -> b) -> (c -> d -> a) -> (c -> d -> b)
184(.:) g f x y = g (f x y)
192(.:) g f x y = g (f x y)
186foldr : (Byte -> a -> a) -> a -> Bytes -> a
195foldr : (Byte -> a -> a) -> a -> Bytes -> a
187foldr f = iterateR (Cont .: f)
196foldr f = iterateR (Cont .: f)
189foldl : (a -> Byte -> a) -> a -> Bytes -> a
199foldl : (a -> Byte -> a) -> a -> Bytes -> a
190foldl f = iterateL (Cont .: f)
200foldl f = iterateL (Cont .: f)
192spanLength : (Byte -> Bool) -> Bytes -> Int
203spanLength : (Byte -> Bool) -> Bytes -> Int
193spanLength p = iterateL step 0
204spanLength p = iterateL step 0
@@ -200,25 +211,31 @@ spanLength p = iterateL step 0
200find : Byte -> Bytes -> Maybe Int
211find : Byte -> Bytes -> Maybe Int
201find b (B arr ofs end) = unsafePerformIO $ BA.find b arr ofs end
212find b (B arr ofs end) = unsafePerformIO $ BA.find b arr ofs end
203splitAt : Int -> Bytes -> (Bytes, Bytes)
215splitAt : Int -> Bytes -> (Bytes, Bytes)
204splitAt n bs = (takePrefix n bs, dropPrefix n bs)
216splitAt n bs = (takePrefix n bs, dropPrefix n bs)
206splitOn : Byte -> Bytes -> (Bytes, Bytes)
219splitOn : Byte -> Bytes -> (Bytes, Bytes)
207splitOn b bs with (find b bs)
220splitOn b bs with (find b bs)
208 | Nothing = (bs, empty)
221 | Nothing = (bs, empty)
209 | Just ofs = (takePrefix ofs bs, dropPrefix (ofs+1) bs)
222 | Just ofs = (takePrefix ofs bs, dropPrefix (ofs+1) bs)
211splitsOn : Byte -> Bytes -> List Bytes
225splitsOn : Byte -> Bytes -> List Bytes
212splitsOn b bs with (find b bs)
226splitsOn b bs with (find b bs)
213 | Nothing = [bs]
227 | Nothing = [bs]
214 | Just ofs = takePrefix ofs bs :: splitsOn b (assert_smaller bs $ dropPrefix (ofs+1) bs)
228 | Just ofs = takePrefix ofs bs :: splitsOn b (assert_smaller bs $ dropPrefix (ofs+1) bs)
216asciiLines : Bytes -> List Bytes
231asciiLines : Bytes -> List Bytes
217asciiLines = splitsOn 0x0A
232asciiLines = splitsOn 0x0A
219span : (Byte -> Bool) -> Bytes -> (Bytes, Bytes)
235span : (Byte -> Bool) -> Bytes -> (Bytes, Bytes)
220span p bs = splitAt (spanLength p bs) bs
236span p bs = splitAt (spanLength p bs) bs
222break : (Byte -> Bool) -> Bytes -> (Bytes, Bytes)
239break : (Byte -> Bool) -> Bytes -> (Bytes, Bytes)
223break p bs = span (not . p) bs
240break p bs = span (not . p) bs
@@ -244,9 +261,11 @@ implementation Eq Bytes where
244implementation Ord Bytes where
261implementation Ord Bytes where
245 compare = Bytes.cmp
262 compare = Bytes.cmp
247toString : Bytes -> String
265toString : Bytes -> String
248toString = foldr (strCons . chr . toInt) ""
266toString = foldr (strCons . chr . toInt) ""
250fromString : String -> Bytes
269fromString : String -> Bytes
251fromString = foldl (\bs, c => bs |> fromInt (ord c)) empty . unpack
270fromString = foldl (\bs, c => bs |> fromInt (ord c)) empty . unpack