-
Notifications
You must be signed in to change notification settings - Fork 49
Implement Regex.mapOutput #455
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2022 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@available(SwiftStdlib 5.7, *) | ||
extension Regex { | ||
@available(SwiftStdlib 5.7, *) | ||
public func mapOutput<NewOutput>( | ||
_ body: @escaping (Output) -> NewOutput | ||
) -> Regex<NewOutput> { | ||
let transform: (Any) -> NewOutput = { | ||
if let previousTransform = outputTransform { | ||
return body(previousTransform($0)) | ||
} else { | ||
return body($0 as! Output) | ||
} | ||
} | ||
|
||
var regex = Regex<NewOutput>(node: root) | ||
regex.outputTransform = transform | ||
return regex | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@testable import _RegexParser | ||
|
||
import XCTest | ||
@testable import _StringProcessing | ||
|
||
enum Transaction: String { | ||
case credit | ||
case debit | ||
} | ||
|
||
extension RegexTests { | ||
func testMapOutput() throws { | ||
let regex0: Regex<(Substring, Substring)> = try Regex(#"Transaction: (credit|debit)"#) | ||
let string0 = "Transaction: credit" | ||
|
||
let regex1 = regex0.mapOutput { | ||
($0, transaction: Transaction(rawValue: String($1))) | ||
} | ||
|
||
let match0 = try XCTUnwrap(string0.firstMatch(of: regex1)?.output) | ||
XCTAssertTrue(match0 == ("Transaction: credit", .credit)) | ||
|
||
let regex2 = regex1.mapOutput { | ||
$1 | ||
} | ||
|
||
let match1 = try XCTUnwrap(string0.firstMatch(of: regex2)?.output) | ||
XCTAssertEqual(match1, .credit) | ||
|
||
let regex3: Regex<Substring> = try Regex(#"Hello"#) | ||
let string1 = "Hello" | ||
|
||
let regex4 = regex3.mapOutput { | ||
"\($0) world!"[...] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious here — does this cause a problem if you leave off the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously if the output type was just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also add a test of the behavior when dropping an output-mapped regex in a builder block? |
||
} | ||
|
||
let match2 = try XCTUnwrap(string1.firstMatch(of: regex4)?.output) | ||
XCTAssertEqual(match2, "Hello world!") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the tradeoff of storing transform here vs just doing it when making a Match object?