1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889(* Copyright (C) 2024 Petter A. Urkedal <paurkedal@gmail.com>
*
* This library 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 (at your
* option) any later version, with the LGPL-3.0 Linking Exception.
*
* This library 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 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.
*)typet=stringletrecskip_zeros_fromsi=ifi=String.lengths||s.[i]<>'0'thenielseskip_zeros_froms(i+1)letrecskip_digits_fromsi=ifi=String.lengthsthenielse(matchs.[i]with|'0'..'9'->skip_digits_froms(i+1)|_->i)letreccompare_with_empty_fromvi=ifi=String.lengthvthen0else(matchv.[i]with|'~'->-1|'0'|'.'->compare_with_empty_fromv(i+1)|_->1)letcompare_charchLchR=ifchL=chRthen0elseifchL='~'then-1elseifchR='~'then+1elseChar.comparechLchRletcomparevLvR=letnL,nR=String.lengthvL,String.lengthvRinletrecstartiLiR=ifiL=nL&&iR=nRthen0elseifiL=nLthen-compare_with_empty_fromvRiRelseifiR=nRthen+compare_with_empty_fromvLiLelse(matchvL.[iL],vR.[iR]with|'.','.'->start(iL+1)(iR+1)|'0'..'9','0'..'9'->letiL=skip_zeros_fromvLiLinletiR=skip_zeros_fromvRiRinletkL=skip_digits_fromvLiLinletkR=skip_digits_fromvRiRinifkL-iL<kR-iRthen-1elseifkL-iL>kR-iRthen+1elseletc=digitsiLiR(kL-iL)inifc<0then-1elseifc>0then+1elsestartkLkR|chL,chR->letc=compare_charchLchRinifc<0then-1elseifc>0then+1elsestart(iL+1)(iR+1))anddigitsiLiRn=ifn=0thenstartiLiRelseletc=Char.comparevL.[iL]vR.[iR]inifc<0then-1elseifc>0then+1elsedigits(iL+1)(iR+1)(n-1)instart00letequalvLvR=comparevLvR=0letpp=Format.pp_print_stringletof_string_unsafeversion=versionmoduleInfix=structlet(=*)v1v2=comparev1v2=0let(<>*)v1v2=comparev1v2<>0let(<*)v1v2=comparev1v2<0let(<=*)v1v2=comparev1v2<=0let(>*)v1v2=comparev1v2>0let(>=*)v1v2=comparev1v2>=0end