123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105(* CSS Syntax 3 (ED) sec. 3.3 turns CR, FF and the CRLF pair into one LF before
tokenizing, and a column is one element of the decoded stream: a code point,
or one U+FFFD per maximal ill-formed subpart (sec. 3.2). A source the reader
already preprocessed holds only LF; counting the other breaks as well gives
the same answer on a raw string.
A walk is the state after decoding the elements before byte [i]. [after_cr]
holds the LF of a CRLF pair back from opening a second line. *)typewalk={mutablei:int;mutableline:int;mutablecol:int;mutableafter_cr:bool;}letstart()={i=0;line=1;col=1;after_cr=false}letbreakw=w.line<-w.line+1;w.col<-1(* Decode [source] from [w.i] up to the first element boundary at or after
[until], inside the window that ends at [stop]. A sequence [stop] cuts short
is one ill-formed element. Every byte below 0x80 is an element of its own, so
only the others go through the decoder. *)letadvancesourcew~until~stop=whilew.i<untildoleti=w.iinmatchString.unsafe_getsourceiwith|'\n'->ifw.after_crthenw.after_cr<-falseelsebreakw;w.i<-i+1|'\x0C'->breakw;w.after_cr<-false;w.i<-i+1|'\r'->breakw;w.after_cr<-true;w.i<-i+1|c->letn=ifChar.codec<0x80then1elseUchar.utf_decode_length(String.get_utf_8_ucharsourcei)inw.col<-w.col+1;w.after_cr<-false;w.i<-minstop(i+n)doneletclampsourcepos=max0(minpos(String.lengthsource))(* Mark [k] is the walk at the first element boundary at or after byte [k *
stride], three slots wide: the boundary, the line, and the column doubled
with [after_cr] in the low bit. An element is at most four bytes, so every
multiple of the stride up to the length has a mark of its own, and [marks]
holds marks [1 .. length / stride]; mark [0] is the start of the source. The
marks are the only state, written once and the same however many readers race
to build them, so lookups share [t] freely. *)typet={source:string;mutablemarks:intarrayoption}letstride=1024letvsource={source;marks=None}letsourcet=t.sourceletcountprobesn=matchprobeswithSomep->p:=!p+n|None->()letbuild?probest=letlen=String.lengtht.sourceinletcount_marks=len/strideinletmarks=Array.make(3*count_marks)0inletw=start()infork=1tocount_marksdoadvancet.sourcew~until:(k*stride)~stop:len;letslot=3*(k-1)inmarks.(slot)<-w.i;marks.(slot+1)<-w.line;marks.(slot+2)<-(2*w.col)+Bool.to_intw.after_crdone;countprobesw.i;t.marks<-Somemarks;marks(* The walk at the last mark at or before [stop]. The mark of the stride [stop]
falls in can sit up to three bytes past it. *)letresumemarksstop=letk=stop/strideinletk=ifk>0&&marks.(3*(k-1))>stopthenk-1elsekinifk=0thenstart()elseletslot=3*(k-1)inletpacked=marks.(slot+2)in{i=marks.(slot);line=marks.(slot+1);col=packed/2;after_cr=packedland1=1;}letlocate?probestpos=letstop=clampt.sourceposinletmarks=matcht.markswithSomem->m|None->build?probestinletw=resumemarksstopincountprobes(stop-w.i);advancet.sourcew~until:stop~stop;(w.line,w.col)