<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">I hope I am correct about the no-copy thing, and I would also like to<br class="">permit promoting C strings to Swift strings without validation. This<br class="">is obviously unsafe in general, but I know my strings... and I care<br class="">about performance. ;)<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">We intend to support that use-case. That's part of the reason for the</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">ValidUTF8 and ValidUTF16 encodings you see here:</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://github.com/apple/swift/blob/unicode-rethink/stdlib/public/core/Unicode2.swift#L598" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://github.com/apple/swift/blob/unicode-rethink/stdlib/public/core/Unicode2.swift#L598</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">and here:</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://github.com/apple/swift/blob/unicode-rethink/stdlib/public/core/Unicode2.swift#L862" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://github.com/apple/swift/blob/unicode-rethink/stdlib/public/core/Unicode2.swift#L862</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote></div><br class=""><div class="">It seems a little strange to me that a pre-validated UTF8 string from C would have different types to a UTF8String (i.e. using ValidUTF8 vs UTF8). It defeats the point of having the encoding represented in the type-system.</div><div class=""><br class=""></div><div class="">For example, if I write a generic function:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Courier" class="">func sendMessage<Source: Unicode where Source.Encoding == UTF8>(from: Source)</font></div></blockquote><div class=""><br class=""></div><div class="">I would only be able to accept UTF-8 text which hasn’t already been validated. </div><div class=""><br class=""></div><div class="">What about if we allowed each encoding to provide multiple kinds of decoder? That would also allow us to substitute our own decoders in, if there are application-specific shortcuts we can take.</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Courier" class="">protocol UnicodeEncoding {</font></div><div class=""><font face="Courier" class=""> associatedtype CodeUnit</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><span style="font-family: Courier;" class=""> associatedtype ValidatingDecoder: UnicodeDecoder</span></div><span style="font-family: Courier;" class=""> associatedtype NonValidatingDecoder: UnicodeDecoder</span></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><span style="font-family: Courier;" class="">}</span></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">protocol UnicodeDecoder {</font></div><div class=""><font face="Courier" class=""> associatedtype Encoding: UnicodeEncoding</font></div><div class=""><font face="Courier" class=""> associatedtype DecodedScalar: RandomAccessCollection where Iterator.Element == Encoding.CodeUnit</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class=""> static func parse1Forward<C>(…) -> ParseResult<DecodedScalar, C.Index></font></div><div class=""><font face="Courier" class=""> static func parse1Backward<C>(…) -> ParseResult<DecodedScalar, C.Index></font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><font face="Courier" class="">// Not shown: UnicodeEncoder protocol, with transcodeScalar<T> function.</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><div class=""><font face="Courier" class="">struct UTF8: UnicodeEncoding { </font></div><div class=""><font face="Courier" class=""> typealias CodeUnit = UInt8 </font></div><div class=""><font face="Courier" class=""> typealias ValidatingDecoder = ValidatingUTF8Decoder</font></div><div class=""><span style="font-family: Courier;" class=""> typealias NonValidatingDecoder = NonValidatingUTF8Decoder</span></div><div class=""><font face="Courier" class="">}</font></div></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><div class=""><font face="Courier" class="">struct NonValidatingUTF8Decoder: UnicodeDecoder {</font></div></div><div class=""><div class=""><font face="Courier" class=""> typealias Encoding = UTF8</font></div></div><div class=""><div class=""><font face="Courier" class=""> struct DecodedScalar: RandomAccessCollection { … }</font></div></div><div class=""><div class=""><font face="Courier" class=""> // Parsing functions</font></div></div><div class=""><div class=""><font face="Courier" class="">}</font></div></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">struct ValidatingUTF8Decoder: UnicodeDecoder {</font></div><div class=""><font face="Courier" class=""> typealias Encoding = UTF8</font></div><div class=""><font face="Courier" class=""> typealias DecodedScalar = NonValidatingUTF8Decoder.DecodedScalar // newtype would be cool here</font></div><div class=""><font face="Courier" class=""> // Parsing functions</font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">struct String {</font></div><div class=""><font face="Courier" class=""> init<C, Encoding, Decoder>(from: C, encodedAs: Encoding, using: Decoder = Encoding.ValidatingDecoder) </font></div><div class=""><font face="Courier" class=""> where C: Collection, C.Iterator.Element == Encoding.CodeUnit, Decoder.Encoding == Encoding {</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class=""> // transcode to native String encoding using ‘Decoder’ we were given</font></div><div class=""><font face="Courier" class=""> }</font></div><div class=""><font face="Courier" class="">}</font></div></blockquote><div class=""><br class=""></div><div class="">- Karl</div></body></html>