Skip to content
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
12 changes: 11 additions & 1 deletion Sources/_StringProcessing/LiteralPrinter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,17 @@ extension String {

extension UnicodeScalar {
var escapedString: String {
"\\u{" + String(value, radix: 16) + "}"
switch self {
case "\n": return #"\n"#
case "\r": return #"\r"#
case "\t": return #"\t"#
default:
let code = String(value, radix: 16, uppercase: true)
let prefix = code.count <= 4
? #"\u"# + String(repeating: "0", count: 4 - code.count)
: #"\U"# + String(repeating: "0", count: 8 - code.count)
return prefix + code
}
}
}

Expand Down
31 changes: 24 additions & 7 deletions Tests/RegexTests/LiteralPrinterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,35 @@ import XCTest
import _StringProcessing
import RegexBuilder

@available(SwiftStdlib 6.0, *)
fileprivate func _literalTest<T>(
_ regex: Regex<T>,
expected: String?,
file: StaticString = #filePath,
line: UInt = #line
) {
XCTAssertEqual(regex._literalPattern, expected, file: file, line: line)
if let expected {
let remadeRegex = try? Regex(expected)
XCTAssertEqual(expected, remadeRegex?._literalPattern, file: file, line: line)
}
}

@available(SwiftStdlib 6.0, *)
extension RegexTests {
func testPrintableRegex() throws {
let regexString = #"([a-fGH1-9[^\D]]+)?b*cd(e.+)\2\w\S+?"#
let regex = try! Regex(regexString)
let pattern = try XCTUnwrap(regex._literalPattern)
let regex = try Regex(regexString)
// Note: This is true for this particular regex, but not all regexes
XCTAssertEqual(regexString, pattern)
_literalTest(regex, expected: regexString)

let printableRegex = try XCTUnwrap(PrintableRegex(regex))
XCTAssertEqual("\(printableRegex)", pattern)
XCTAssertEqual("\(printableRegex)", regexString)
}

func testUnicodeEscapes() throws {
let regex = #/\r\n\t cafe\u{301} \u{1D11E}/#
_literalTest(regex, expected: #"\r\n\t cafe\u0301 \U0001D11E"#)
}

func testPrintableDSLRegex() throws {
Expand All @@ -39,8 +57,7 @@ extension RegexTests {
}.dotMatchesNewlines()
Optionally("c")
}.ignoresCase()
let pattern = try XCTUnwrap(regex._literalPattern)
XCTAssertEqual("(?i:(?:aaa)+?(?s:(?:bbb)*|d+|e{3,})c?)", pattern)
_literalTest(regex, expected: "(?i:(?:aaa)+?(?s:(?:bbb)*|d+|e{3,})c?)")

let nonPrintableRegex = Regex {
OneOrMore("a")
Expand All @@ -49,7 +66,7 @@ extension RegexTests {
} transform: { Int($0)! }
Optionally("b")
}
XCTAssertNil(nonPrintableRegex._literalPattern)
_literalTest(nonPrintableRegex, expected: nil)
}
}

Expand Down