123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196# 1 "Camomile/public/uTF16.ml"(** UTF-16 encoded string. *)(* Copyright (C) 2002, 2003, 2004 Yamagata Yoriyuki. *)(* 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 2 of *)(* the License, or (at your option) any later version. *)(* As a special exception to the GNU Library General Public License, you *)(* may link, statically or dynamically, a "work that uses this library" *)(* with a publicly distributed version of this library to produce an *)(* executable file containing portions of this library, and distribute *)(* that executable file under terms of your choice, without any of the *)(* additional requirements listed in clause 6 of the GNU Library General *)(* Public License. By "a publicly distributed version of this library", *)(* we mean either the unmodified Library as distributed by the authors, *)(* or a modified version of this library that is distributed under the *)(* conditions defined in clause 3 of the GNU Library General Public *)(* License. This exception does not however invalidate any other reasons *)(* why the executable file might be covered by the GNU Library General *)(* Public License . *)(* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *)(* USA *)(* You can contact the authour by sending email to *)(* yoriyuki.y@gmail.com *)openBigarrayexceptionOut_of_range(* UTF-16 encoded string. the type is bigarray of 16-bits integers. *)typet=(int,int16_unsigned_elt,c_layout)Array1.ttypeindex=intexceptionMalformed_codeletrecvalidate_aux(a:t)i=ifi>=Array1.dimathen()elseletn=a.{i}inifn<0xd800||n>=0xe000&&n<0xfffethenvalidate_auxa(i+1)elseifn>=0xd800&&n<0xdc00thenifi+1>=Array1.dimathenraiseMalformed_codeelseletn'=a.{i+1}inifn'<0xdc00||n'>=0xe000thenraiseMalformed_codeelsevalidate_auxa(i+2)elseraiseMalformed_codeletvalidate(a:t)=validate_auxa0letlook(a:t)i:UChar.t=letn0=a.{i}inifn0<0xd800||n0>=0xe000thenUChar.chr_of_uintn0elseifn0<0xdc00thenletn1=a.{i+1}inUChar.chr_of_uint(((n0-0xd800)lsl10)+(n1-0xdc00)+0x10000)elseinvalid_arg"UTF16.look"letreclength_aux(a:t)ci=ifi>=Array1.dimathencelseletn=a.{i}inifn<0xd800||n>=0xe000thenlength_auxa(c+1)(i+1)elselength_auxa(c+1)(i+2)letlength(a:t)=length_auxa00letnext(a:t)i=letn=a.{i}inifn<0xd800||n>=0xdc00theni+1elsei+2letprev(a:t)i=leti'=i-1inletn=a.{i'}inifn<0xdc00||n>=0xe000theni'elsei'-1letrecmove_forward(a:t)ic=ifc>0thenmove_forwarda(nextai)(c-1)elseiletrecmove_backward(a:t)ic=ifc<0thenmove_backwarda(prevai)(c+1)elseiletmove(a:t)ic=ifc>0thenmove_forwardaicelseifc<0thenmove_backwardaicelseiletfirst_=0letlast(a:t)=preva(Array1.dima)letout_of_range(a:t)i=i<0||i>=Array1.dimaletcompare_index_ij=i-jletnth(a:t)c=move_forwarda0cletget(a:t)c=looka(nthac)letreciter_auxproc(a:t)i=ifi>=Array1.dimathen()elsebeginproc(lookai);iter_auxproca(nextai)endletiterproc(a:t)=iter_auxproca0moduleBuf=structletset(a:t)iu=letn=UChar.uint_codeuinifn<0thenraiseOut_of_rangeelseifn<0xd800||n>=0xe000&&n<=0xfffdthenbegina.{i}<-n;1endelseifn>=0x10000&&n<=0x10ffffthenbegina.{i}<-((n-0x10000)lsr10)+0xd800;a.{i+1}<-((n-0x10000)land0x3ff)+0xdc00;2endelseraiseOut_of_rangetypebuf={init_size:int;mutablepos:index;mutablecontents:t}letcreaten=letcontents=Array1.createint16_unsignedc_layoutnin{init_size=n;pos=0;contents=contents}letclearbuf=buf.pos<-0letresetbuf=buf.contents<-Array1.createint16_unsignedc_layoutbuf.init_size;buf.pos<-0letcontentsbuf=leta=Array1.createint16_unsignedc_layoutbuf.posinletsrc=Array1.subbuf.contents0buf.posinArray1.blitsrca;aletresizebufn=ifArray1.dimbuf.contents>=nthen()elseleta=Array1.createint16_unsignedc_layout(2*n)inleta'=Array1.suba0(Array1.dimbuf.contents)inArray1.blitbuf.contentsa';buf.contents<-aletadd_charbufu=resizebuf(buf.pos+2);buf.pos<-buf.pos+setbuf.contentsbuf.posuletadd_stringbuf(a:t)=letlen=buf.pos+Array1.dimainresizebuflen;letb=Array1.subbuf.contentsbuf.pos(Array1.dima)inArray1.blitab;buf.pos<-lenletadd_bufferbuf1buf2=letlen=buf1.pos+buf2.posinresizebuf1len;leta=Array1.subbuf2.contents0buf2.posinletb=Array1.subbuf1.contentsbuf1.posbuf2.posinArray1.blitab;buf1.pos<-lenendletinitlenf=letbuf=Buf.create(len+1)infori=0tolen-1doBuf.add_charbuf(fi)done;Buf.contentsbufletreccompare_aux(a:t)bi=ifi>=Array1.dimathen0elseletn1=a.{i}inletn2=b.{i}inifn1=n2thencompare_auxab(i+1)else(ifn1<0xd800||n1>=0xdc00thenn1else0x10000lorn1)-(ifn2<0xd800||n2>=0xdc00thenn2else0x10000lorn2)letcompare(a:t)b=letsgn=Array1.dima-Array1.dimbinifsgn=0thencompare_auxab0elsesgn