1 | module Data.ByteString
4 | import Data.Buffer.Builder
5 | import Data.Buffer.Mutable
8 | import Data.Nat.BSExtra
13 | import public Data.ByteVect as BV
22 | null : ByteString -> Bool
23 | null (BS Z _) = True
27 | length : ByteString -> Nat
32 | index : (x : Nat) -> ByteString -> Maybe Bits8
33 | index x (BS n repr) = at repr <$> tryNatToFin x
46 | pack : (as : List Bits8) -> ByteString
47 | pack as = BS _ $
pack as
51 | singleton : Bits8 -> ByteString
52 | singleton v = pack [v]
57 | toList : (Bits8 -> a) -> ByteString -> List a
58 | toList f (BS _ bs) = toList f bs
62 | unpack : ByteString -> List Bits8
67 | toString : ByteString -> String
68 | toString (BS _ bs) = toString bs
71 | Interpolation ByteString where interpolate = toString
77 | Show ByteString where
78 | show (BS _ bs) = show bs
82 | BS _ bs1 == BS _ bs2 = heq bs1 bs2
85 | Ord ByteString where
86 | compare (BS _ bs1) (BS _ bs2) = hcomp bs1 bs2
89 | FromString ByteString where
90 | fromString s = BS _ $
fromString s
93 | Cast String ByteString where
97 | Cast (List Bits8) ByteString where
101 | Cast Bits8 ByteString where
105 | Cast Char ByteString where
106 | cast = cast {from = String} . singleton
109 | {n : _} -> Cast (ByteVect n) ByteString where
113 | {n : _} -> Cast (IBuffer n) ByteString where
114 | cast = cast . fromIBuffer
117 | Cast AnyBuffer ByteString where
118 | cast (AB _ ib) = cast ib
125 | generate : (n : Nat) -> (Fin n -> Bits8) -> ByteString
126 | generate n f = BS n (generate n f)
129 | replicate : (n : Nat) -> Bits8 -> ByteString
130 | replicate n = generate n . const
133 | unsafeByteString : (n : Nat) -> Buffer -> ByteString
134 | unsafeByteString n buf = BS n (BV (unsafeMakeBuffer buf) 0 reflexive)
139 | toBuffer : ByteString -> IO Buffer
140 | toBuffer (BS n (BV b o lt)) =
142 | let _ # t := icopy b o 0 n r t
143 | in pure (unsafeFromMBuffer r) # t
147 | fromIBuffer : {n : _} -> IBuffer n -> ByteString
148 | fromIBuffer b = BS n $
fromIBuffer b
155 | TotLength : List ByteString -> Nat
157 | TotLength (BS n _ :: xs) = n + TotLength xs
161 | (ps : List ByteString)
163 | -> {auto 0 prf : pos + TotLength ps === n}
164 | -> (r : MBuffer s n)
166 | copyMany [] pos r t = () # t
167 | copyMany (BS k (BV b o lt):: xs) pos r t =
168 | let _ # t := icopy b o pos k @{lt} @{rewrite sym prf in concatLemma1} r t
169 | in copyMany xs (pos + k) @{concatLemma2 prf} r t
175 | fastConcat : (bs : List ByteString) -> ByteString
176 | fastConcat [] = empty
179 | alloc (TotLength bs) $
\r,t =>
180 | let _ # t := copyMany bs 0 r t in unsafeFreezeByteString r t
185 | concatSep : (sep : ByteString) -> (bs : List ByteString) -> ByteString
186 | concatSep sep = fastConcat . intersperse sep
191 | concatSep1 : (sep : Bits8) -> (bs : List ByteString) -> ByteString
192 | concatSep1 = concatSep . singleton
197 | unixUnlines : List ByteString -> ByteString
198 | unixUnlines = concatSep1 0x0A
202 | append : ByteString -> ByteString -> ByteString
203 | append (BS 0 _) b2 = b2
204 | append b1 (BS 0 _) = b1
205 | append (BS m bv1) (BS n bv2) = BS _ $
append bv1 bv2
209 | cons : Bits8 -> ByteString -> ByteString
210 | cons = append . singleton
214 | snoc : Bits8 -> ByteString -> ByteString
215 | snoc w bs = bs `append` singleton w
218 | Semigroup ByteString where
222 | Monoid ByteString where
226 | padLeft : Nat -> Bits8 -> ByteString -> ByteString
227 | padLeft n b bs = replicate (n `minus` bs.size) b <+> bs
230 | padRight : Nat -> Bits8 -> ByteString -> ByteString
231 | padRight n b bs = bs <+> replicate (n `minus` bs.size) b
239 | head : ByteString -> Maybe Bits8
244 | tail : ByteString -> Maybe ByteString
245 | tail (BS (S k) bs) = Just (BS k $
tail bs)
250 | uncons : ByteString -> Maybe (Bits8, ByteString)
251 | uncons (BS (S k) bs) = let (b,bs2) := uncons bs in Just (b, BS k bs2)
256 | last : ByteString -> Maybe Bits8
257 | last (BS (S k) bs) = Just $
last bs
262 | init : ByteString -> Maybe ByteString
263 | init (BS (S k) bs) = Just (BS k $
init bs)
268 | unsnoc : ByteString -> Maybe (Bits8, ByteString)
269 | unsnoc (BS (S k) bs) = let (b,bs2) := unsnoc bs in Just (b, BS k bs2)
279 | map : (Bits8 -> Bits8) -> ByteString -> ByteString
280 | map f (BS n bs) = BS n $
map f bs
285 | toUpper : ByteString -> ByteString
286 | toUpper = map toUpper
291 | toLower : ByteString -> ByteString
292 | toLower = map toLower
295 | reverse : ByteString -> ByteString
296 | reverse (BS n bs) = BS n $
reverse bs
300 | all : (Bits8 -> Bool) -> ByteString -> Bool
301 | all p (BS n bs) = all p bs
306 | any : (Bits8 -> Bool) -> ByteString -> Bool
307 | any p (BS n bs) = any p bs
311 | elem : Bits8 -> ByteString -> Bool
312 | elem b = any (b ==)
315 | foldl : (a -> Bits8 -> a) -> a -> ByteString -> a
316 | foldl f ini (BS n bs) = foldl f ini bs
319 | foldr : (Bits8 -> a -> a) -> a -> ByteString -> a
320 | foldr f ini (BS n bs) = foldr f ini bs
326 | findIndex : (Bits8 -> Bool) -> ByteString -> Maybe Nat
327 | findIndex p (BS n bs) = finToNat <$> findIndex p bs
333 | elemIndex : Bits8 -> ByteString -> Maybe Nat
334 | elemIndex c = findIndex (c ==)
337 | find : (Bits8 -> Bool) -> ByteString -> Maybe Bits8
338 | find p (BS n bs) = find p bs
341 | isInfixOf : ByteString -> ByteString -> Bool
342 | isInfixOf (BS _ bv1) (BS _ bv2) = isInfixOf bv1 bv2
345 | isPrefixOf : ByteString -> ByteString -> Bool
346 | isPrefixOf (BS _ bv1) (BS _ bv2) = isPrefixOf bv1 bv2
349 | isSuffixOf : ByteString -> ByteString -> Bool
350 | isSuffixOf (BS _ bv1) (BS _ bv2) = isSuffixOf bv1 bv2
360 | substring : (start,length : Nat) -> ByteString -> ByteString
361 | substring s l (BS n bs) =
362 | case tryLTE (s+l) of
363 | Just0 p => BS l (substring s l bs)
364 | Nothing0 => case tryLTE s of
365 | Just0 p => BS (n `minus` s) (drop s bs)
369 | mapMaybe : (Bits8 -> Maybe Bits8) -> ByteString -> ByteString
370 | mapMaybe f (BS n bs) = mapMaybe f bs
373 | filter : (Bits8 -> Bool) -> ByteString -> ByteString
374 | filter p (BS n bs) = filter p bs
380 | take : Nat -> ByteString -> ByteString
383 | Just0 p => BS k (take k bs)
384 | Nothing0 => BS n bs
389 | takeEnd : Nat -> ByteString -> ByteString
390 | takeEnd k (BS n bs) =
392 | Just0 p => BS k (takeEnd k bs)
393 | Nothing0 => BS n bs
397 | drop : Nat -> ByteString -> ByteString
400 | Just0 p => BS (n `minus` k) (drop k bs)
405 | dropEnd : Nat -> ByteString -> ByteString
406 | dropEnd k (BS n bs) =
408 | Just0 p => BS (n `minus` k) (dropEnd k bs)
412 | splitAt : (k : Nat) -> ByteString -> Maybe (ByteString,ByteString)
413 | splitAt k (BS n bs) =
416 | let (bs1,bs2) := splitAt k bs
417 | in Just (BS k bs1, BS (n `minus` k) bs2)
418 | Nothing0 => Nothing
423 | takeWhile : (Bits8 -> Bool) -> ByteString -> ByteString
424 | takeWhile p (BS n bs) = takeWhile p bs
429 | takeWhileEnd : (Bits8 -> Bool) -> ByteString -> ByteString
430 | takeWhileEnd p (BS n bs) = takeWhileEnd p bs
435 | dropWhile : (Bits8 -> Bool) -> ByteString -> ByteString
436 | dropWhile p (BS n bs) = dropWhile p bs
441 | dropWhileEnd : (Bits8 -> Bool) -> ByteString -> ByteString
442 | dropWhileEnd p (BS n bs) = dropWhileEnd p bs
447 | break : (Bits8 -> Bool) -> ByteString -> (ByteString,ByteString)
448 | break p (BS n bs) =
449 | let MkBreakRes n1 n2 bs1 bs2 _ := break p bs
450 | in (BS n1 bs1, BS n2 bs2)
455 | breakEnd : (Bits8 -> Bool) -> ByteString -> (ByteString,ByteString)
456 | breakEnd p (BS n bs) =
457 | let MkBreakRes n1 n2 bs1 bs2 _ := breakEnd p bs
458 | in (BS n1 bs1, BS n2 bs2)
463 | span : (Bits8 -> Bool) -> ByteString -> (ByteString,ByteString)
464 | span p = break (not . p)
469 | spanEnd : (Bits8 -> Bool) -> ByteString -> (ByteString,ByteString)
470 | spanEnd p (BS n bs) =
471 | let MkBreakRes n1 n2 bs1 bs2 _ := spanEnd p bs
472 | in (BS n1 bs1, BS n2 bs2)
476 | trimLeft : ByteString -> ByteString
477 | trimLeft (BS _ bv) = trimLeft bv
481 | trimRight : ByteString -> ByteString
482 | trimRight (BS _ bv) = trimRight bv
486 | trim : ByteString -> ByteString
487 | trim (BS _ bv) = trim bv
494 | splitWith : (Bits8 -> Bool) -> ByteString -> List ByteString
495 | splitWith p (BS n bs) = splitWith p bs
499 | splitWithNonEmpty : (Bits8 -> Bool) -> ByteString -> List ByteString
500 | splitWithNonEmpty p = filter (not . null) . splitWith p
509 | split : Bits8 -> ByteString -> List ByteString
510 | split b = splitWith (b ==)
514 | splitNonEmpty : Bits8 -> ByteString -> List ByteString
515 | splitNonEmpty p = filter (not . null) . split p
521 | breakAtSubstring : (sep,val : ByteString) -> (ByteString,ByteString)
522 | breakAtSubstring (BS _ s) (BS _ v) =
523 | let MkBreakRes _ _ x y _ := breakAtSubstring s v
524 | in (BS _ x, BS _ y)
529 | breakDropAtSubstring : (pat,target : ByteString) -> (ByteString,ByteString)
530 | breakDropAtSubstring pat = map (drop $
size pat) . breakAtSubstring pat
541 | splitAtSubstring : (sep, val : ByteString) -> List ByteString
542 | splitAtSubstring (BS _ s) (BS _ v) = splitAtSubstring s v
546 | unixLines : ByteString -> List ByteString
547 | unixLines (BS _ bv) = lines bv
565 | (start,end,target : ByteString)
566 | -> Maybe (ByteString,ByteString,ByteString)
567 | breakAround s e t =
568 | if s.size == 0 || e.size == 0 then Nothing else
569 | case breakAtSubstring s t of
570 | (_,BS 0 _) => Nothing
571 | (pre,suf) => case breakAtSubstring e (drop s.size suf) of
572 | (_,BS 0 _) => Nothing
573 | (mid,end) => Just (pre,mid, drop e.size end)
584 | between : (start,end,target : ByteString) -> Maybe ByteString
585 | between s e = map (fst . snd) . breakAround s e
590 | nonEmpty : ByteString -> Maybe ByteString
591 | nonEmpty (BS 0 _) = Nothing
592 | nonEmpty bs = Just bs
600 | manyBetween : (start,end : ByteString) -> ByteString -> List ByteString
601 | manyBetween s e = go [<]
603 | go : SnocList ByteString -> ByteString -> List ByteString
605 | case breakAround s e bs of
606 | Just (x,y,z) => go (sb:<y) (assert_smaller bs z)
607 | Nothing => sb <>> []
612 | betweenNonEmpty : (start,end,target : ByteString) -> Maybe ByteString
613 | betweenNonEmpty s e t = between s e t >>= nonEmpty
623 | (start,end : ByteString)
624 | -> (f : ByteString -> ByteString)
625 | -> (target : ByteString)
627 | modBetween s e f t =
628 | case breakAround s e t of
629 | Just (x,y,z) => fastConcat [x,s,f y,e,z]
640 | (start,end : ByteString)
641 | -> (f : ByteString -> ByteString)
642 | -> (target : ByteString)
644 | modBetweenAll s e f = go [<]
647 | go : SnocList ByteString -> ByteString -> ByteString
649 | case breakAround s e bs of
650 | Just (x,y,z) => go (sb:<x:<s:<f y:<e) (assert_smaller bs z)
651 | Nothing => fastConcat (sb <>> [bs])
661 | -> {auto 0 p1 : LTE 2 base}
662 | -> {auto 0 p2 : LTE base 16}
665 | parseAnyNat base (BS _ bv) = parseAnyNat base bv
669 | parseDecimalNat : ByteString -> Maybe Nat
670 | parseDecimalNat (BS _ bv) = parseDecimalNat bv
673 | parseHexadecimalNat : ByteString -> Maybe Nat
674 | parseHexadecimalNat (BS _ bv) = parseHexadecimalNat bv
677 | parseOctalNat : ByteString -> Maybe Nat
678 | parseOctalNat (BS _ bv) = parseOctalNat bv
681 | parseBinaryNat : ByteString -> Maybe Nat
682 | parseBinaryNat (BS _ bv) = parseBinaryNat bv
685 | parseInteger : ByteString -> Maybe Integer
686 | parseInteger (BS _ bv) = parseInteger bv
689 | parseDouble : ByteString -> Maybe Double
690 | parseDouble (BS _ bv) = parseDouble bv
697 | readByteString : HasIO io => Nat -> File -> io (Either FileError ByteString)
698 | readByteString max f = do
699 | Right (
n ** ib)
<- readIBuffer max f | Left x => pure (Left x)
700 | pure . Right $
BS n (BV ib 0 reflexive)
705 | -> {auto _ : HasIO io}
708 | -> io (Either (FileError,Int) ())
709 | writeByteVect h (BV buf o _) = writeIBuffer h o n buf
713 | {auto _ : HasIO io}
716 | -> io (Either (FileError,Int) ())
717 | writeByteString h (BS n bs) = writeByteVect h bs
724 | putByteString : Builder q => ByteString -> F1' q
725 | putByteString (BS _ bv) = putByteVect bv
728 | getByteString : Builder q => F1 q ByteString
729 | getByteString t = let ab # t := getBytes t in cast ab # t