123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2019,2020 DaiLambda, Inc. <contact@dailambda.jp> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openResult_lwt.InfixtypeError.t+=|GC_invalid_basesofstring|GC_copy_hash_mismatchlet()=Error.register_printer(function|GC_invalid_basess->Some(Printf.sprintf"GC: invalid bases: %s"s)|GC_copy_hash_mismatch->Some"GC: copy hash does not agree with the source"|_->None)letcopy~base_src~src~base_dst=letcheck_src_ctxts()=ifnot(Cursor.contextbase_src==Cursor.contextsrc)thenError(GC_invalid_bases"Source base and src use different contexts")elseOk(Cursor.contextsrc)inletcheck_base_nhs()=letbase_src,base_src_nh=Cursor.compute_hashbase_srcinletbase_dst,base_dst_nh=Cursor.compute_hashbase_dstinifbase_src_nh<>base_dst_nhthenError(GC_invalid_bases"Hashes of bases are not equal")elseOk(base_src,base_dst)incheck_src_ctxts()>>?funsrc_ctxt->check_base_nhs()>>?fun(base_src,base_dst)->letsrc,src_nh=Cursor.compute_hashsrcinletCursor(_,base_src_n,_,_)=base_srcinletCursor(_,src_n,_,_)=srcinletdiffs=Diff.diffsrc_ctxtbase_src_nsrc_ninletdst=base_dstin(* XXX The sharings of Diff.t are lost by Diff.apply *)Result.fold_leftM(fundstd->letd=matchdwith|Diff.Add(n,segs)->Diff.Add(Node_storage.read_node_fully~reset_index:truesrc_ctxtn,segs)|d->dinDiff.applydstd)dstdiffs>>?fundst->letdst,dst_nh=Cursor.compute_hashdstinifsrc_nh=dst_nhthenOkdstelseErrorGC_copy_hash_mismatch