[swift-evolution] Trial balloon: Ensure that String always contains valid Unicode
Dmitri Gribenko
gribozavr at gmail.com
Mon Jan 4 13:59:47 CST 2016
On Mon, Jan 4, 2016 at 9:37 PM, Kevin Ballard via swift-evolution <
swift-evolution at swift.org> wrote:
> I agree in principle that it would be great if String could enforce that
> it's always valid.
>
> But unfortunately, in practice, there's no way to do that without making
> it expensive to bridge from Obj-C. Because, as you've demonstrated, you can
> create NSStrings that contain things that aren't actually valid unicode
> sequences, every single bridge from an NSString to a String would have to
> be checked for validity. Not only that, but it's not clear what the
> behavior would be if an invalid string is found, since these bridges are
> unconditional - would Swift panic? Would it silently replace the invalid
> sequence with U+FFFD? Or something else entirely? But the question doesn't
> really matter, because turning these bridges from O(1) into O(N) would be
> an unacceptable performance penalty anyway.
>
Currently String replaces invalid sequences with U+FFFD lazily during
access, but there are corner cases related to Objective-C bridging that can
still leak invalid Unicode.
Dmitri
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160104/198c6aa4/attachment.html>
More information about the swift-evolution
mailing list