File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -97,15 +97,15 @@ instance : MonadFinally Get where
9797 match s with
9898 | .success a k =>
9999 let r := f (some a) k
100- let rec go' r :=
100+ let rec @[specialize] go' r :=
101101 match r with
102102 | .success b k' => .success (a, b) k'
103103 | .error err k' => .error err k'
104104 | .pending fn => .pending fun bytes => go' <| fn bytes
105105 go' r
106106 | .error err _ =>
107107 let r := f none d -- backtracking
108- let rec go'' r :=
108+ let rec @[specialize] go'' r :=
109109 match r with
110110 | .success _ k' => .error err k' -- caught, we ignore the inner error
111111 | .error err' k' => .error err' k' -- the finalizer throws an error
@@ -123,6 +123,18 @@ protected def DecodeResult.mkEOI : Decoder → DecodeResult α := .error .eoi
123123@[always_inline]
124124def throwEOI : Get α := DecodeResult.mkEOI
125125
126+ /-- Drop the parsed data from inner buffer. This can be used to save memory. -/
127+ @[always_inline]
128+ def shrink : Get Unit := fun d =>
129+ DecodeResult.success () { data := d.data.extract d.offset d.data.size, offset := 0 }
130+
131+ @[always_inline]
132+ def peek? : Get (Option UInt8) := fun d =>
133+ if h : d.offset < d.data.size then
134+ DecodeResult.success (some d.data[d.offset]) d
135+ else
136+ DecodeResult.success none d
137+
126138class Decode (α : Type ) where
127139 get : Get α
128140export Decode (get)
Original file line number Diff line number Diff line change @@ -7,6 +7,10 @@ namespace Binary
77
88public section
99
10+ @[always_inline]
11+ def fail (msg : String) : Get α :=
12+ throw (.userError msg)
13+
1014@ [always_inline, specialize]
1115def many (p : Get α) : Get (Array α) := do
1216 let mut data := #[]
@@ -21,6 +25,12 @@ def many1 (p : Get α) : Get (Array α) := do
2125 let rest ← many p
2226 return rest.insertIdx 0 first
2327
28+ @ [always_inline, specialize]
29+ def shouldBeEOI : Get Unit := do
30+ let x ← remaining
31+ if x > 0 then
32+ fail "expected EOI"
33+
2434-- TODO: refactor following definitions for performance
2535
2636@ [inline, specialize]
Original file line number Diff line number Diff line change @@ -19,10 +19,6 @@ private def byteToChar (b : UInt8) : Char :=
1919private def chars_to_string (xs : Array Char) : String :=
2020 String.ofList xs.toList
2121
22- @[always_inline]
23- def fail {α} (msg : String) : Get α :=
24- throw (.userError msg)
25-
2622@ [always_inline, specialize]
2723def satisfy (p : Char → Bool) : Get Char := do
2824 let b ← getThe UInt8
You can’t perform that action at this time.
0 commit comments