12345678910111213141516171819202122232425262728(** Encodes a string using the given translation function that maps a character
to a string that is its encoded version, if that character needs encoding. *)letencodetranslates=letn=String.lengthsinletneed_encoding=letb=reffalseinleti=ref0inwhile(not!b)&&!i<ndob:=translates.[!i]<>None;incridone;!binifneed_encodingthen(letbuf=Buffer.create0inletm=ref0infori=0ton-1domatchtranslates.[i]with|None->()|Somen->Buffer.add_substringbufs!m(i-!m);Buffer.add_stringbufn;m:=i+1done;Buffer.add_substringbufs!m(n-!m);Buffer.contentsbuf)elses