<div dir="ltr">Hey Jens,<div><br></div><div>Thanks so much! This is really useful! I&#39;m going to get started on this.</div><div><br></div><div>Cheers!</div><div>Mohit</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 22, 2017 at 9:09 PM, Jens Alfke <span dir="ltr">&lt;<a href="mailto:jens@mooseyard.com" target="_blank">jens@mooseyard.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><span class=""><br><div><blockquote type="cite"><div>On Feb 22, 2017, at 6:05 PM, Mohit Athwani via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_-3353294231848408059Apple-interchange-newline"><div><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">I don&#39;t understand why we need the usedEncoding parameter? I understand that it&#39;s a pointer but how do we decide what encoding to use? Do we default to NSUTF8StringEncoding?</div></div></blockquote></div><br></span><div>The original implementation in Foundation uses some heuristics to try to guess the encoding, since there are unfortunately billions of plain text files out there that don’t explicitly state their encoding. It’s not open source, so we can’t know for sure [except for the people who work at Apple], but I’m sure it includes things like:</div><div><br></div><div>- Look for a Unicode BOM at the start, in which case it’s probably UTF-16 (or maybe UTF-32? I don’t know the details.)</div><div>- If not, see whether all bytes are 0x00-0x7F ⟶ in that case use ASCII</div><div>- If not, does it contain any byte sequences that are illegal in UTF-8? ⟶ If not, use UTF-8</div><div>- Otherwise, does it contain any bytes in the range 0x80-0xBF?</div><div><span class="m_-3353294231848408059Apple-tab-span" style="white-space:pre-wrap">        </span>⟶ If not, ISO-8859-1  (aka ISO-Latin-1) is a good guess</div><div><span class="m_-3353294231848408059Apple-tab-span" style="white-space:pre-wrap">        </span>⟶ If so, CP-1252 (aka WinLatin1) is a good guess; it’s a nonstandard but very common superset of ISO-8859-1 with extra characters in that byte range</div><div><br></div><div>There are likely other heuristics too. It used to be important to detect the old MacRoman encoding used in pre-OS X apps, but it’s been long enough that there shouldn’t be many docs like that in the wild anymore. There are multibyte non-Unicode encodings that used to be very common in non-Roman languages, like Shift-JIS, but I have no idea how to detect them or if they’re even still relevant.</div><div><br></div><div>It could also be useful to check whether the start of the file looks like XML or HTML, and if so, parse it enough to find where it specifies its encoding. (Are there other text formats that include encodings? I’ve seen special markings at the top of source files used for emacs or vi, specifying tab widths and such, but I don’t know if those can specify encodings too.)</div><div><br></div><div>I’m not involved in Swift development, but IMHO a basic implementation that just uses the rules I sketched above would be pretty useful, and then people with more domain knowledge could enhance that code to add more heuristics later on.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>—Jens</div></font></span></div></blockquote></div><br></div>