Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor Hashtbl.find out of xenopsd/xc/readln.ml
Signed-off-by: Andrii Sultanov <[email protected]>
  • Loading branch information
Andrii Sultanov committed Jul 4, 2024
commit 47f3c3d68f839176007fb3168d169805d74d48c6
31 changes: 16 additions & 15 deletions ocaml/xenopsd/xc/readln.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,26 @@ let read fd =
(Bytes.to_string pending)
)
| n ->
let data = Bytes.sub buffer 0 n in
let inpt = try Hashtbl.find input fd with Not_found -> Bytes.empty in
Hashtbl.replace input fd (Bytes.cat inpt data) ;
let rec loop msgs =
let data = Hashtbl.find input fd in
(* never fails *)
match Bytes.index data '\n' with
| exception Not_found ->
Ok (List.rev msgs)
| index ->
let rec loop msgs data =
match Bytes.index_opt data '\n' with
| None ->
(List.rev msgs, data)
| Some index ->
let remain =
Bytes.sub data (index + 1) (Bytes.length data - index - 1)
in
Hashtbl.replace input fd remain ;
(* reset input *)
loop (Bytes.sub_string data 0 index :: msgs)
(* store msg *)
loop
(Bytes.sub_string data 0 index :: msgs)
remain (* reset input *)
in
let data = Bytes.sub buffer 0 n in
let inpt =
Option.value (Hashtbl.find_opt input fd) ~default:Bytes.empty
in
loop []
let inp_data = Bytes.cat inpt data in
let res, data = loop [] inp_data in
Hashtbl.replace input fd data ;
Ok res
| exception Unix.Unix_error (error, _, _) ->
Error (Unix.error_message error)

Expand Down