0 | module FS.Internal.Bytes
3 | import Data.ByteString
5 | import Derive.Prelude
8 | %language ElabReflection
12 | SnocBytes = SnocList ByteString
16 | Bytes = List ByteString
19 | concatSnoc : SnocBytes -> ByteString
20 | concatSnoc [<] = empty
22 | concatSnoc sx = fastConcat (sx <>> [])
28 | ls : SnocBytes -> (n : Nat) -> ByteVect n -> (Maybe Bytes, ByteString)
29 | ls sb n bs = case breakNL bs of
30 | MkBreakRes l1 0 b1 _ prf => (Just $
sb <>> [], BS l1 b1)
31 | MkBreakRes l1 (S l2) b1 b2 prf =>
32 | ls (sb :< BS l1 b1) (assert_smaller n l2) (tail b2)
35 | splitNL : ByteString -> ByteString -> (Maybe Bytes, ByteString)
36 | splitNL x (BS n bs) =
38 | MkBreakRes l1 0 b1 _ prf => (Nothing, x <+> BS l1 b1)
39 | MkBreakRes l1 (S l2) b1 b2 prf => ls [<x <+> BS l1 b1] l2 (tail b2)
42 | breakLastNL : ByteString -> ByteString -> (Maybe ByteString, ByteString)
43 | breakLastNL x (BS n bs) =
44 | case breakEnd (0x0a ==) bs of
45 | MkBreakRes 0 l2 b1 b2 prf => (Nothing, x <+> BS l2 b2)
46 | MkBreakRes (S l1) l2 b1 b2 prf => (Just (x <+> BS _ (init b1)), BS l2 b2)
55 | continuationBytes : Bits8 -> Maybe Nat
56 | continuationBytes b =
59 | if (b .&. 0b1000_0000) == 0b0000_0000 then Just 0
60 | else if (b .&. 0b1110_0000) == 0b1100_0000 then Just 1
61 | else if (b .&. 0b1111_0000) == 0b1110_0000 then Just 2
62 | else if (b .&. 0b1111_1000) == 0b1111_0000 then Just 3
70 | -> {auto 0 p : LTE k n}
71 | -> (Maybe ByteString,ByteString)
72 | splitLeading 0 x = (Nothing, BS _ x)
73 | splitLeading (S k) x =
74 | case continuationBytes (atNat x k) of
75 | Nothing => splitLeading k x
78 | then (nonEmpty $
BS _ x, empty)
79 | else (nonEmpty $
BS _ $
take k x, BS _ $
drop k x)
86 | breakAtLastIncomplete : ByteString -> ByteString -> (Maybe ByteString, ByteString)
87 | breakAtLastIncomplete pre cur =
88 | let BS sz bv := pre <+> cur
89 | in splitLeading sz bv
97 | data BSSRes : Type where
99 | TooShort : ByteString -> BSSRes
103 | NoSS : (pre,pst : ByteString) -> BSSRes
106 | SS : (pre,pst : ByteString) -> BSSRes
108 | %runElab derive "BSSRes" [Show,Eq]
111 | breakAtSS : (ss, cur : ByteString) -> BSSRes
113 | case size cur < size ss of
114 | True => TooShort cur
115 | False => case breakAtSubstring ss cur of
117 | let ln := S (size pre) `minus` size ss
118 | in NoSS (take ln pre) (drop ln pre)
119 | (pre, pst) => SS pre (drop ss.size pst)