123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 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. *)(* *)(*****************************************************************************)(* Info stored in Cursor.t *)openUtilstypet={copies:Path.tlist(* invariant: Segment.list is never empty *)}letempty={copies=[]}letequalt1t2=ifList.lengtht1.copies<>List.lengtht2.copiesthenfalseelse(* not tail recursive... *)List.for_all2(funsegs1segs2->Path.equalsegs1segs2)t1.copiest2.copiesletencodet=ift=emptythen""else(* for future extensibility, info is saved as (tag*content) list
ended with 0
*)letcopies=ift.copies=[]then[]else"\001"::List.mapSegment.Serialization.encode_list(List.mapPath.to_segmentst.copies@[[]])inString.concat""@@copies@["\000"]letdecode_copies(s,off)=letrecgrev_copiesoff=matchSegment.Serialization.decode_list_slice(s,off)with|None->None(* error *)|Some([],off)->(* end *)Some(List.revrev_copies,off)|Some(copy,off)->g(Path.of_segmentscopy::rev_copies)offing[]offletdecodes=ifs=""thenSomeemptyelseletslen=String.lengthsinletrecfcopiesoff=matchString.unsafe_getsoffwith|'\000'whenoff+1=slen->(* EOS *)Some{copies=beginmatchcopieswith|None->[]|Somecopies->copiesend}|'\000'->None(* garbage at the end *)|'\001'whencopies<>None->None(* copies cannot appear more than once *)|'\001'->beginmatchdecode_copies(s,off+1)with|None->None|Some(copies,off)->f(Somecopies)offend|_c->None(* unknown kind *)infNone0letwritestorageinfo=Storage.Chunk.writestorage(encodeinfo)typeError.t+=Invalidlet()=Error.register_printer@@function|Invalid->Some"Invalid commit info"|_->Noneletreadstoragei=ifi=Index.zerothenOkemptyelsematchdecode(Storage.Chunk.readstoragei)with|None->ErrorInvalid|Somev->Okv