From 764514a330cb561824aab8ccc3cac84bbd6f1e0d Mon Sep 17 00:00:00 2001 From: Spiros Eliopoulos Date: Tue, 24 Sep 2019 20:43:25 -0400 Subject: [PATCH 1/2] input-bugfix: add test demonstrating bug --- lib_test/test_angstrom.ml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib_test/test_angstrom.ml b/lib_test/test_angstrom.ml index 8c5668d..d18fd83 100644 --- a/lib_test/test_angstrom.ml +++ b/lib_test/test_angstrom.ml @@ -370,6 +370,22 @@ let choice_commit = (parse_string p "@@^"); end ] +let input = + [ "non-zero offset", `Quick, begin fun () -> + let parser = take_while (fun _ -> true) in + match Angstrom.Unbuffered.parse parser 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 + Alcotest.(check (result string string)) + "offset and length respected" + (Ok "bc") + (Angstrom.Unbuffered.state_to_result state); + end ] +;; + + let () = Alcotest.run "test suite" @@ -383,4 +399,5 @@ let () = ; "incremental input" , incremental ; "count_while regression", count_while_regression ; "choice and commit" , choice_commit + ; "input" , input ] From 63dd1f06623308c5987fea1b34f4935dcd3d6f28 Mon Sep 17 00:00:00 2001 From: Spiros Eliopoulos Date: Tue, 24 Sep 2019 20:47:32 -0400 Subject: [PATCH 2/2] input-bugfix: account for offset in termination condition --- lib/input.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/input.ml b/lib/input.ml index 3237772..1e3d0fe 100644 --- a/lib/input.ml +++ b/lib/input.ml @@ -100,7 +100,7 @@ let count_while t pos ~f = let off = offset_in_buffer t pos in let i = ref off in let len = t.len in - while !i < len && f (Bigstringaf.unsafe_get buffer !i) do + while !i < off + len && f (Bigstringaf.unsafe_get buffer !i) do incr i done; !i - off