123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127typet={mutableo_s:bytes;mutableo_max_len:int;mutableo_len:int;mutableo_offs:int;o_init_len:int;o_make_room:(t->int->unit);mutableo_shared:Bi_share.Wr.tbl;o_shared_init_len:int;}letreally_extendbn=letslen0=b.o_max_leninletreqlen=b.o_len+ninletslen=letx=maxreqlen(2*slen0)inifx<=Sys.max_string_lengththenxelseifSys.max_string_length<reqlentheninvalid_arg"Buf.extend: reached Sys.max_string_length"elseSys.max_string_lengthinlets=Bytes.createsleninBytes.blitb.o_s0s0b.o_len;b.o_s<-s;b.o_max_len<-slenletflush_to_outputabstract_outputbn=abstract_output(Bytes.to_stringb.o_s)0b.o_len;b.o_offs<-b.o_offs+b.o_len;b.o_len<-0;ifn>b.o_max_lenthenreally_extendbnletflush_to_channeloc=flush_to_output(output_substringoc)letcreate?(make_room=really_extend)?(shrlen=16)n={o_s=Bytes.createn;o_max_len=n;o_len=0;o_offs=0;o_init_len=n;o_make_room=make_room;o_shared=Bi_share.Wr.createshrlen;o_shared_init_len=shrlen;}letcreate_channel_writer?(len=4096)?shrlenoc=create~make_room:(flush_to_channeloc)?shrlenlenletflush_channel_writerb=b.o_make_roomb0letcreate_output_writer?(len=4096)?shrlenout=create~make_room:(flush_to_outputout#output)?shrlenlenletflush_output_writer=flush_channel_writer(*
Guarantee that the buffer string has enough room for n additional bytes.
*)letextendbn=ifb.o_len+n>b.o_max_lenthenb.o_make_roombnletallocbn=extendbn;letpos=b.o_leninb.o_len<-pos+n;posletadd_subblitbsposlen=extendblen;blitsposb.o_sb.o_lenlen;b.o_len<-b.o_len+lenletadd_substring=add_subString.blitletadd_subbytes=add_subBytes.blitletadd_stringbs=add_substringbs0(String.lengths)letadd_bytesbs=add_subbytesbs0(Bytes.lengths)letadd_charbc=letpos=allocb1inBytes.setb.o_sposcletunsafe_add_charbc=letlen=b.o_leninBytes.setb.o_slenc;b.o_len<-len+1letadd_char2bc1c2=letpos=allocb2inlets=b.o_sinBytes.unsafe_setsposc1;Bytes.unsafe_sets(pos+1)c2letadd_char4bc1c2c3c4=letpos=allocb4inlets=b.o_sinBytes.unsafe_setsposc1;Bytes.unsafe_sets(pos+1)c2;Bytes.unsafe_sets(pos+2)c3;Bytes.unsafe_sets(pos+3)c4letclearb=b.o_offs<-0;b.o_len<-0;Bi_share.Wr.clearb.o_sharedletresetb=ifBytes.lengthb.o_s<>b.o_init_lenthenb.o_s<-Bytes.createb.o_init_len;b.o_offs<-0;b.o_len<-0;b.o_shared<-Bi_share.Wr.createb.o_shared_init_lenletcontentsb=Bytes.sub_stringb.o_s0b.o_len