Skip to content

[5.7] Improve Unicode/UTS18 and semantic level support (#268) #405

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

Closed
Closed
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ let package = Package(
name: "RegexTests",
dependencies: ["_StringProcessing"],
swiftSettings: [
.unsafeFlags(["-Xfrontend", "-disable-availability-checking"])
.unsafeFlags(["-Xfrontend", "-disable-availability-checking"]),
]),
.testTarget(
name: "RegexBuilderTests",
Expand Down
56 changes: 43 additions & 13 deletions Sources/_StringProcessing/ByteCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,15 @@ extension Compiler.ByteCodeGen {
}

mutating func emitCharacter(_ c: Character) throws {
// FIXME: Does semantic level matter?
// Unicode scalar matches the specific scalars that comprise a character
if options.semanticLevel == .unicodeScalar {
print("emitting '\(c)' as a sequence of \(c.unicodeScalars.count) scalars")
for scalar in c.unicodeScalars {
try emitScalar(scalar)
}
return
}

if options.isCaseInsensitive && c.isCased {
// TODO: buildCaseInsensitiveMatch(c) or buildMatch(c, caseInsensitive: true)
builder.buildConsume { input, bounds in
Expand Down Expand Up @@ -625,22 +633,44 @@ extension Compiler.ByteCodeGen {
try emitAtom(a)

case let .quotedLiteral(s):
// TODO: Should this incorporate options?
if options.isCaseInsensitive {
// TODO: buildCaseInsensitiveMatchSequence(c) or alternative
builder.buildConsume { input, bounds in
var iterator = s.makeIterator()
if options.semanticLevel == .graphemeCluster {
if options.isCaseInsensitive {
// TODO: buildCaseInsensitiveMatchSequence(c) or alternative
builder.buildConsume { input, bounds in
var iterator = s.makeIterator()
var currentIndex = bounds.lowerBound
while let ch = iterator.next() {
guard currentIndex < bounds.upperBound,
ch.lowercased() == input[currentIndex].lowercased()
else { return nil }
input.formIndex(after: &currentIndex)
}
return currentIndex
}
} else {
builder.buildMatchSequence(s)
}
} else {
builder.buildConsume {
[caseInsensitive = options.isCaseInsensitive] input, bounds in
// TODO: Case folding
var iterator = s.unicodeScalars.makeIterator()
var currentIndex = bounds.lowerBound
while let ch = iterator.next() {
guard currentIndex < bounds.upperBound,
ch.lowercased() == input[currentIndex].lowercased()
else { return nil }
input.formIndex(after: &currentIndex)
while let scalar = iterator.next() {
guard currentIndex < bounds.upperBound else { return nil }
if caseInsensitive {
if scalar.properties.lowercaseMapping != input.unicodeScalars[currentIndex].properties.lowercaseMapping {
return nil
}
} else {
if scalar != input.unicodeScalars[currentIndex] {
return nil
}
}
input.unicodeScalars.formIndex(after: &currentIndex)
}
return currentIndex
}
} else {
builder.buildMatchSequence(s)
}

case let .regexLiteral(l):
Expand Down
Loading