{"id":33800,"date":"2021-10-01T17:00:51","date_gmt":"2021-10-01T21:00:51","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=33800"},"modified":"2021-10-15T15:56:26","modified_gmt":"2021-10-15T19:56:26","slug":"swift-declarative-string-processing-pitch","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/10\/01\/swift-declarative-string-processing-pitch\/","title":{"rendered":"Swift Declarative String Processing Pitch"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/declarative-string-processing-overview\/52459\/1\">Michael Ilseman<\/a> (<a href=\"https:\/\/twitter.com\/Ilseman\/status\/1443353310786822145\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/declarative-string-processing-overview\/52459\/1\"><p>String processing is hard and the current affordances provided by the Swift Standard Library are underpowered. We propose adding two new <em>declarative<\/em> string processing APIs&mdash;a familiar <code>Regex<\/code> literal and a more powerful <code>Pattern<\/code> result builder&mdash;to help make Swift string processing fast and easy.<\/p><p>[&#8230;]<\/p><p>We propose adding a new regular expression literal, with strongly typed captures, to Swift.<\/p><p>[&#8230;]<\/p><p>Sophisticated features like inline capture group processing feel right at home with the result builder syntax <em>because it&rsquo;s all just regular Swift code<\/em>&mdash;it isn&rsquo;t nearly as natural to try to force this kind of functionality into the regex literal.<\/p><p>[&#8230;]<\/p><p>We intended to extend and add generic <a href=\"https:\/\/forums.swift.org\/t\/prototype-protocol-powered-generic-trimming-searching-splitting\/29415\">consumer and searcher<\/a> algorithms to the standard library for operating over collections using patterns or regexes.<\/p><p>[&#8230;]<\/p><p>The Swift operator <code>~=<\/code> allows libraries to extend syntactic pattern matching by returning whether matching succeeded or not. An <a href=\"https:\/\/gist.github.com\/milseman\/bb39ef7f170641ae52c13600a512782f#pattern-matching-through-conformance-to-pattern\">enhancement to this<\/a> would allow libraries to produce a result as part of a <em>destructuring<\/em> pattern match, allowing patterns and regexes to be used inside <code>case<\/code> syntax and directly bind their captures to variables.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/github.com\/apple\/swift-experimental-string-processing\/blob\/main\/Documentation\/BigPicture.md\">Michael Ilseman<\/a>:<\/p>\n<blockquote cite=\"https:\/\/github.com\/apple\/swift-experimental-string-processing\/blob\/main\/Documentation\/BigPicture.md\"><p>I&rsquo;ve been finding it helpful to think of our long-term goal as making Swift awesome at string processing, data processing, and &ldquo;event processing&rdquo; (working title, suggestions welcome). These are not rigid or clear-cut distinct domains (they actually blend together in extremity) so much as they are 3 interesting &ldquo;regions&rdquo; in this design space. Thinking about these regions helps clarify what tasks we&rsquo;re enabling and helps push us towards more general solutions.<\/p><p>Each of these regions share technical fundamentals, but present novel performance and API design challenges. I hope that keeping the big picture in mind will help guide the design process towards pragmatic trade-offs and robust solutions.<\/p><p>[&#8230;]<\/p><p>We want to be able to compose, layer, and even interweave different kinds of processing together. And, we want these areas to be library-extensible, so that libraries can provide custom behavior through custom protocol conformances.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/01\/27\/swift-4-string-manifesto\/\">Swift 4 String Manifesto<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/12\/19\/nsregularexpression-and-swift\/\">NSRegularExpression and Swift<\/a><\/li>\n<\/ul>\n\n<p id=\"swift-declarative-string-processing-pitch-update-2021-10-15\">Update (2021-10-15): <a href=\"https:\/\/forums.swift.org\/t\/pitch-regular-expression-literals\/52820\">Hamish Knight<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/pitch-regular-expression-literals\/52820\">\n<p>We propose to introduce a first-class regular expression literal into the language that can take advantage of library support to offer extensible, powerful, and familiar textual pattern matching.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Michael Ilseman (tweet): String processing is hard and the current affordances provided by the Swift Standard Library are underpowered. We propose adding two new declarative string processing APIs&mdash;a familiar Regex literal and a more powerful Pattern result builder&mdash;to help make Swift string processing fast and easy.[&#8230;]We propose adding a new regular expression literal, with strongly [&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":"2021-10-01T21:00:56Z","apple_news_api_id":"35ed92db-4e23-47a3-84d0-0d50d860140f","apple_news_api_modified_at":"2021-10-15T19:56:30Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/ANe2S204jR6OE0A1Q2GAUDw","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,71,234,901],"class_list":["post-33800","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-regex","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33800","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=33800"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33800\/revisions"}],"predecessor-version":[{"id":33924,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33800\/revisions\/33924"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=33800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=33800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=33800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}