Skip to content

Commit

Permalink
Merge pull request #182 from inhabitedtype/input-bugfix-take-2
Browse files Browse the repository at this point in the history
input-bugfix-take-2: proper bugfix
  • Loading branch information
seliopou authored Sep 26, 2019
2 parents e32ea03 + 4dd94d5 commit cf6f5d3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/input.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ let count_while t pos ~f =
let buffer = t.buffer in
let off = offset_in_buffer t pos in
let i = ref off in
let len = t.len in
while !i < off + len && f (Bigstringaf.unsafe_get buffer !i) do
let limit = t.off + t.len in
while !i < limit && f (Bigstringaf.unsafe_get buffer !i) do
incr i
done;
!i - off
Expand Down
17 changes: 12 additions & 5 deletions lib_test/test_angstrom.ml
Original file line number Diff line number Diff line change
Expand Up @@ -371,17 +371,24 @@ let choice_commit =
end ]

let input =
[ "non-zero offset", `Quick, begin fun () ->
let parser = take_while (fun _ -> true) in
match Angstrom.Unbuffered.parse parser with
let test p input ~off ~len expect =
match Angstrom.Unbuffered.parse p with
| Done _ | Fail _ -> assert false
| Partial { continue; committed } ->
Alcotest.(check int) "committed is zero" 0 committed;
let state = continue (Bigstringaf.of_string "abcd" ~off:0 ~len:4) ~off:1 ~len:2 Complete in
let bs = Bigstringaf.of_string input ~off:0 ~len:(String.length input) in
let state = continue bs ~off ~len Complete in
Alcotest.(check (result string string))
"offset and length respected"
(Ok "bc")
(Ok expect)
(Angstrom.Unbuffered.state_to_result state);
in

[ "offset and length respected", `Quick, begin fun () ->
let open Angstrom in
let take_all = take_while (fun _ -> true) in
test take_all "abcd" ~off:1 ~len:2 "bc";
test (take 4 *> take_all) "abcdefg" ~off:0 ~len:7 "efg";
end ]
;;

Expand Down

0 comments on commit cf6f5d3

Please sign in to comment.