123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157(*
RE - A regular expression library
Copyright (C) 2001 Jerome Vouillon
email: Jerome.Vouillon@pps.jussieu.fr
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, with
linking exception; either version 2.1 of the License, or (at
your option) any later version.
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 along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*)typec=inttypet=(c*c)listletrecunionll'=matchl,l'with_,[]->l|[],_->l'|(c1,c2)::r,(c1',c2')::r'->ifc2+1<c1'then(c1,c2)::unionrl'elseifc2'+1<c1then(c1',c2')::unionlr'elseifc2<c2'thenunionr((minc1c1',c2')::r')elseunion((minc1c1',c2)::r)r'letrecinterll'=matchl,l'with_,[]->[]|[],_->[]|(c1,c2)::r,(c1',c2')::r'->ifc2<c1'theninterrl'elseifc2'<c1theninterlr'elseifc2<c2'then(maxc1c1',c2)::interrl'else(maxc1c1',c2')::interlr'letrecdiffll'=matchl,l'with_,[]->l|[],_->[]|(c1,c2)::r,(c1',c2')::r'->ifc2<c1'then(c1,c2)::diffrl'elseifc2'<c1thendifflr'elseletr''=ifc2'<c2then(c2'+1,c2)::relserinifc1<c1'then(c1,c1'-1)::diffr''r'elsediffr''r'letsinglec=[c,c]letaddcl=union(singlec)lletseqcc'=ifc<=c'then[c,c']else[c',c]letrecoffsetol=matchlwith[]->[]|(c1,c2)::r->(c1+o,c2+o)::offsetorletempty=[]letrecmem(c:int)s=matchswith[]->false|(c1,c2)::rem->ifc<=c2thenc>=c1elsememcrem(****)typehash=intletrechash_rec=function|[]->0|(i,j)::r->i+13*j+257*hash_recrlethashl=(hash_recl)land0x3FFFFFFF(****)letprint_onech(c1,c2)=ifc1=c2thenFormat.fprintfch"%d"c1elseFormat.fprintfch"%d-%d"c1c2letpp=Fmt.listprint_oneletrecitert~f=matchtwith|[]->()|(x,y)::xs->fxy;iterxs~fletone_char=function|[i,j]wheni=j->Somei|_->NonemoduleCSetMap=Map.Make(structtypet=int*(int*int)listletcompare(i,u)(j,v)=letc=compareijinifc<>0thencelsecompareuvend)letfold_rightt~init~f=List.fold_rightftinitletcsinglec=single(Char.codec)letcany=[0,255]letis_empty=function|[]->true|_->falseletrecprependsxl=matchs,lwith|[],_->l|_r,[]->[]|(_c,c')::r,([d,_d'],_x')::_r'whenc'<d->prependrxl|(c,c')::r,([d,d'],x')::r'->ifc<=dthenbeginifc'<d'then([d,c'],x@x')::prependrx(([c'+1,d'],x')::r')else([d,d'],x@x')::prependsxr'endelsebeginifc>d'then([d,d'],x')::prependsxr'else([d,c-1],x')::prependsx(([c,d'],x')::r')end|_->assertfalseletpick=function|[]->invalid_arg"Re_cset.pick"|(x,_)::_->x