<commit_before># Multi-Line String Literals

* Proposal: [SE-0168](0168-multi-line-string-literals.md)
* Authors: [<NAME>](https://github.com/johnno1962), [<NAME>](https://github.com/brentdax), [<NAME>](https://github.com/TheArtOfEngineering)
* Review Manager: [<NAME>](https://github.com/jckarter)
* Status: **Active review (April 6...12, 2017)**
* Bug: [SR-170](https://bugs.swift.org/browse/SR-170)

## Introduction

This proposal introduces multi-line string literals to Swift source code.
This has been discussed a few times on swift-evolution most recently
putting forward a number of different syntaxes that could achieve this goal
each of which has their own use case and constituency for discussion.

[Swift-evolution thread](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001565.html)

## Motivation

Multi-line string literals are a common programming language feature that is, to date, still missing in
Swift. Whether generating XML/JSON messages or building usage messages in Swift scripts, providing string
literals that extend over multiple lines offers a simple way to represent text without having to manually
break lines using string concatenation. Concatenation is ungainly and may result in slow compilation; 
escape sequences like `\n` make it harder to picture how the string constructed by the literal will look.

If the goal is to make multi-line string literals more readable, we can't ignore indentation. Any 
feature we design should allow the contents of a string literal to be indented to match the surrounding 
code. Languages which include a multiline string literal syntax, but don't allow them to be indented, 
often end up adding an indentation feature later. We think we should tackle this problem from the outset.

A related problem is that of string literals which contain double-quote characters. Because `"` is the 
delimiter for string literals, double-quote characters *within* a string literal have to be escaped with 
a `\`. This can make even relatively short string literals difficult to read, particularly if they mix 
quotation marks, backslashes, newlines, and/or interpolated expressions in close proximity. Thus, the 
right design for a multi-line string literal feature might also improve the readability of some short 
string literals.

## Proposed solution

In brief, we propose adding a new *tripled string literal* syntax. This behaves the same as the ordinary 
string literal syntax, including supporting all existing backslash escapes and interpolations, except 
that it:

1. Uses `"""` as its delimiter.
2. Can contain newlines, tabs, and `"` and `""` character sequences without using any escapes.
3. Supports backslashing a newline, meaning that the newline is not present in the string's contents.
4. Strips whitespace from each line if the closing delimiter is placed on a new line and indented.
5. Optionally removes a leading newline immediately after the opening delimiter.

If accepted, this proposal will permit users to write code using long, complicated, formatted string 
literals without compromising its readability:

```swift
var xml = """
    <?xml version="1.0"?>
    <catalog>
    """

for (id, author, title, genre, price) in bookTuples {
    xml += """
            <book id="bk\(id)">
                <author>\(author)</author>
                <title>\(title)</title>
                <genre>\(genre)</genre>
                <price>\(price)</price>
            </book><commit_msg>indenting<commit_after># Multi-Line String Literals

* Proposal: [SE-0168](0168-multi-line-string-literals.md)
* Authors: [<NAME>](https://github.com/johnno1962), [<NAME>](https://github.com/brentdax), [<NAME>](https://github.com/TheArtOfEngineering)
* Review Manager: [<NAME>](https://github.com/jckarter)
* Status: **Active review (April 6...12, 2017)**
* Bug: [SR-170](https://bugs.swift.org/browse/SR-170)

## Introduction

This proposal introduces multi-line string literals to Swift source code.
This has been discussed a few times on swift-evolution most recently
putting forward a number of different syntaxes that could achieve this goal
each of which has their own use case and constituency for discussion.

[Swift-evolution thread](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001565.html)

## Motivation

Multi-line string literals are a common programming language feature that is, to date, still missing in
Swift. Whether generating XML/JSON messages or building usage messages in Swift scripts, providing string
literals that extend over multiple lines offers a simple way to represent text without having to manually
break lines using string concatenation. Concatenation is ungainly and may result in slow compilation; 
escape sequences like `\n` make it harder to picture how the string constructed by the literal will look.

If the goal is to make multi-line string literals more readable, we can't ignore indentation. Any 
feature we design should allow the contents of a string literal to be indented to match the surrounding 
code. Languages which include a multiline string literal syntax, but don't allow them to be indented, 
often end up adding an indentation feature later. We think we should tackle this problem from the outset.

A related problem is that of string literals which contain double-quote characters. Because `"` is the 
delimiter for string literals, double-quote characters *within* a string literal have to be escaped with 
a `\`. This can make even relatively short string literals difficult to read, particularly if they mix 
quotation marks, backslashes, newlines, and/or interpolated expressions in close proximity. Thus, the 
right design for a multi-line string literal feature might also improve the readability of some short 
string literals.

## Proposed solution

In brief, we propose adding a new *tripled string literal* syntax. This behaves the same as the ordinary 
string literal syntax, including supporting all existing backslash escapes and interpolations, except 
that it:

1. Uses `"""` as its delimiter.
2. Can contain newlines, tabs, and `"` and `""` character sequences without using any escapes.
3. Supports backslashing a newline, meaning that the newline is not present in the string's contents.
4. If the closing delimiter is preceded by a newline and a sequence of whitespace characters, that whitespace is treated as indentation to remove from preceding lines.
5. Optionally removes a leading newline immediately after the opening delimiter.

If accepted, this proposal will permit users to write code using long, complicated, formatted string 
literals without compromising its readability:

```swift
var xml = """
    <?xml version="1.0"?>
    <catalog>
    """

for (id, author, title, genre, price) in bookTuples {
    xml += """
            <book id="bk\(id)">
                <author>\(author)</author>
                <title>\(title)</title>
                <genre>\(genre)</genre>
                <price>\(price)</price>
            </book>
{'fraction_non_alphanumeric': 0.07955030596271524, 'fraction_numerical': 0.013092358047530952, 'mean_word_length': 4.175257731958763, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 2, '>': 35, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '243276', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1767, 'n_words': 872}