123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329(*******************************************************************************)(* Volgo - a Versatile OCaml Library for Git Operations *)(* Copyright (C) 2024-2025 Mathieu Barbin <mathieu.barbin@gmail.com> *)(* *)(* This file is part of Volgo. *)(* *)(* Volgo is free software; you can redistribute it and/or modify it under *)(* the terms of the GNU Lesser General Public License as published by the *)(* Free Software Foundation either version 3 of the License, or any later *)(* version, with the LGPL-3.0 Linking Exception. *)(* *)(* Volgo is distributed in the hope that it will be useful, but WITHOUT ANY *)(* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *)(* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License and *)(* the file `NOTICE.md` at the root of this repository for more details. *)(* *)(* You should have received a copy of the GNU Lesser General Public License *)(* and the LGPL-3.0 Linking Exception along with this library. If not, see *)(* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively. *)(*******************************************************************************)open!Stdlib_compatmoduleArray=structincludeArrayLabelsletcreate~lena=makelenaletfilter_mapit~f=letout_count=ref0inletout=ref[]initerit~f:(funia->matchfiawith|None->()|Somee->incrout_count;out:=e::!out);match!outwith|[]->[||]|hd::_->letout_count=!out_countinletres=create~len:out_counthdinList.iteri(funia->res.(out_count-1-i)<-a)!out;res;;letreva=letlen=lengthainletres=create~lena.(0)initeria~f:(funix->res.(len-1-i)<-x);res;;letsortt~compare=sortt~cmp:compareendmoduleChar=structincludeCharletis_alphanum=function|'a'..'z'|'A'..'Z'|'0'..'9'->true|_->false;;letis_whitespace=function|'\t'|'\n'|'\011'(* vertical tab *)|'\012'(* form feed *)|'\r'|' '->true|_->false;;endmoduleHashtbl=structinclude(MoreLabels.Hashtbl:moduletypeofMoreLabels.HashtblwithmoduleMake:=MoreLabels.Hashtbl.Make)moduletypeS_extended=sigincludeMoreLabels.Hashtbl.Svaladd_exn:'at->key:key->data:'a->unitvaladd_multi:'alistt->key:key->data:'a->unitvalfind:'at->key->'aoptionvalset:'at->key:key->data:'a->unitendexceptionEofSexp.tlet()=Sexplib0.Sexp_conv.Exn_converter.add[%extension_constructorE](function|Esexp->sexp|_->assertfalse);;moduleMake(H:sigincludeHashtbl.HashedTypevalsexp_of_t:t->Sexp.tend)=structincludeMoreLabels.Hashtbl.Make(H)letadd_exnt~key~data=ifmemtkeythenraise(E[%sexp"Hashtbl.add_exn: key already present",{key:H.t}])elseaddt~key~data;;letadd_multit~key~data=letdata=matchfind_opttkeywith|None->[data]|Somel->data::linreplacet~key~data;;letfind=find_optletset=replaceendendmoduleInt=structincludeIntletincr=incrletmax_value=max_intletof_strings=tryint_of_stringswith|_->failwith(Printf.sprintf"Int.of_string: %S"s);;letof_string_opt=int_of_string_optletto_string_humn=lets=string_of_intninletlen=String.lengthsinletis_negative=n<0inletsign_count=ifis_negativethen1else0inletabsolute_digit_count=ifis_negativethenlen-1elseleninletseparator_count=absolute_digit_count/3inletinitial_skip_count=letdigit_skip=absolute_digit_countmod3insign_count+ifdigit_skip>0thendigit_skipelse3inletbuffer=Buffer.create(len+separator_count)inletrecauxicount=ifi<lenthenifcount=0then(Buffer.add_charbuffer'_';auxi3)else(Buffer.add_charbuffers.[i];aux(i+1)(count-1))inaux0initial_skip_count;Buffer.contentsbuffer;;endmoduleList=structincludeListLabelsletdedup_and_sortt~compare=sort_uniqt~cmp:comparelethd=function|[]->None|hd::_->Somehd;;letfilter_optt=filter_mapt~f:Fun.idletfindt~f=find_optt~fletfoldt~init~f=fold_left~f~inittletsortt~compare=sortt~cmp:compareendmoduleOption=structincludeOptionletmapt~f=mapftletsome_ifconda=ifcondthenSomeaelseNoneendmoduleOrdering=structtypet=|Less|Equal|Greaterletof_inti=ifi<0thenLesselseifi=0thenEqualelseGreaterendmoduleQueue=structincludeQueueletenqueueta=pushatletto_listt=t|>to_seq|>List.of_seqendmoduleResult=structtype('a,'b)t=('a,'b)Result.t=|Okof'a|Errorof'b[@@derivingsexp_of]include(Result:moduletypeofResultwithtype('a,'b)t:=('a,'b)t)moduleMonad_syntax=structlet(let*)=Result.bindendletmapt~f=mapftletmap_errort~f=map_errorftletof_optiont~error=matchtwith|Somev->Okv|None->Errorerror;;letreturn=okendmoduleString=structincludeStringLabelsletto_stringt=tletchop_prefixt~prefix=ifAstring.String.is_prefixt~affix:prefixthenSome(Astring.String.with_index_ranget~first:(String.lengthprefix))elseNone;;letchop_suffixt~suffix=ifAstring.String.is_suffixt~affix:suffixthenSome(Astring.String.with_index_ranget~last:(String.lengtht-String.lengthsuffix-1))elseNone;;letinitlen~f=String.initlenfletis_empty=Astring.String.is_emptyletlsplit2t~on=Astring.String.cut~sep:(String.make1on)tletrsplit2t~on=Astring.String.cut~sep:(String.make1on)t~rev:true(* The function [split_lines] below was copied from [Base.String0.split_lines]
version [v0.17] which is released under MIT and may be found at
[https://github.com/janestreet/base].
The changes we made were minimal:
- Changed references to [Char0] to [Char].
See Base's LICENSE below:
----------------------------------------------------------------------------
The MIT License
Copyright (c) 2016--2024 Jane Street Group, LLC <opensource-contacts@janestreet.com>
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. *)letsplit_lines=letback_up_at_newline~t~pos~eol=pos:=!pos-if!pos>0&&Char.equalt.[!pos-1]'\r'then2else1;eol:=!pos+1infunt->letn=lengthtinifn=0then[]else((* Invariant: [-1 <= pos < eol]. *)letpos=ref(n-1)inleteol=refninletac=ref[]in(* We treat the end of the string specially, because if the string ends with a
newline, we don't want an extra empty string at the end of the output. *)ifChar.equalt.[!pos]'\n'thenback_up_at_newline~t~pos~eol;while!pos>=0doifnot(Char.equalt.[!pos]'\n')thendecrposelse((* Because [pos < eol], we know that [start <= eol]. *)letstart=!pos+1inac:=subt~pos:start~len:(!eol-start)::!ac;back_up_at_newline~t~pos~eol)done;subt~pos:0~len:!eol::!ac);;(* ---------------------------------------------------------------------------- *)letsplitt~on=split_on_char~sep:ontletstrip=trimletuncapitalize=uncapitalize_asciiendletcompare_bool=Bool.compareletcompare_int=Int.compareletcompare_string=String.compareletequal_bool=Bool.equalletequal_int=Int.equalletequal_string=String.equalletequal_listeqab=List.equal~eqablethash_string=String.hash