3 | import public Data.ByteString
5 | import Data.Buffer.Mutable
6 | import FS.Internal.Bytes
12 | nebs : ByteString -> Maybe ByteString
13 | nebs (BS 0 _) = Nothing
17 | emitBS : ByteString -> Pull f ByteString es ()
18 | emitBS (BS 0 _) = pure ()
22 | consBS : ByteString -> Pull f ByteString es r -> Pull f ByteString es r
23 | consBS (BS 0 _) p = p
24 | consBS bs p = cons bs p
32 | unlines : Pull f (List ByteString) es r -> Pull f ByteString es r
33 | unlines = mapOutput (\cs => fastConcat $
cs >>= \b => [b,nl])
41 | lines : Pull f ByteString es r -> Pull f (List ByteString) es r
42 | lines = scanFull empty splitNL (map pure . nonEmpty)
47 | breakNL : Pull f ByteString es r -> Pull f ByteString es r
48 | breakNL = scanFull empty breakLastNL nonEmpty
57 | (orElse : r -> Pull f ByteString es r)
59 | -> Pull f ByteString es r
60 | -> Pull f ByteString es (Pull f ByteString es r)
61 | breakAtSubstring orElse ss = go empty
65 | -> Pull f ByteString es r
66 | -> Pull f ByteString es (Pull f ByteString es r)
68 | assert_total $
uncons p >>= \case
69 | Left v => consBS pre (pure $
orElse v)
70 | Right (cur,q) => case breakAtSS ss (pre <+> cur) of
71 | TooShort x => go x q
72 | NoSS x y => consBS x (go y q)
73 | SS x y => consBS x (pure $
emitBS y >> q)
78 | forceBreakAtSubstring :
79 | {auto has : Has e es}
82 | -> Pull f ByteString es r
83 | -> Pull f ByteString es (Pull f ByteString es r)
84 | forceBreakAtSubstring err = breakAtSubstring (const $
throw err)
93 | chunks : Stream f es ByteString -> Stream f es ByteString
94 | chunks = scanFull empty UTF8.breakAtLastIncomplete nonEmpty
102 | decode : Stream f es ByteString -> Stream f es String
103 | decode = mapOutput toString . UTF8.chunks
110 | encode : Stream f es String -> Stream f es ByteString
111 | encode = mapOutput fromString
117 | breakImpl : BreakInstruction -> (Bits8 -> Bool) -> ByteString -> BreakRes ByteString
120 | (pre, BS 0 _) => Keep pre
121 | (pre, pst@(BS (S k) bv)) => case x of
122 | PostHit => Broken (nebs pre) (Just pst)
123 | DropHit => Broken (nebs pre) (nebs $
BS k (tail bv))
125 | let len := S pre.size
126 | in Broken (Just $
take len bs) (nebs $
drop len bs)
128 | splitImpl : Nat -> ByteString -> SplitRes ByteString
130 | case splitAt k bs of
131 | Nothing => All (k `minus` size bs)
133 | let Just y := nebs post | Nothing => All (k `minus` size pre)
138 | -> (st -> Either r (Bits8,st))
142 | -> {auto ix : Ix k n}
143 | -> F1 s (UnfoldRes r st ByteString)
144 | unfoldImpl f cur buf 0 t =
145 | let ibuf # t := unsafeFreeze buf t
146 | in More (fromIBuffer ibuf) cur # t
147 | unfoldImpl f cur buf (S k) t =
150 | let _ # t := setIx buf k v t
151 | in unfoldImpl f nxt buf k t
153 | let ibuf # t := unsafeFreezeLTE buf (ixToNat ix) @{ixLTE ix} t
154 | in case nebs (fromIBuffer ibuf) of
155 | Nothing => Done x # t
156 | Just bs => Last x bs # t
159 | Chunk ByteString Bits8 where
160 | filterChunk pred = nebs . filter pred
162 | breakChunk = breakImpl
164 | splitChunkAt = splitImpl
166 | unconsChunk (BS (S k) bv) = Just (at bv 0, nebs (BS k $
tail bv))
167 | unconsChunk _ = Nothing
169 | isEmpty (BS 0 _) = True
172 | replicateChunk @{CS n} v = replicate n v
174 | unfoldChunk @{CS n} f ini = alloc n (\buf => unfoldImpl f ini buf n)