<div dir="ltr"><div><div><div><div>This frustrated me in Ruby unicode too....<br><br></div>Typically i/o is the ultimate in "untrusted and untrustworthy" sources, coming usually from systems beyond my control.<br>
<br></div>Likely to be corrupted, or maliciously crafted, or defective...<br><br></div>Unfortunately not all sequences of bytes are valid UTF8.<br><br>Thus inevitably in every collection of inputs there are always going to be around 1 in a million codepoints resulting in an UTFException thrown.<br>
<br></div>Alas, I always have to do Regex matches on the other 999999 valid codepoints.....<br clear="all"><div><div><div><div><div><br></div><div>Is there a standard recipe in stdio for squashing bad codepoints to some default?<br>
<br></div><div>These days memory is very much larger than most files I want to scan.<br><br>So if I was doing this in C I would typically mmap the file PROT_READ | PROT_WRITE and MAP_PRIVATE then run down the file squashing bad codepoints and then run down it again matching patterns.<br>
</div><div><br></div><div>In Ruby I have a horridly inefficient utility....<br></div><div> def IO.read_utf_8(file)<br> read(file,:external_encoding=>'ASCII-8BIT').encode('UTF-8',:undef=>:replace)<br>
end<br><br></div><div>What is the idiomatic D solution to this conundrum?<br></div><div><br>-- <br><div dir="ltr">John Carter<br>Phone : (64)(3) 358 6639<br>Tait Electronics <br>PO Box 1645 Christchurch<br>
New Zealand<br><br></div>
</div></div></div></div></div></div>
<br>
<hr><font color="#808080">This email, including any attachments, is only
for the intended recipient. It is subject to copyright, is confidential
and may be the subject of legal or other privilege, none of which is
waived or lost by reason of this transmission.</font><div><font color="#808080">If
you are not an intended recipient, you may not use, disseminate,
distribute or reproduce such email, any attachments, or any part
thereof. If you have received a message in error, please notify the
sender immediately and erase all copies of the message and any
attachments.</font></div><div><font color="#808080">Unfortunately, we
cannot warrant that the email has not been altered or corrupted during
transmission nor can we guarantee that any email or any attachments are
free from computer viruses or other conditions which may damage or
interfere with recipient data, hardware or software. The recipient
relies upon its own procedures and assumes all risk of use and of
opening any attachments.</font></div><div><hr></div>