Skip to content

Commit bf9b2a7

Browse files
committed
Add character class / character exclusion
1 parent 9e9978c commit bf9b2a7

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Sources/_StringProcessing/Regex/DSLList.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,14 @@ extension DSLTree.Atom {
330330
return a != b
331331
case (.characterClass(let a), .characterClass(let b)):
332332
return a.excludes(b)
333-
333+
// FIXME: Need to track matching options so we can know if this actually matches
334+
case (.characterClass(let a), .char(let b)), (.char(let b), .characterClass(let a)):
335+
let s = "\(b)"
336+
return a.asRuntimeModel(MatchingOptions()).matches(in: s, at: s.startIndex, limitedBy: s.endIndex) == nil
337+
case (.characterClass(let a), .scalar(let b)), (.scalar(let b), .characterClass(let a)):
338+
let s = "\(b)"
339+
return a.asRuntimeModel(MatchingOptions()).matches(in: s, at: s.startIndex, limitedBy: s.endIndex) == nil
340+
334341
default:
335342
return false
336343
}

Tests/RegexTests/OptimizationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import Testing
3131
}
3232

3333
@available(macOS 9999, *)
34-
@Test(arguments: [#/a+b/#, #/a*b/#, #/\w+\s/#, #/(?:a+b|b+a)/#, #/(?:(?:a+b)+b)/#])
34+
@Test(arguments: [#/a+b/#, #/a*b/#, #/\w+\s/#, #/(?:a+b|b+a)/#, #/(?:(?:a+b)+b)/#, #/\d+a/#])
3535
func autoPossessify(pattern: Regex<Substring>) throws {
3636
var list = DSLList(tree: pattern.program.tree)
3737
list.autoPossessify()

0 commit comments

Comments
 (0)