@@ -2,7 +2,7 @@ module Data.Bytes
22
33import Data.ByteArray as BA
44
5−%access public
5+%access public export
66%default total
77
88-- Structure of the allocated ByteArray
@@ -10,7 +10,7 @@ import Data.ByteArray as BA
1010-- used_size is an int and it takes up BA.bytesPerInt bytes
1111-- at the beginning of the array
1212
13−abstract
13+export
1414record Bytes where
1515 constructor B
1616 arr : ByteArray
@@ -32,15 +32,15 @@ allocate capacity = do
3232 BA.fill dataOfs capacity 0 arr -- zero the array
3333 return $ B arr dataOfs dataOfs
3434
35−abstract
35+export
3636length : Bytes -> Int
3737length (B arr ofs end) = end - ofs
3838
39−abstract
39+export
4040empty : Bytes
4141empty = unsafePerformIO $ allocate minimalCapacity
4242
43−abstract
43+export
4444null : Bytes -> Bool
4545null (B arr ofs end) = (ofs == end)
4646
@@ -62,7 +62,7 @@ grow factor (B arr ofs end) = do
6262 return $ B arr' ofs' (ofs' + bytesUsed)
6363
6464%assert_total
65−abstract
65+export
6666snoc : Bytes -> Byte -> Bytes
6767snoc bs@(B arr ofs end) byte
6868 = if end >= BA.size arr
@@ -81,6 +81,7 @@ snoc bs@(B arr ofs end) byte
8181 return $ B arr ofs (end+1)
8282
8383infixl 7 |>
84+export
8485(|>) : Bytes -> Byte -> Bytes
8586(|>) = snoc
8687
@@ -89,7 +90,7 @@ namespace SnocView
8990 Nil : SnocView
9091 Snoc : (bs : Bytes) -> (b : Byte) -> SnocView
9192
92− abstract
93+ export
9394 snocView : Bytes -> SnocView
9495 snocView (B arr ofs end) =
9596 if end == ofs
@@ -103,7 +104,7 @@ namespace ConsView
103104 Nil : ConsView
104105 Cons : (b : Byte) -> (bs : Bytes) -> ConsView
105106
106− abstract
107+ export
107108 consView : Bytes -> ConsView
108109 consView (B arr ofs end) =
109110 if end == ofs
@@ -114,7 +115,7 @@ namespace ConsView
114115
115116infixr 7 ++
116117%assert_total
117−abstract
118+export
118119(++) : Bytes -> Bytes -> Bytes
119120(++) bsL@(B arrL ofsL endL) bsR@(B arrR ofsR endR)
120121 = let countR = endR - ofsR in
@@ -133,12 +134,15 @@ abstract
133134 BA.copy (arrR, ofsR) (arrL, endL) countR
134135 return $ B arrL ofsL (endL + countR)
135136
137+export
136138dropPrefix : Int -> Bytes -> Bytes
137139dropPrefix n (B arr ofs end) = B arr (((ofs + n) `min` end) `max` dataOfs) end
138140
141+export
139142takePrefix : Int -> Bytes -> Bytes
140143takePrefix n (B arr ofs end) = B arr ofs (((ofs + n) `min` end) `max` dataOfs)
141144
145+export
142146pack : List Byte -> Bytes
143147pack = fromList empty
144148 where
@@ -146,11 +150,13 @@ pack = fromList empty
146150 fromList bs [] = bs
147151 fromList bs (x :: xs) = fromList (bs `snoc` x) xs
148152
153+export
149154unpack : Bytes -> List Byte
150155unpack bs with (consView bs)
151156 | Nil = []
152157 | Cons x xs = x :: unpack (assert_smaller bs xs)
153158
159+export
154160slice : Int -> Int -> Bytes -> Bytes
155161slice ofs' end' (B arr ofs end)
156162 = B arr
@@ -164,6 +170,7 @@ data Result : Type -> Type where
164170 Stop : (result : a) -> Result a
165171 Cont : (acc : a) -> Result a
166172
173+export
167174iterateR : (Byte -> a -> Result a) -> a -> Bytes -> a
168175iterateR f acc bs with (snocView bs)
169176 | Nil = acc
@@ -171,6 +178,7 @@ iterateR f acc bs with (snocView bs)
171178 | Stop result = result
172179 | Cont acc' = iterateR f acc' (assert_smaller bs ys)
173180
181+export
174182iterateL : (a -> Byte -> Result a) -> a -> Bytes -> a
175183iterateL f acc bs with (consView bs)
176184 | Nil = acc
@@ -183,12 +191,15 @@ private
183191(.:) : (a -> b) -> (c -> d -> a) -> (c -> d -> b)
184192(.:) g f x y = g (f x y)
185193
194+export
186195foldr : (Byte -> a -> a) -> a -> Bytes -> a
187196foldr f = iterateR (Cont .: f)
188197
198+export
189199foldl : (a -> Byte -> a) -> a -> Bytes -> a
190200foldl f = iterateL (Cont .: f)
191201
202+export
192203spanLength : (Byte -> Bool) -> Bytes -> Int
193204spanLength p = iterateL step 0
194205 where
@@ -200,25 +211,31 @@ spanLength p = iterateL step 0
200211find : Byte -> Bytes -> Maybe Int
201212find b (B arr ofs end) = unsafePerformIO $ BA.find b arr ofs end
202213
214+export
203215splitAt : Int -> Bytes -> (Bytes, Bytes)
204216splitAt n bs = (takePrefix n bs, dropPrefix n bs)
205217
218+export
206219splitOn : Byte -> Bytes -> (Bytes, Bytes)
207220splitOn b bs with (find b bs)
208221 | Nothing = (bs, empty)
209222 | Just ofs = (takePrefix ofs bs, dropPrefix (ofs+1) bs)
210223
224+export
211225splitsOn : Byte -> Bytes -> List Bytes
212226splitsOn b bs with (find b bs)
213227 | Nothing = [bs]
214228 | Just ofs = takePrefix ofs bs :: splitsOn b (assert_smaller bs $ dropPrefix (ofs+1) bs)
215229
230+export
216231asciiLines : Bytes -> List Bytes
217232asciiLines = splitsOn 0x0A
218233
234+export
219235span : (Byte -> Bool) -> Bytes -> (Bytes, Bytes)
220236span p bs = splitAt (spanLength p bs) bs
221237
238+export
222239break : (Byte -> Bool) -> Bytes -> (Bytes, Bytes)
223240break p bs = span (not . p) bs
224241
@@ -244,9 +261,11 @@ implementation Eq Bytes where
244261implementation Ord Bytes where
245262 compare = Bytes.cmp
246263
264+export
247265toString : Bytes -> String
248266toString = foldr (strCons . chr . toInt) ""
249267
268+export
250269fromString : String -> Bytes
251270fromString = foldl (\bs, c => bs |> fromInt (ord c)) empty . unpack
252271