{"id":17031,"date":"2017-01-27T14:28:58","date_gmt":"2017-01-27T19:28:58","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=17031"},"modified":"2020-04-21T14:01:50","modified_gmt":"2020-04-21T18:01:50","slug":"swift-4-string-manifesto","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/01\/27\/swift-4-string-manifesto\/","title":{"rendered":"Swift 4 String Manifesto"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/apple\/swift\/blob\/master\/docs\/StringManifesto.md\">Dave Abrahams and Ben Cohen<\/a> (<a href=\"https:\/\/forums.swift.org\/t\/strings-in-swift-4\/4939\">mailing list<\/a>):<\/p>\n<blockquote cite=\"https:\/\/github.com\/apple\/swift\/blob\/master\/docs\/StringManifesto.md\">\n<p>The first step in improving this [internationalization] situation is to regularize all localized operations as invocations of normal string operations with extra parameters.<\/p>\n<p>[&#8230;]<\/p>\n<p>When assigning a <code>Substring<\/code> to a longer-lived variable (usually a stored\nproperty) explicitly of type <code>String<\/code>, a type conversion will be performed, and\nat this point the substring buffer is copied and the original string&rsquo;s storage\ncan be released.<\/p>\n<p>[&#8230;]<\/p>\n<p>With <code>Substring<\/code> and <code>String<\/code> being distinct types and sharing almost all\ninterface and semantics, and with the highest-performance string processing\nrequiring knowledge of encoding and layout that the currency types can&rsquo;t\nprovide, it becomes important to capture the common &ldquo;string API&rdquo; in a protocol.\nSince Unicode conformance is a key feature of string processing in Swift, we\ncall that protocol <code>Unicode<\/code>[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>Index interchange between <code>String<\/code> and its <code>unicodeScalars<\/code>, <code>codeUnits<\/code>,\nand <code>extendedASCII<\/code> views can be made entirely\nseamless by having them share an index type (semantics of indexing a <code>String<\/code>\nbetween grapheme cluster boundaries are TBD--it can either trap or be forgiving).\nHaving a common index allows easy traversal into the interior of graphemes,\nsomething that is often needed, without making it likely that someone will do it\nby accident.<\/p>\n<p>[&#8230;]<\/p>\n<p>In the long run, we should improve Swift string interpolation to the point where it can participate in most any formatting job. Mostly this centers around fixing the interpolation protocols per the previous item, and supporting localization.<\/p>\n<p>[&#8230;]<\/p>\n<p>This proposal depends on two new features in the Swift language:<\/p>\n<ol>\n<li><p>Generic subscripts, to\nenable unified slicing syntax.<\/p><\/li>\n<li><p>A subtype relationship between\n<code>Substring<\/code> and <code>String<\/code>, enabling framework APIs to traffic solely in\n<code>String<\/code> while still making it possible to avoid copies by handling\n<code>Substring<\/code>s where necessary.<\/p><\/li>\n<\/ol>\n<\/blockquote>\n<p>Regarding this last point, note that <code>String<\/code> is a <code>struct<\/code>.<\/p>\n<p><a href=\"https:\/\/forums.swift.org\/t\/strings-in-swift-4\/4939\/130\">Chris Lattner<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/strings-in-swift-4\/4939\/130\"><p>I agree with Joe that Swift can learn a lot from Perl 6 grammar&rsquo;s and we should take the time to do it right.<\/p>\n<p>[&#8230;]<\/p>\n<p>So why bless regex literals with language support at all?  I see several reasons[&#8230;]<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Dave Abrahams and Ben Cohen (mailing list): The first step in improving this [internationalization] situation is to regularize all localized operations as invocations of normal string operations with extra parameters. [&#8230;] When assigning a Substring to a longer-lived variable (usually a stored property) explicitly of type String, a type conversion will be performed, and at [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2020-04-21T18:01:54Z","apple_news_api_id":"3fcf9b94-16ab-4fed-8c70-c63d3d2839d3","apple_news_api_modified_at":"2020-04-21T18:01:54Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AP8-blBarT-2McMY9PSg50w","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[46,309,252,71,234,901,258],"class_list":["post-17031","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-localization","tag-perl","tag-programming","tag-regex","tag-swift-programming-language","tag-unicode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=17031"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17031\/revisions"}],"predecessor-version":[{"id":28737,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17031\/revisions\/28737"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=17031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=17031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=17031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}