<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Someone on the team here just reminded me that we do have a very basic form of encoding detection here as well: just looking for the BOM at the beginning of the data.<div class=""><br class=""></div><div class="">- Tony<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jun 21, 2017, at 9:55 AM, Tony Parker via swift-corelibs-dev <<a href="mailto:swift-corelibs-dev@swift.org" class="">swift-corelibs-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Our preferred approach so far is to mirror Foundation as closely as possible.<div class=""><br class=""></div><div class="">I don’t know if we want to implement stringEncodingForData as part of swift-corelibs-foundation. In any case, we are trying to avoid bringing in as few dependencies outside of the Swift project itself as possible, to keep Foundation as low level as possible for stability, ease of use, and ease of portability.</div><div class=""><br class=""></div><div class="">- Tony<br class=""><div class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Jun 21, 2017, at 9:51 AM, Andy Best <<a href="mailto:andybest.net@gmail.com" class="">andybest.net@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Is the preferred approach to mirror Foundation as closely as possible (e.g. under Linux basically do nothing), or is implementing something like stringEncodingForData under the hood preferable in this case?</div><div class="gmail_extra"><br class=""><div class="gmail_quote">On 21 June 2017 at 17:43, Tony Parker <span dir="ltr" class=""><<a href="mailto:anthony.parker@apple.com" target="_blank" class="">anthony.parker@apple.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space" class="">Hi Andy,<div class=""><br class=""></div><div class=""><div class=""><span class=""><br class=""><blockquote type="cite" class=""><div class="">On Jun 21, 2017, at 7:39 AM, Andy Best via swift-corelibs-dev <<a href="mailto:swift-corelibs-dev@swift.org" target="_blank" class="">swift-corelibs-dev@swift.org</a>> wrote:</div><br class="m_8981510993544425545Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hey,<div class=""><br class=""></div><div class="">I've been looking at the init(contentsOfFile, usedEncoding) initializer for NSString in corelibs-foundation.</div><div class=""><br class=""></div><div class="">Am I right in thinking that this method should use some method to attempt to detect the character encoding of the file before returning a decoded String?</div></div></div></blockquote><div class=""><br class=""></div></span>In this case, the Foundation implementation just looks at an extended attribute of the file to see if it contains the encoding. If it doesn’t have the xattr then we don’t attempt to guess (name of xattr is “com.apple.TextEncoding”).</div><div class=""><br class=""></div><div class="">Foundation has another API which attempts to guess the encoding of a data blob, but I think we left it out of the swift-corelibs stubs:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class="">+ (<span style="color:#4f8187" class="">NSStringEncoding</span>)<wbr class="">stringEncodingForData:(<span style="color:#4f8187" class="">NSData</span> *)data</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""> encodingOptions:(<span style="color:#ba2da2" class="">nullable</span> <span style="color:#4f8187" class="">NSDictionary</span><<span style="color:#4f8187" class="">NSStringEncodingD<wbr class="">etectionOptionsKey</span>, <span style="color:#ba2da2" class="">id</span>> *)opts</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""> convertedString:(<span style="color:#4f8187" class="">NSString</span> * <span style="color:#ba2da2" class="">_Nullable</span> * <span style="color:#ba2da2" class="">_Nullable</span>)string</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""> usedLossyConversion:(<span style="color:#ba2da2" class="">nullable</span> <span style="color:#ba2da2" class="">BOOL</span> *)usedLossyConversion <span style="color:#78492a" class="">API_AVAILABLE</span>(macos(<span style="color:#272ad8" class="">10.10</span>), ios(<span style="color:#272ad8" class="">8.0</span>), watchos(<span style="color:#272ad8" class="">2.0</span>), tvos(<span style="color:#272ad8" class="">9.0</span>));</div><div class=""><br class=""></div><div class="">- Tony</div></div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><span class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class="">If so, I've been working on a pure Swift library to detect string encodings, and wondered if continued work on it might be useful for implementing this missing method?</div><div class=""><br class=""></div><div class="">Andy</div></div></span>
______________________________<wbr class="">_________________<br class="">swift-corelibs-dev mailing list<br class=""><a href="mailto:swift-corelibs-dev@swift.org" target="_blank" class="">swift-corelibs-dev@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-corelibs-dev" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">corelibs-dev</a><br class=""></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></div></div>_______________________________________________<br class="">swift-corelibs-dev mailing list<br class=""><a href="mailto:swift-corelibs-dev@swift.org" class="">swift-corelibs-dev@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-corelibs-dev<br class=""></div></blockquote></div><br class=""></div></body></html>