-
Notifications
You must be signed in to change notification settings - Fork 50
Obtain match output elements without materializing the output. #469
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 |
---|---|---|
|
@@ -28,6 +28,10 @@ extension Regex { | |
|
||
@available(SwiftStdlib 5.7, *) | ||
extension Regex.Match { | ||
var input: String { | ||
anyRegexOutput.input | ||
} | ||
|
||
/// The output produced from the match operation. | ||
public var output: Output { | ||
if Output.self == AnyRegexOutput.self { | ||
|
@@ -37,33 +41,48 @@ extension Regex.Match { | |
) | ||
|
||
let output = AnyRegexOutput( | ||
input: anyRegexOutput.input, | ||
input: input, | ||
elements: [wholeMatchCapture] + anyRegexOutput._elements | ||
) | ||
|
||
return output as! Output | ||
} else if Output.self == Substring.self { | ||
// FIXME: Plumb whole match (`.0`) through the matching engine. | ||
return anyRegexOutput.input[range] as! Output | ||
} else if anyRegexOutput.isEmpty, value != nil { | ||
return input[range] as! Output | ||
} else if anyRegexOutput.isEmpty, let value { | ||
// FIXME: This is a workaround for whole-match values not | ||
// being modeled as part of captures. We might want to | ||
// switch to a model where results are alongside captures | ||
return value! as! Output | ||
return value as! Output | ||
} else { | ||
guard value == nil else { | ||
fatalError("FIXME: what would this mean?") | ||
} | ||
let typeErasedMatch = anyRegexOutput.existentialOutput( | ||
from: anyRegexOutput.input[range] | ||
from: input[range] | ||
) | ||
return typeErasedMatch as! Output | ||
} | ||
} | ||
|
||
var wholeMatchType: Any.Type { | ||
value.map { type(of: $0) } ?? Substring.self | ||
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. What if it's optional? Also, are we tracking nested optionals correctly here (I.e. do they get turned into values or are they counted)? 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. When there's no dedicated value for whole match, it can only be a |
||
} | ||
|
||
/// Accesses a capture by its name or number. | ||
public subscript<T>(dynamicMember keyPath: KeyPath<Output, T>) -> T { | ||
output[keyPath: keyPath] | ||
// Note: We should be able to get the element offset from the key path | ||
// itself even at compile time. We need a better way of doing this. | ||
guard let outputTupleOffset = MemoryLayout.tupleElementIndex( | ||
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. When we store the whole match inside the capture list, we will be able to precompute these offsets for all elements in a |
||
of: keyPath, elementTypes: [wholeMatchType] + anyRegexOutput.map(\.type) | ||
) else { | ||
return output[keyPath: keyPath] | ||
} | ||
if outputTupleOffset == 0 { | ||
return value.map { $0 as! T } ?? (input[range] as! T) | ||
} else { | ||
return anyRegexOutput[outputTupleOffset - 1].value as! T | ||
} | ||
} | ||
|
||
/// Accesses a capture using the `.0` syntax, even when the match isn't a tuple. | ||
|
@@ -83,7 +102,7 @@ extension Regex.Match { | |
} | ||
|
||
return element.existentialOutputComponent( | ||
from: anyRegexOutput.input[...] | ||
from: input[...] | ||
) as! Capture | ||
} | ||
} | ||
|
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.
@lorentey just reminded me of this... 😭
@Azoy, do you have the radar number for this issue I can put in the comment?
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.
rdar://63819465