Skip to content

Adds RegexBuilder.CharacterClass.anyUnicodeScalar #315

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
Apr 21, 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
4 changes: 4 additions & 0 deletions Sources/RegexBuilder/CharacterClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ extension RegexComponent where Self == CharacterClass {
public static var anyGrapheme: CharacterClass {
.init(unconverted: .anyGrapheme)
}

public static var anyUnicodeScalar: CharacterClass {
.init(unconverted: .anyUnicodeScalar)
}

public static var whitespace: CharacterClass {
.init(unconverted: .whitespace)
Expand Down
15 changes: 14 additions & 1 deletion Sources/_StringProcessing/_CharacterClassModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public struct _CharacterClassModel: Hashable {
case any
/// Any grapheme cluster
case anyGrapheme
/// Any Unicode scalar
case anyScalar
/// Character.isDigit
case digit
/// Character.isHexDigit
Expand Down Expand Up @@ -159,8 +161,12 @@ public struct _CharacterClassModel: Hashable {
case .graphemeCluster:
let c = str[i]
var matched: Bool
var next = str.index(after: i)
switch cc {
case .any, .anyGrapheme: matched = true
case .anyScalar:
matched = true
next = str.unicodeScalars.index(after: i)
case .digit:
matched = c.isNumber && (c.isASCII || !options.usesASCIIDigits)
case .hexDigit:
Expand All @@ -178,12 +184,13 @@ public struct _CharacterClassModel: Hashable {
if isInverted {
matched.toggle()
}
return matched ? str.index(after: i) : nil
return matched ? next : nil
case .unicodeScalar:
let c = str.unicodeScalars[i]
var matched: Bool
switch cc {
case .any: matched = true
case .anyScalar: matched = true
case .anyGrapheme: fatalError("Not matched in this mode")
case .digit:
matched = c.properties.numericType != nil && (c.isASCII || !options.usesASCIIDigits)
Expand Down Expand Up @@ -228,6 +235,10 @@ extension _CharacterClassModel {
.init(cc: .anyGrapheme, matchLevel: .graphemeCluster)
}

public static var anyUnicodeScalar: _CharacterClassModel {
.init(cc: .any, matchLevel: .unicodeScalar)
}

public static var whitespace: _CharacterClassModel {
.init(cc: .whitespace, matchLevel: .graphemeCluster)
}
Expand Down Expand Up @@ -279,6 +290,7 @@ extension _CharacterClassModel.Representation: CustomStringConvertible {
switch self {
case .any: return "<any>"
case .anyGrapheme: return "<any grapheme>"
case .anyScalar: return "<any scalar>"
case .digit: return "<digit>"
case .hexDigit: return "<hex digit>"
case .horizontalWhitespace: return "<horizontal whitespace>"
Expand Down Expand Up @@ -445,6 +457,7 @@ extension AST.Atom.EscapedBuiltin {
case .notWordCharacter: return .word.inverted

case .graphemeCluster: return .anyGrapheme
case .trueAnychar: return .anyUnicodeScalar

default:
return nil
Expand Down
12 changes: 5 additions & 7 deletions Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1512,13 +1512,11 @@ extension RegexTests {
(eDecomposed, false))

// FIXME: \O is unsupported
firstMatchTest(#"\O\u{301}"#, input: eDecomposed, match: eDecomposed,
xfail: true)
firstMatchTest(#"e\O"#, input: eDecomposed, match: eDecomposed,
xfail: true)
firstMatchTest(#"\O\u{301}"#, input: eComposed, match: nil,
xfail: true)
firstMatchTest(#"e\O"#, input: eComposed, match: nil,
firstMatchTest(#"(?u)\O\u{301}"#, input: eDecomposed, match: eDecomposed)
firstMatchTest(#"(?u)e\O"#, input: eDecomposed, match: eDecomposed,
xfail: true)
firstMatchTest(#"\O"#, input: eComposed, match: eComposed)
firstMatchTest(#"\O"#, input: eDecomposed, match: nil,
xfail: true)

matchTest(
Expand Down