Skip to content

Commit 937891b

Browse files
committed
Normalize line ending to \n in StringLiteralExprSyntax.representedLiteralValue
This matches the specification of multi-line string literal handling: From https://docs.swift.org/swift-book/documentation/the-swift-programming-language/lexicalstructure/#String-Literals: > Line breaks in a multiline string literal are normalized to use the line feed character. Even if your source file has a mix of carriage returns and line feeds, all of the line breaks in the string will be the same. From https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170417/035923.html: > The quoted string should normalize newlines to \n in the value of the literal, regardless of whether the source file uses \n (Unix), \r\n (Windows), or \r (classic Mac) line endings. Likewise, when the compiler strips the initial and final newline from the literal value, it will strip one of any of the \n, \r\n, or \r line-ending sequences from both ends of the literal.
1 parent e3ea968 commit 937891b

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

Sources/SwiftParser/StringLiteralRepresentedLiteralValue.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ extension StringSegmentSyntax {
105105
)
106106

107107
switch lex {
108+
case .success(UnicodeScalar("\r\n")), .success(Unicode.Scalar("\r")):
109+
// Line endings in multi-line string literals are normalized to line feeds even if the source file has a
110+
// different encoding few new lines.
111+
output.append("\n")
108112
case .success(let scalar),
109113
.validatedEscapeSequence(let scalar):
110114
output.append(Character(scalar))

0 commit comments

Comments
 (0)