Skip to content

Provide literal pattern for convertible regexes #670

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 4 commits into from
Dec 13, 2023
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
4 changes: 1 addition & 3 deletions Sources/_RegexParser/Regex/Parse/LexicalAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2091,9 +2091,7 @@ extension Parser {
// multiple scalars. These may be confusable for metacharacters, e.g
// `[\u{301}]` wouldn't be interpreted as a custom character class due
// to the combining accent (assuming it is literal, not `\u{...}`).
let scalars = char.unicodeScalars
if scalars.count > 1 && scalars.first!.isASCII && char != "\r\n" &&
!char.isLetter && !char.isNumber {
if char.isConfusable {
p.error(.confusableCharacter(char), at: charLoc.location)
}
break
Expand Down
13 changes: 13 additions & 0 deletions Sources/_RegexParser/Utility/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ extension Character {
let str = String(self)
return str._nfcCodeUnits.elementsEqual(str.utf8)
}

/// Whether this character could be confusable with a metacharacter in a
/// regex literal.
///
/// A "confusable" character is one that starts with a non-alphanumeric ASCII
/// character and includes other combining Unicode scalars. For example,
/// `"[́"` (aka `"[\u{301}"`) is confusable, since it looks just like the
/// `"["` metacharacter, but doesn't parse as one.
public var isConfusable: Bool {
let scalars = self.unicodeScalars
return scalars.count > 1 && scalars.first!.isASCII && self != "\r\n" &&
!self.isLetter && !self.isNumber
}
}

extension CustomStringConvertible {
Expand Down
1 change: 1 addition & 0 deletions Sources/_StringProcessing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ add_library(_StringProcessing
Compiler.swift
ConsumerInterface.swift
Executor.swift
LiteralPrinter.swift
MatchingOptions.swift
PrintAsPattern.swift)
target_compile_options(_StringProcessing PRIVATE
Expand Down
Loading