• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Home
  • About Us
  • Contact Us

iHash

News and How to's

  • OTTERBOX DEFENDER SERIES SCREENLESS EDITION Case for iPhone 13 Pro (ONLY) – HUNTER GREEN for $29

    OTTERBOX DEFENDER SERIES SCREENLESS EDITION Case for iPhone 13 Pro (ONLY) – HUNTER GREEN for $29
  • DUBLIN 1L Stainless Steel French Press for $63

    DUBLIN 1L Stainless Steel French Press for $63
  • Prodigy Afterschool Masterclasses for Kids for $99

    Prodigy Afterschool Masterclasses for Kids for $99
  • 10.1" WiFi Digital Photo Frame with Photo/Video Sharing for $149

    10.1" WiFi Digital Photo Frame with Photo/Video Sharing for $149
  • 8" WiFi Cloud Photo Frame for $112

    8" WiFi Cloud Photo Frame for $112
  • News
    • Rumor
    • Design
    • Concept
    • WWDC
    • Security
    • BigData
  • Apps
    • Free Apps
    • OS X
    • iOS
    • iTunes
      • Music
      • Movie
      • Books
  • How to
    • OS X
      • OS X Mavericks
      • OS X Yosemite
      • Where Download OS X 10.9 Mavericks
    • iOS
      • iOS 7
      • iOS 8
      • iPhone Firmware
      • iPad Firmware
      • iPod touch
      • AppleTV Firmware
      • Where Download iOS 7 Beta
      • Jailbreak News
      • iOS 8 Beta/GM Download Links (mega links) and How to Upgrade
      • iPhone Recovery Mode
      • iPhone DFU Mode
      • How to Upgrade iOS 6 to iOS 7
      • How To Downgrade From iOS 7 Beta to iOS 6
    • Other
      • Disable Apple Remote Control
      • Pair Apple Remote Control
      • Unpair Apple Remote Control
  • Special Offers
  • Contact us

Behind the Proposal — SE-0200 Enhancing String Literals Delimiters to Support Raw Text

Apr 4, 2022 by iHash Leave a Comment

February 20, 2019

The development, refinement, and deployment of SE-0200 Enhancing String Literals Delimiters to Support Raw Text was a long and surprising journey. It ended with a uniquely Swift take on “raw strings” that focused on adding custom delimiters to string literals and escape sequences.

This post discusses what raw strings are, how Swift designed its take on this technology, and how you can use this new Swift 5 feature in your code.

Table of Contents

  • Escape Sequences
  • Raw Strings
  • Multi-Line Swift Strings
  • Swift Raw Strings: Take One
  • Customizable Delimiters
  • Adopting SE-0200 Strings In Your Code

Escape Sequences

Escape sequences are backslash-prepended combinations like \\, \", and \u{n} that incorporate characters that would otherwise be hard to express inside a normal string literal. Swift escape sequences include:

  • The special characters \0 (null character), \\ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quotation mark) and \' (single quotation mark)
  • Arbitrary Unicode scalars, written as \u{n}, where n is a 1–8 digit hexadecimal number with a value equal to a valid Unicode code point
  • Interpolated expressions, introduced by \( and terminated by ). Swift’s interpolation feature offers a powerful and compiler-checked way to add content to strings. It is one of the language’s highlights.

For example, the string literal "hello\n\n\tworld" consists of three lines, with “hello” on the first and “world” on the third. “world” is indented by a single tab:

A raw string, in contrast, ignores escape sequences and treats all content as literal characters. In a raw string, \n represents the backslash character followed by the letter n rather than a line feed. This feature is used in applications that produce code output, that work with regular expressions, that use in-app source code (for example, when interactively teaching a language), and for pre-escaped domain-specific content like JSON and XML.

Raw Strings

Raw strings are used in many languages including C#, Perl, Rust, Python, Ruby, and Scala. A raw string does not interpret escape sequences. Its content continues until it reaches the string’s end delimiter, which varies by language, as in the following table:

