12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394moduleString=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;4done;if!needs_quotingthenNeeds_quoting_with_lengthlenelse(assert(len=!n);No_quoting)letescape_tos~dst:s'~ofs=letn=refofsinletlen=String.lengthsinfori=0tolen-1do(matchString.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/10mod10)));incrn;Bytes.unsafe_sets'!n(Char.unsafe_chr(48+(amod10))));incrndone(* Surround [s] with quotes, escaping it if necessary. *)letquote_if_neededs=letlen=String.lengthsinmatchquote_lengthswith|No_quoting->ifs=""then"\"\""elses|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'