1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374moduleString=StringLabelstypequote=|Needs_quoting_with_lengthofint|No_quotingletquote_lengths=letn=ref0inletlen=String.lengthsinletneeds_quoting=reffalseinfori=0tolen-1don:=!n+(matchString.unsafe_getsiwith|'\"'|'\\'|'\n'|'\t'|'\r'|'\b'->needs_quoting:=true;2|' '->needs_quoting:=true;1|'!'..'~'->1|_->needs_quoting:=true;4)done;if!needs_quotingthenNeeds_quoting_with_lengthlenelse(assert(len=!n);No_quoting)letescape_tos~dst:s'~ofs=letn=refofsinletlen=String.lengthsinfori=0tolen-1dobeginmatchString.unsafe_getsiwith|('\"'|'\\')asc->Bytes.unsafe_sets'!n'\\';incrn;Bytes.unsafe_sets'!nc|'\n'->Bytes.unsafe_sets'!n'\\';incrn;Bytes.unsafe_sets'!n'n'|'\t'->Bytes.unsafe_sets'!n'\\';incrn;Bytes.unsafe_sets'!n't'|'\r'->Bytes.unsafe_sets'!n'\\';incrn;Bytes.unsafe_sets'!n'r'|'\b'->Bytes.unsafe_sets'!n'\\';incrn;Bytes.unsafe_sets'!n'b'|(' '..'~')asc->Bytes.unsafe_sets'!nc|c->leta=Char.codecinBytes.unsafe_sets'!n'\\';incrn;Bytes.unsafe_sets'!n(Char.unsafe_chr(48+a/100));incrn;Bytes.unsafe_sets'!n(Char.unsafe_chr(48+(a/10)mod10));incrn;Bytes.unsafe_sets'!n(Char.unsafe_chr(48+amod10));end;incrndone(* Surround [s] with quotes, escaping it if necessary. *)letquote_if_neededs=letlen=String.lengthsinmatchquote_lengthswith|No_quoting->s|Needs_quoting_with_lengthn->lets'=Bytes.create(n+2)inBytes.unsafe_sets'0'"';iflen=0||n>lenthenescape_tos~dst:s'~ofs:1elseBytes.blit_string~src:s~src_pos:0~dst:s'~dst_pos:1~len;Bytes.unsafe_sets'(n+1)'"';Bytes.unsafe_to_strings'