Skip to content

Add convenience initializer for raw string literals #579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,58 @@
//===----------------------------------------------------------------------===//

import SwiftSyntax
import Foundation

/// A regular expression matching sequences of `#`s with an adjacent quote or
/// interpolation. Used to determine the number of `#`s for a raw string literal.
private let rawStringPotentialEscapesPattern = try! NSRegularExpression(
pattern: [
#""(#+)"#, // Match potential opening delimiters
#"(#+)""#, // Match potential closing delimiters
#"\\(#+)\("#, // Match potential interpolations
].joined(separator: "|")
)

extension StringLiteralExpr {
public init(_ value: String, openQuote: TokenSyntax = .stringQuote, closeQuote: TokenSyntax = .stringQuote) {
/// Creates a string literal, optionally specifying quotes and delimiters.
public init(
openDelimiter: TokenSyntax? = nil,
openQuote: TokenSyntax = .stringQuote,
_ value: String,
closeQuote: TokenSyntax = .stringQuote,
closeDelimiter: TokenSyntax? = nil
) {
let content = TokenSyntax.stringSegment(value)
let segment = StringSegment(content: content)
let segments = StringLiteralSegments([segment])

self.init(openQuote: openQuote,
segments: segments,
closeQuote: closeQuote)
self.init(
openDelimiter: openDelimiter,
openQuote: openQuote,
segments: segments,
closeQuote: closeQuote,
closeDelimiter: closeDelimiter
)
}

/// Creates a raw string literal. Automatically determines
/// the number of `#`s needed to express the string as-is without any escapes.
public init(raw value: String) {
// Match potential escapes in the string
let matches = rawStringPotentialEscapesPattern.matches(in: value, range: NSRange(value.startIndex..., in: value))

// Find longest sequence of `#`s by taking the maximum length over all captures
let maxPoundCount = matches
.compactMap { match in (1..<match.numberOfRanges).map { match.range(at: $0).length }.max() }
.max() ?? 0

// Use a delimiter that is exactly one longer
let delimiter = TokenSyntax.rawStringDelimiter(String(repeating: "#", count: 1 + maxPoundCount))

self.init(
openDelimiter: delimiter,
value,
closeDelimiter: delimiter
)
}
}
6 changes: 6 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/StringLiteralTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ final class StringLiteralTests: XCTestCase {
#line: (StringLiteralExpr("asdf"), #"␣"asdf""#),
#line: ("", #"␣"""#),
#line: ("asdf", #"␣"asdf""#),
#line: (StringLiteralExpr(raw: "abc"), "␣#\"abc\"#"),
#line: (StringLiteralExpr(raw: #""quoted""#), ##"␣#""quoted""#"##),
#line: (StringLiteralExpr(raw: ##"#"rawquoted"#"##), ###"␣##"#"rawquoted"#"##"###),
#line: (StringLiteralExpr(raw: ####"###"unbalanced"####), #####"␣####"###"unbalanced"####"#####),
#line: (StringLiteralExpr(raw: ###"some "# string ##""###), ####"␣###"some "# string ##""###"####),
#line: (StringLiteralExpr(raw: ###"\##(abc) \(def)"###), ####"␣###"\##(abc) \(def)"###"####),
]

for (line, testCase) in testCases {
Expand Down