Skip to content

Improve Unicode/UTS18 and semantic level support #268

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 16 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions Sources/_StringProcessing/ByteCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,16 @@ extension Compiler.ByteCodeGen {
: nil
}
} else {
let done = builder.makeAddress()
let next = builder.makeAddress()
builder.buildSave(next)
for scalar in c.unicodeScalars {
try emitScalar(scalar)
}
builder.buildBranch(to: done)
builder.label(next)
builder.buildMatch(c)
builder.label(done)
}
}

Expand Down
10 changes: 7 additions & 3 deletions Sources/_StringProcessing/_CharacterClassModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,14 @@ public struct _CharacterClassModel: Hashable {
return matched ? str.index(after: i) : nil
case .unicodeScalar:
let c = str.unicodeScalars[i]
var nextIndex = str.unicodeScalars.index(after: i)
var matched: Bool
switch cc {
case .any: matched = true
case .anyGrapheme: fatalError("Not matched in this mode")
case .any:
matched = true
case .anyGrapheme:
matched = true
nextIndex = str.index(after: i)
case .digit:
matched = c.properties.numericType != nil && (c.isASCII || !options.usesASCIIDigits)
case .hexDigit:
Expand All @@ -197,7 +201,7 @@ public struct _CharacterClassModel: Hashable {
if isInverted {
matched.toggle()
}
return matched ? str.unicodeScalars.index(after: i) : nil
return matched ? nextIndex : nil
}
}
}
Expand Down
29 changes: 23 additions & 6 deletions Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ extension RegexTests {
"a++a",
("babc", nil),
("baaabc", nil),
("bb", nil))
("bb", nil),
xfail: true)
firstMatchTests(
"a+?a",
("babc", nil),
Expand Down Expand Up @@ -468,22 +469,33 @@ extension RegexTests {
"a{2,4}+a",
("babc", nil),
("baabc", nil),
("baaabc", nil),
("baaaaabc", "aaaaa"),
("baaaaaaaabc", "aaaaa"),
("bb", nil))
firstMatchTests(
"a{,4}+a",
("babc", nil),
("baabc", nil),
("baaabc", nil),
("baaaaabc", "aaaaa"),
("baaaaaaaabc", "aaaaa"),
("bb", nil))
firstMatchTests(
"a{2,}+a",
("babc", nil),
("baabc", nil),
("bb", nil))

// XFAIL'd versions of the above
firstMatchTests(
"a{2,4}+a",
("baaabc", nil),
xfail: true)
firstMatchTests(
"a{,4}+a",
("babc", nil),
("baabc", nil),
("baaabc", nil),
xfail: true)
firstMatchTests(
"a{2,}+a",
("baaabc", nil),
("baaaaabc", nil),
("baaaaaaaabc", nil),
Expand Down Expand Up @@ -964,7 +976,11 @@ extension RegexTests {
firstMatchTests(
#"\u{65}"#, // Scalar 'e' is present in both
("Cafe\u{301}", nil), // but scalar mode requires boundary at end of match
xfail: true)
firstMatchTests(
#"\u{65}"#, // Scalar 'e' is present in both
("Sol Cafe", "e")) // standalone is okay

firstMatchTests(
#"\u{65}\y"#, // Grapheme boundary assertion
("Cafe\u{301}", nil),
Expand Down Expand Up @@ -1379,7 +1395,8 @@ extension RegexTests {
firstMatchTest(#"\u{65}\u{301}$"#, input: eDecomposed, match: eDecomposed)
firstMatchTest(#"\u{65}\u{301}$"#, input: eComposed, match: eComposed)

firstMatchTest(#"\u{65}"#, input: eDecomposed, match: "e",
// FIXME: Implicit \y at end of match
firstMatchTest(#"\u{65}"#, input: eDecomposed, match: nil,
xfail: true)
firstMatchTest(#"\u{65}$"#, input: eDecomposed, match: nil)
// FIXME: \y is unsupported
Expand Down
9 changes: 7 additions & 2 deletions Tests/RegexTests/UTS18Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,16 @@ extension UTS18Tests {
// matching against an arbitrary extended grapheme cluster, Character Classes
// with Strings, and extended grapheme cluster boundaries.
func testExtendedGraphemeClusters() {
XCTExpectFailure { XCTFail("Implement tests") }
XCTAssertTrue("abcdef🇬🇭".contains(#/abcdef.$/#))
XCTAssertTrue("abcdef🇬🇭".contains(#/abcdef\X$/#))
XCTAssertTrue("abcdef🇬🇭".contains(#/abcdef\X$/#.matchingSemantics(.unicodeScalar)))
XCTAssertTrue("abcdef🇬🇭".contains(#/abcdef.+\y/#.matchingSemantics(.unicodeScalar)))
}

func testCharacterClassesWithStrings() {
XCTExpectFailure { XCTFail("Implement tests") }
let regex = #/[a-z🧐🇧🇪🇧🇫🇧🇬]/#
XCTAssertTrue("🧐".contains(regex))
XCTAssertTrue("🇧🇫".contains(regex))
}

// RL2.3 Default Word Boundaries
Expand Down