Swift should provide raw string literals and multiline string literals

Originator:kristopherdjohnson
Number:rdar://17970377 Date Originated:09-Aug-2014 07:18 PM
Status:Duplicate/12574718 Resolved:
Product:Developer Tools Product Version:Xcode 6 beta 5 (6A279r)
Classification:Enhancement Reproducible:Not applicable
 
Summary:
Other programming languages provide a "raw" string literal that can be used to define a string containing "special characters" without the need to escape each one.  This is useful when using regular expressions or other inline code that contains backslashes or double-quote characters.

Other programming languages also allow string literals to span multiple lines. Again, this is helpful when using regular expressions, template substitution, and other programming techniques that require long strings with embedded newlines.

Currently in Swift, one must escape all special characters, and defining a multi-line string requires quoting each line and using + to concatenate them. Swift should provide raw string literals and multiline string literals.

Steps to Reproduce:
Try to define a regular expression in Swift using code like this copied from source in another programming language:

let myRegexPattern = @"
^[ ]{{0,{0}}}\[([^\[\]]+)\]:  # id = $1
  [ ]*
  \n?                   # maybe *one* newline
  [ ]*
<?(\S+?)>?              # url = $2
  [ ]*
  \n?                   # maybe one newline
  [ ]*
(?:
    (?<=\s)             # lookbehind for whitespace
    [""(]
    (.+?)               # title = $3
    ["")]
    [ ]*
)?                      # title is optional
(?:\n+|\Z)
"


Expected Results:
Should be easy

Actual Results:
Compiler errors. One has to rewrite it like this (and I'm not sure I escaped everything properly):

let myRegexPattern = "
"^[ ]{{0,{0}}}\[([^\\[\\]]+)\\]:  # id = $1 \n" +
"  [ ]* \n" +
"  \\n?                   # maybe *one* newline \n" +
"  [ ]* \n" +
"<?(\S+?)>?              # url = $2 \n" +
"  [ ]* \n" +
"  \\n?                   # maybe one newline \n" +
"  [ ]* \\n" +
"(?: \n" +
"    (?<=\\s)             # lookbehind for whitespace \n" +
"    [\"(] \n" +
"    (.+?)               # title = $3 \n" +
"    [\")] \n" +
"    [ ]* \n" +
")?                      # title is optional \n" +
"(?:\\n+|\\Z) \n"

Version:
Xcode 6.0 (6A279r)

Notes:


Configuration:
OS X 10.9

Attachments:

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!