123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175open!Core(*= Bytebuffer is split into three regions using two separate indices that are used
to support read and write operations.
+--------------------+---------------------------+----------------------------+
| Consumed Bytes | Bytes available to read | Empty space for writing |
+--------------------+---------------------------+----------------------------+
| 0 <= pos_read <= pos_fill <= capacity
Consumed Bytes: This is content that's already consumed via a get/read operation.
This space can be safely reclaimed.
Bytes available to read: This is the actual content that will be surfaced to users via
get/read operations on the bytebuffer.
Empty space for writing: This is space that will be filled by any set/write operations
on the bytebuffer.
*)typet={mutablebuf:(Bigstring.t[@sexp.opaque]);mutablepos_read:int;mutablepos_fill:int}[@@derivingsexp_of]letcreatesize=ifsize<=0thenraise_s[%message"Buffer size cannot be negative"~requested_size:(size:int)];letbuf=Bigstring.createsizein{buf;pos_read=0;pos_fill=0};;letcompactt=ift.pos_read>0then(letlen=t.pos_fill-t.pos_readiniflen=0then(t.pos_read<-0;t.pos_fill<-0)else(Bigstring.blit~src:t.buf~dst:t.buf~src_pos:t.pos_read~dst_pos:0~len;t.pos_read<-0;t.pos_fill<-len));;letlengtht=t.pos_fill-t.pos_readletcapacityt=Bigstring.lengtht.bufletavailable_to_writet=Bigstring.lengtht.buf-t.pos_fillletdroptlen=iflen<0||len>lengthttheninvalid_arg"Bytebuffer.drop: Index out of bounds";t.pos_read<-t.pos_read+len;;letreadtfd=letcount=Bigstring_unix.readfdt.buf~pos:t.pos_fill~len:(Bigstring.lengtht.buf-t.pos_fill)inifcount>0thent.pos_fill<-t.pos_fill+count;count;;letwritetfd=letcount=Bigstring_unix.writefdt.buf~pos:t.pos_read~len:(lengtht)inifcount>0thent.pos_read<-t.pos_read+count;count;;letread_assume_fd_is_nonblockingtfd=letres=Bigstring_unix.read_assume_fd_is_nonblockingfdt.buf~pos:t.pos_fill~len:(Bigstring.lengtht.buf-t.pos_fill)inifCore_unix.Syscall_result.Int.is_okresthen(letcount=Core_unix.Syscall_result.Int.ok_exnresinifcount>0thent.pos_fill<-t.pos_fill+count);res;;letwrite_assume_fd_is_nonblockingtfd=letres=Bigstring_unix.write_assume_fd_is_nonblockingfdt.buf~pos:t.pos_read~len:(lengtht)inifres>0thent.pos_read<-t.pos_read+res;res;;letensure_spacetlen=ifavailable_to_writet<lenthen(letnew_length=Bigstring.lengtht.buf+leninletcurr_len=t.pos_fill-t.pos_readinletlen=Int.ceil_pow2new_lengthinletnew_buf=Bigstring.createleninBigstring.unsafe_blit~src:t.buf~dst:new_buf~src_pos:t.pos_read~dst_pos:0~len:curr_len;t.buf<-new_buf;t.pos_read<-0;t.pos_fill<-curr_len);;letadd_chartch=ensure_spacet1;Bigstring.sett.buft.pos_fillch;t.pos_fill<-t.pos_fill+1;;letadd_gent?(pos=0)?len~total_length~blitstr=letlen=matchlenwith|Somei->i|None->total_length-posinOrdered_collection_common.check_pos_len_exn~pos~len~total_length;ensure_spacetlen;blit~src:str~src_pos:pos~dst:t.buf~dst_pos:t.pos_fill~len;t.pos_fill<-t.pos_fill+len;;letadd_stringt?pos?lenstr=add_gent?pos?len~total_length:(String.lengthstr)~blit:Bigstring.From_string.blitstr;;letadd_bytest?pos?lenstr=add_gent?pos?len~total_length:(Bytes.lengthstr)~blit:Bigstring.From_bytes.blitstr;;letadd_bigstringt?pos?lenstr=add_gent?pos?len~total_length:(Bigstring.lengthstr)~blit:Bigstring.blitstr;;letadd_bytebuffertbuf=add_bigstringt~pos:buf.pos_read~len:(lengthbuf)buf.bufletto_stringt=Bigstring.To_string.subt.buf~pos:t.pos_read~len:(lengtht)moduleSlice=structtypet={buf:(Bigstring.t[@sexp.opaque]);pos:int;len:int}[@@derivingsexp_of]endletunsafe_peekt={Slice.buf=t.buf;pos=t.pos_read;len=lengtht}letslice?(pos=0)?lent=lettotal_length=lengthtinletlen=matchlenwith|None->total_length-pos|Somel->linOrdered_collection_common.check_pos_len_exn~pos~len~total_length;{Slice.buf=t.buf;pos=pos+t.pos_read;len};;letunsafe_indextch=letidx=Bigstring.unsafe_findt.bufch~pos:t.pos_read~len:(lengtht)inifidx<0then-1elseidx-t.pos_read;;