From 4dd94d56193939f472428db61f6000a35d0d3c93 Mon Sep 17 00:00:00 2001 From: Spiros Eliopoulos Date: Wed, 25 Sep 2019 20:11:39 -0400 Subject: [PATCH] input-bugfix-take-2: proper bugfix This fixes the bug introduced by #181, which was a bugfix for a bug reported in #180. --- lib/input.ml | 4 ++-- lib_test/test_angstrom.ml | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/input.ml b/lib/input.ml index 1e3d0fe..0355d36 100644 --- a/lib/input.ml +++ b/lib/input.ml @@ -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 diff --git a/lib_test/test_angstrom.ml b/lib_test/test_angstrom.ml index d18fd83..19d112b 100644 --- a/lib_test/test_angstrom.ml +++ b/lib_test/test_angstrom.ml @@ -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 ] ;;