(* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. *) open Common type t = Common.file let file_gensym = ref 0 let show file = Format.sprintf "{name: %s, contents: \"%s\"}" file.name (file.contents |> Dynarray.to_list |> List.map Value.show |> String.concat ", ") let pp ppf file = Format.fprintf ppf "%s" (show file); () let create ?(contents = []) name = { name; contents = Dynarray.of_list contents } let equal (f1 : t) (f2 : t) : bool = f1.name = f2.name && Dynarray.equal Value.equal f1.contents f2.contents let contents_equal (f1 : t) (f2 : t) : bool = Dynarray.equal Value.equal f1.contents f2.contents let fill file strs = Dynarray.append_list file.contents strs let clear file = Dynarray.clear file.contents let length file = Dynarray.length file.contents let void file idx = let len = length file in if idx < 0 then () else if idx >= len then () else let contents = file.contents in let new_contents = Dynarray.make (len - 1) (Value.int 0) in let left_len = idx in let right_len = len - left_len - 1 in if left_len > 0 then Dynarray.blit ~src:contents ~src_pos:0 ~dst:new_contents ~dst_pos:0 ~len:left_len; if right_len > 0 then Dynarray.blit ~src:contents ~src_pos:(idx + 1) ~dst:new_contents ~dst_pos:left_len ~len:right_len; file.contents <- new_contents; () let next_name () = let id = string_of_int !file_gensym in file_gensym := !file_gensym + 1; id