Skip to content

Enable quantification optimizations for scalar semantics #671

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 7 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[testing] Quantified scalar semantic matching
  • Loading branch information
milseman committed May 20, 2023
commit bade9d2992d7aa7d72d8fa2351e04f50ecba0397
4 changes: 3 additions & 1 deletion Sources/_StringProcessing/ByteCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ extension Compiler {
var hasEmittedFirstMatchableAtom = false

private let compileOptions: _CompileOptions
fileprivate var optimizationsEnabled: Bool { !compileOptions.contains(.disableOptimizations) }
fileprivate var optimizationsEnabled: Bool {
!compileOptions.contains(.disableOptimizations)
}

init(
options: MatchingOptions,
Expand Down
36 changes: 36 additions & 0 deletions Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,42 @@ extension RegexTests {
// TODO: After captures, easier to test these
}

func testQuantificationScalarSemantics() {
// TODO: We want more thorough testing here, including "a{n,m}", "a?", etc.

firstMatchTest("a*", input: "aaa\u{301}", match: "aa")
firstMatchTest("a*", input: "aaa\u{301}", match: "aaa", semanticLevel: .unicodeScalar)
firstMatchTest("a+", input: "aaa\u{301}", match: "aa")
firstMatchTest("a+", input: "aaa\u{301}", match: "aaa", semanticLevel: .unicodeScalar)
firstMatchTest("a?", input: "a\u{301}", match: "")
firstMatchTest("a?", input: "a\u{301}", match: "a", semanticLevel: .unicodeScalar)

firstMatchTest("[ab]*", input: "abab\u{301}", match: "aba")
firstMatchTest("[ab]*", input: "abab\u{301}", match: "abab", semanticLevel: .unicodeScalar)
firstMatchTest("[ab]+", input: "abab\u{301}", match: "aba")
firstMatchTest("[ab]+", input: "abab\u{301}", match: "abab", semanticLevel: .unicodeScalar)
firstMatchTest("[ab]?", input: "b\u{301}", match: "")
firstMatchTest("[ab]?", input: "b\u{301}", match: "b", semanticLevel: .unicodeScalar)

firstMatchTest(#"\s*"#, input: " \u{301}", match: " \u{301}")
firstMatchTest(#"\s*"#, input: " \u{301}", match: " ", semanticLevel: .unicodeScalar)
firstMatchTest(#"\s+"#, input: " \u{301}", match: " \u{301}")
firstMatchTest(#"\s+"#, input: " \u{301}", match: " ", semanticLevel: .unicodeScalar)
firstMatchTest(#"\s?"#, input: " \u{301}", match: " \u{301}")
firstMatchTest(#"\s?"#, input: " \u{301}", match: " ", semanticLevel: .unicodeScalar)

firstMatchTest(#".*?a"#, input: "xxa\u{301}xaZ", match: "xxa\u{301}xa")
firstMatchTest(#".*?a"#, input: "xxa\u{301}xaZ", match: "xxa", semanticLevel: .unicodeScalar)
firstMatchTest(#".+?a"#, input: "xxa\u{301}xaZ", match: "xxa\u{301}xa")
firstMatchTest(#".+?a"#, input: "xxa\u{301}xaZ", match: "xxa", semanticLevel: .unicodeScalar)
firstMatchTest(#".?a"#, input: "e\u{301}aZ", match: "e\u{301}a")
firstMatchTest(#".?a"#, input: "e\u{301}aZ", match: "\u{301}a", semanticLevel: .unicodeScalar)



// TODO: other test cases?
}

func testMatchCharacterClasses() {
// Must have new stdlib for character class ranges and word boundaries.
guard ensureNewStdlib() else { return }
Expand Down