Syntax Language(s)
'Hello, world!' Bourne shell, Perl, PHP, Ruby, Windows PowerShell
q(Hello, world!) Perl (alternate)
%q(Hello, world!) Ruby (alternate)
@"Hello, world!" C#, F#
R"(Hello, world!)" C++11
r"Hello, world!" D, Python
r#"Hello, world!"# Rust
"""hello \' world""" and raw"Hello, world!" Scala
`Hello, world!` D, Go, `…`
``...`` Java, any number of `

Most languages adopt a prefix (like q, R, or r) to indicate raw content. Rust and Java go beyond this to allow customizable delimiters. This feature allows variations of the delimiter to be included within the string, allowing more expressive raw string content.

Multi-Line Swift Strings

SE-0168 Multi-Line String Literals not only introduced a way to create string literals with more than one line and no new-line escapes, it also provided a hint of the direction the Swift language would take in terms of custom delimiters. Since multi-line strings used three quotes """ to start and end literals, they allowed individual quote marks and new lines without escape sequences. Under the new system, this literal:

"\"Either it brings tears to their eyes, or else -\"\n\n\"Or else what?\" said Alice, for the Knight had made a sudden pause.\n\n\"Or else it doesn't, you know.\""

became this:

"""
    "Either it brings tears to their eyes, or else -"
    
    "Or else what?" said Alice, for the Knight had made a sudden pause.

    "Or else it doesn't, you know."
    """

Quote and newline backslashes evaporate in the new syntax. The resulting string literal is clear, readable, and inspectable. In introducing the new delimiter and multi-line support, new-line and quote marks can be used without escapes, taking the first steps forward towards better literals.

Multi-line literals did not lose any of Swift’s string power. They support escapes, including interpolation, unicode character insertion, and so forth. At the same time, the feature set the standard for what Swift “raw” strings should look like.

Swift Raw Strings: Take One

SE-0200 first entered review in March 2018. Its initial design added a single r prefix to single and multi-line strings. The community disliked the design (“The proposed r"..." syntax didn’t fit well with the rest of the language”) and felt it wasn’t expansive enough to support enough use-cases. The proposal was returned for revision in April 2018. It was time to search for a better design, better use-cases, and a more Swift-aligned expression.

Revisiting design involved an extensive review of raw strings in other languages, eventually focussing on Rust. Rust not only supports raw strings, it uses customizable delimiters. You can create raw strings with r#""#, r##""##, r###""###, and so forth. You choose the number of pound signs to pad each side of the string literal. In the unlikely circumstance you needed to include "# in a string, which would normally terminate a basic raw string, these custom delimiters ensure you can add a second pound sign, allowing you to adjust the way the string ends.

Yes, it is extremely rare you ever need more than one pound sign but Rust’s design takes that rarity into account. It creates an expansible and customizable system that offers coverage of even the most outlandish edge cases. That strength is impressive and core to Swift’s eventual design. In its revision, SE-0200 dropped the r (which stands for “raw”) while adopting the adaptable Rust-style pound signs on each side of the literal. As in Rust, each Swift string literal must use the same number of pounds before and after, whether working with single- or multi-line strings.

At that point, inspiration struck as the SE-0200 team realized that custom delimiters offered more power than plain raw strings.

Customizable Delimiters

When using the updated raw strings design, time and again the team regretted the loss of string interpolation. By definition, raw strings do not use escape sequences. Interpolation depends on them. It was SE-0200 co-author Brent Royal-Gordon who had the flash of insight that we could incorporate the Rust-inspired syntax while retaining access to escape sequences.

Instead of creating raw strings, SE-0200 introduced something similar: a blend of the alternate delimiters Swift first encountered in multi-line strings and the customizable delimiters from Rust. By extending that customization to escape sequences, SE-0200’s design inherited all the power of raw strings and the convenience of Swift interpolation.

SE-0200 adds custom delimiters at the start and end of each string literal and, in lockstep, customizes the escape sequence delimiter from a simple backslash to one decorated with pound-signs. This design matches escape sequences to the number of pound-signs for the string literal. For a "" string, the escape token is \. For #""#, it is \#, and ##""## it is \##, and so forth.

By adding escape sequences – this modification supports all of them, not just interpolation – Swift’s #-annotated strings were no longer “raw”. They support the same features you find in raw strings, they mostly act like raw strings, however the design incorporates escaping, which means the literals are not raw. If you feel fanciful, you can call them “medium rare” strings.

Any time you include what would otherwise be recognized as an escape sequence, you can extend the number of delimiter pound-signs until the contents are no longer interpreted. It is rare to need this feature but when used, just one or two pound signs should both support interpolation in some parts of your string and disallow it in others:

"\(thisInterpolates)"
#"\(thisDoesntInterpolate) \#(thisInterpolates)"#
##"\(thisDoesntInterpolate) \#(thisDoesntInterpolate) \##(thisInterpolates)"##

"\n" // new line
#"\n"# // backslash plus n
#"\#n"# // new line

Adopting SE-0200 Strings In Your Code

In Swift 5, each of the following literals declares the string “Hello”, even though they use a variety of single and multi-line styles:

let u = "Hello" // No pounds
let v = #"Hello"# // One pound
let w = ####"Hello"#### // Many pounds
let x = "\("Hello")" // Interpolation
let y = #"\#("Hello")"# // Interpolation with pound
let z = """ // Multiline
    Hello
    """
let a = #""" // Multiline with pound
    Hello
    """#

The rules are as follows:

  • Match the number of pound-signs before and after a string literal, from zero to however many. “Zero” or “one” are almost always the right answer for “however many”.
  • When using pound-signs, you change the escape sequence from a single backslash to a backslash infixed with the same number of pound signs. A ##"Hello"## string uses a \## escape sequence.
  • Anything that doesn’t match the closing delimiter is part of the string. To add """ to a multiline string without escaping, change the delimiter by adding a pound-sign.
  • Use the fewest pound signs required for the results you need. Zero is best. One is fine. Two or more should be very, very rare.

With SE-0200, anyone writing code generation apps like PaintCode or Kite Compositor, writing network code with escaped-JSON, or including backslash-heavy ASCII clip art, can paste and go. Add pound-signs as needed, without sacrificing the convenience of string interpolation or escape sequences.

These delimiters ensure your code remains free of escape clutter. The results are cleaner. They’re easier to read and to cut/paste into your codebase. You’ll be able to test, reconfigure, and adapt raw content without the hurdles of escaping and unescaping that otherwise limit your development.

Read more about Swift’s new custom string delimiters in the SE-0200 proposal. It includes further details, many examples, and explores alternate designs that were considered and rejected.

Please feel free to post questions about this post on the associated thread on the Swift forums.

Swift 5.1 Release Process

UTF-8 String

Behind the Proposal — SE-0200 Enhancing String Literals Delimiters to Support Raw Text

Share this:

  • Facebook
  • Twitter
  • Pinterest
  • LinkedIn

Filed Under: News Tagged With: Apple

Special Offers

  • OTTERBOX DEFENDER SERIES SCREENLESS EDITION Case for iPhone 13 Pro (ONLY) – HUNTER GREEN for $29

    OTTERBOX DEFENDER SERIES SCREENLESS EDITION Case for iPhone 13 Pro (ONLY) – HUNTER GREEN for $29
  • DUBLIN 1L Stainless Steel French Press for $63

    DUBLIN 1L Stainless Steel French Press for $63
  • Prodigy Afterschool Masterclasses for Kids for $99

    Prodigy Afterschool Masterclasses for Kids for $99
  • 10.1" WiFi Digital Photo Frame with Photo/Video Sharing for $149

    10.1" WiFi Digital Photo Frame with Photo/Video Sharing for $149
  • 8" WiFi Cloud Photo Frame for $112

    8" WiFi Cloud Photo Frame for $112

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

E-mail Newsletter

  • Facebook
  • GitHub
  • Instagram
  • Pinterest
  • Twitter
  • YouTube

More to See

New ‘FabricScape’ Bug in Microsoft Azure Service Fabric Impacts Linux Workloads

Jun 29, 2022 By iHash

Cloud Security Resources and Guidance

Cloud Security Resources and Guidance

Jun 29, 2022 By iHash

Tags

* Apple Cisco computer security cyber attacks cyber crime cyber news Cyber Security cybersecurity cyber security news cyber security news today cyber security updates cyber threats cyber updates data breach data breaches google hacker hacker news Hackers hacking hacking news how to hack incident response information security iOS iOS 7 iOS 8 iPhone iPhone 6 Malware microsoft network security Privacy ransomware malware risk management security security breaches security vulnerabilities software vulnerability the hacker news Threat update video web applications

Latest

MLPerf Results Highlight More Capable ML Training

Today, MLCommons®, an open engineering consortium, released new results from MLPerf™ Training v2.0, which measures the performance of training machine learning models. Training models empowers researchers to unlock new capabilities faster such as diagnosing tumors, automatic speech recognition or improving movie recommendations. The latest MLPerf Training results demonstrate broad industry participation and up to 1.8X […]

OTTERBOX DEFENDER SERIES SCREENLESS EDITION Case for iPhone 13 Pro (ONLY) – HUNTER GREEN for $29

Expires June 28, 2122 21:59 PST Buy now and get 0% off PRODUCT SPECS Compatible with iPhone 13 Pro (ONLY) Multi-layer defense from the solid inner shell and resilient outer slipcover with port covers that block dirt, dust and lint from getting into jacks and ports Tested to survive 4X as many drops as military […]

DUBLIN 1L Stainless Steel French Press for $63

Expires June 29, 2122 23:59 PST Buy now and get 10% off KEY FEATURES This elegant, durable, and stylish coffee press is the ideal way to enjoy coffee at home. It has a one-liter (1000 ml/34 fl. oz) capacity that makes it perfect for making two to three large cups of coffee. The double-wall insulated […]

What Is Data Reliability Engineering?

Data Reliability Engineering (DRE) is the work done to keep data pipelines delivering fresh and high-quality input data to the users and applications that depend on them. The goal of DRE is to allow for iteration on data infrastructure, the logical data model, etc. as quickly as possible, while—and this is the key part! —still […]

Prodigy Afterschool Masterclasses for Kids for $99

Expires June 28, 2122 23:59 PST Buy now and get 85% off KEY FEATURES Unlock Your Child’s Potential For Success! No dream is too big when you have the tools to achieve it. Whether your child dreams of saving lives as a doctor or inspiring people through the arts, Prodigy will give them the tools […]

Cybersecurity Experts Warn of Emerging Threat of “Black Basta” Ransomware

The Black Basta ransomware-as-a-service (RaaS) syndicate has amassed nearly 50 victims in the U.S., Canada, the U.K., Australia, and New Zealand within two months of its emergence in the wild, making it a prominent threat in a short window. “Black Basta has been observed targeting a range of industries, including manufacturing, construction, transportation, telcos, pharmaceuticals, […]

Jailbreak

Pangu Releases Updated Jailbreak of iOS 9 Pangu9 v1.2.0

Pangu has updated its jailbreak utility for iOS 9.0 to 9.0.2 with a fix for the manage storage bug and the latest version of Cydia. Change log V1.2.0 (2015-10-27) 1. Bundle latest Cydia with new Patcyh which fixed failure to open url scheme in MobileSafari 2. Fixed the bug that “preferences -> Storage&iCloud Usage -> […]

Apple Blocks Pangu Jailbreak Exploits With Release of iOS 9.1

Apple has blocked exploits used by the Pangu Jailbreak with the release of iOS 9.1. Pangu was able to jailbreak iOS 9.0 to 9.0.2; however, in Apple’s document on the security content of iOS 9.1, PanguTeam is credited with discovering two vulnerabilities that have been patched.

Pangu Releases Updated Jailbreak of iOS 9 Pangu9 v1.1.0

  Pangu has released an update to its jailbreak utility for iOS 9 that improves its reliability and success rate.   Change log V1.1.0 (2015-10-21) 1. Improve the success rate and reliability of jailbreak program for 64bit devices 2. Optimize backup process and improve jailbreak speed, and fix an issue that leads to fail to […]

Activator 1.9.6 Released With Support for iOS 9, 3D Touch

  Ryan Petrich has released Activator 1.9.6, an update to the centralized gesture, button, and shortcut manager, that brings support for iOS 9 and 3D Touch.

Copyright iHash.eu © 2022
We use cookies on this website. By using this site, you agree that we may store and access cookies on your device. Accept Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT