Skip to content

Commit bca1d2b

Browse files
committed
Change scalar semantics to match swiftlang#565
1 parent fce8f9a commit bca1d2b

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

Sources/_StringProcessing/ByteCodeGen.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fileprivate extension Compiler.ByteCodeGen {
9999
}
100100

101101
// Fast path for eliding boundary checks for an all ascii quoted literal
102-
if optimizationsEnabled && s.allSatisfy({char in char.isASCII}) {
102+
if optimizationsEnabled && s.allSatisfy(\.isASCII) {
103103
let lastIdx = s.unicodeScalars.indices.last!
104104
for idx in s.unicodeScalars.indices {
105105
let boundaryCheck = idx == lastIdx
@@ -274,10 +274,13 @@ fileprivate extension Compiler.ByteCodeGen {
274274
}
275275

276276
mutating func emitScalar(_ s: UnicodeScalar) {
277+
// A scalar in grapheme semantic mode must match a full charcter, so
278+
// perform a boundary check
279+
let boundaryCheck = options.semanticLevel == .graphemeCluster
277280
if options.isCaseInsensitive && s.properties.isCased {
278-
builder.buildMatchScalarCaseInsensitive(s, boundaryCheck: false)
281+
builder.buildMatchScalarCaseInsensitive(s, boundaryCheck: boundaryCheck)
279282
} else {
280-
builder.buildMatchScalar(s, boundaryCheck: false)
283+
builder.buildMatchScalar(s, boundaryCheck: boundaryCheck)
281284
}
282285
}
283286

@@ -292,8 +295,12 @@ fileprivate extension Compiler.ByteCodeGen {
292295

293296
if options.isCaseInsensitive && c.isCased {
294297
if optimizationsEnabled && c.isASCII {
295-
// c.isCased ensures that c is not CR-LF, so we know that c is a single scalar
296-
builder.buildMatchScalarCaseInsensitive(c.unicodeScalars.last!, boundaryCheck: true)
298+
// c.isCased ensures that c is not CR-LF,
299+
// so we know that c is a single scalar
300+
assert(c.unicodeScalars.count == 1)
301+
builder.buildMatchScalarCaseInsensitive(
302+
c.unicodeScalars.last!,
303+
boundaryCheck: true)
297304
} else {
298305
builder.buildMatch(c, isCaseInsensitive: true)
299306
}

Sources/_StringProcessing/ConsumerInterface.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,20 @@ extension AST.Atom {
218218
var singleScalar: UnicodeScalar? {
219219
switch kind {
220220
case .scalar(let s): return s.value
221+
case .escaped(let e):
222+
guard let s = e.scalarValue else { return nil }
223+
return s
221224
default: return nil
222225
}
223226
}
224227

225228
var singleScalarASCIIValue: UInt8? {
229+
if let s = singleScalar, s.isASCII {
230+
return UInt8(ascii: s)
231+
}
226232
switch kind {
227233
case let .char(c):
228234
return c._singleScalarAsciiValue
229-
case let .scalar(s) where s.value.isASCII:
230-
return UInt8(ascii: s.value)
231235
default:
232236
return nil
233237
}

0 commit comments

Comments
 (0)