Skip to content

Commit b8450ea

Browse files
committed
Miscellaneous (performance) improvements to conflict marker lexing
1 parent afbe222 commit b8450ea

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

Sources/SwiftParser/Lexer/Cursor.swift

+20-15
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ extension Lexer {
292292
self.stateStack.perform(stateTransition: stateTransition, stateAllocator: stateAllocator)
293293
}
294294

295-
func starts(with possiblePrefix: some Sequence<UInt8>) -> Bool {
295+
func starts(with possiblePrefix: SyntaxText) -> Bool {
296296
return self.input.starts(with: possiblePrefix)
297297
}
298298

@@ -2036,7 +2036,7 @@ extension Lexer.Cursor {
20362036
}
20372037

20382038
// Special case; allow '`$`'.
2039-
if quote.starts(with: "`$`".utf8) {
2039+
if quote.starts(with: "`$`") {
20402040
self = quote
20412041
let firstBacktickConsumed = self.advance(matching: "`")
20422042
let dollarConsumed = self.advance(matching: "$")
@@ -2383,7 +2383,7 @@ extension Lexer.Cursor {
23832383
case normal
23842384
case perforce
23852385

2386-
var introducer: String {
2386+
var introducer: SyntaxText {
23872387
switch self {
23882388
case .perforce:
23892389
return ">>>> "
@@ -2392,7 +2392,7 @@ extension Lexer.Cursor {
23922392
}
23932393
}
23942394

2395-
var terminator: String {
2395+
var terminator: SyntaxText {
23962396
switch self {
23972397
case .perforce:
23982398
return "<<<<\n"
@@ -2408,11 +2408,15 @@ extension Lexer.Cursor {
24082408
}
24092409

24102410
// Check to see if we have <<<<<<< or >>>>.
2411-
guard start.starts(with: "<<<<<<< ".utf8) || start.starts(with: ">>>> ".utf8) else {
2411+
let kind: ConflictMarker
2412+
if start.starts(with: ConflictMarker.normal.introducer) {
2413+
kind = .normal
2414+
} else if start.starts(with: ConflictMarker.perforce.introducer) {
2415+
kind = .perforce
2416+
} else {
24122417
return false
24132418
}
24142419

2415-
let kind = start.is(at: "<") ? ConflictMarker.normal : .perforce
24162420
guard let end = Self.findConflictEnd(start, markerKind: kind) else {
24172421
// No end of conflict marker found.
24182422
return false
@@ -2432,16 +2436,17 @@ extension Lexer.Cursor {
24322436
static func findConflictEnd(_ curPtr: Lexer.Cursor, markerKind: ConflictMarker) -> Lexer.Cursor? {
24332437
// Get a reference to the rest of the buffer minus the length of the start
24342438
// of the conflict marker.
2435-
let advanced = curPtr.input.baseAddress?.advanced(by: markerKind.introducer.utf8.count)
2439+
let advanced = curPtr.input.baseAddress?.advanced(by: markerKind.introducer.count)
24362440
var restOfBuffer = Lexer.Cursor(
2437-
input: .init(start: advanced, count: curPtr.input.count - markerKind.introducer.utf8.count),
2438-
previous: curPtr.input[markerKind.introducer.utf8.count - 1]
2441+
input: .init(start: advanced, count: curPtr.input.count - markerKind.introducer.count),
2442+
previous: curPtr.input[markerKind.introducer.count - 1]
24392443
)
2444+
let terminator = markerKind.terminator
2445+
let terminatorStart = terminator.first!
24402446
while !restOfBuffer.isAtEndOfFile {
2441-
let terminatorStart = markerKind.terminator.unicodeScalars.first!
2442-
restOfBuffer.advance(while: { byte in byte != terminatorStart })
2447+
restOfBuffer.advance(while: { $0.value != terminatorStart })
24432448

2444-
guard restOfBuffer.starts(with: markerKind.terminator.utf8) else {
2449+
guard restOfBuffer.starts(with: terminator) else {
24452450
_ = restOfBuffer.advance()
24462451
continue
24472452
}
@@ -2452,10 +2457,10 @@ extension Lexer.Cursor {
24522457
continue
24532458
}
24542459

2455-
let advanced = restOfBuffer.input.baseAddress?.advanced(by: markerKind.terminator.utf8.count)
2460+
let advanced = restOfBuffer.input.baseAddress?.advanced(by: terminator.count)
24562461
return Lexer.Cursor(
2457-
input: .init(start: advanced, count: restOfBuffer.input.count - markerKind.terminator.utf8.count),
2458-
previous: restOfBuffer.input[markerKind.terminator.utf8.count - 1]
2462+
input: .init(start: advanced, count: restOfBuffer.input.count - terminator.count),
2463+
previous: restOfBuffer.input[terminator.count - 1]
24592464
)
24602465
}
24612466
return nil

0 commit comments

Comments
 (0)