@@ -138,7 +138,9 @@ public final class SourceLocationConverter {
138
138
public init ( file: String , source: String ) {
139
139
self . file = file
140
140
self . source = Array ( source. utf8)
141
- ( self . lines, endOfFile) = computeLines ( source)
141
+ ( self . lines, endOfFile) = self . source. withUnsafeBufferPointer { buf in
142
+ return computeLines ( SyntaxText ( buffer: buf) )
143
+ }
142
144
assert ( source. utf8. count == endOfFile. utf8Offset)
143
145
}
144
146
@@ -397,7 +399,7 @@ fileprivate func computeLines(
397
399
return ( lines, position)
398
400
}
399
401
400
- fileprivate func computeLines( _ source: String ) -> ( [ AbsolutePosition ] , AbsolutePosition ) {
402
+ fileprivate func computeLines( _ source: SyntaxText ) -> ( [ AbsolutePosition ] , AbsolutePosition ) {
401
403
var lines : [ AbsolutePosition ] = [ ]
402
404
// First line starts from the beginning.
403
405
lines. append ( . startOfFile)
@@ -412,26 +414,25 @@ fileprivate func computeLines(_ source: String) -> ([AbsolutePosition], Absolute
412
414
return ( lines, position)
413
415
}
414
416
415
- fileprivate extension String {
417
+ fileprivate extension SyntaxText {
416
418
/// Walks and passes to `body` the `SourceLength` for every detected line,
417
419
/// with the newline character included.
418
420
/// - Returns: The leftover `SourceLength` at the end of the walk.
419
421
func forEachLineLength(
420
422
prefix: SourceLength = . zero,
421
423
body: ( SourceLength ) -> ( )
422
424
) -> SourceLength {
423
- let utf8 = self . utf8
424
- let startIndex = utf8. startIndex
425
- let endIndex = utf8. endIndex
425
+ // let startIndex = utf8.startIndex
426
+ // let endIndex = utf8.endIndex
426
427
var curIdx = startIndex
427
428
var lineLength = prefix
428
429
let advanceLengthByOne = { ( ) -> ( ) in
429
430
lineLength += SourceLength ( utf8Length: 1 )
430
- curIdx = utf8 . index ( after: curIdx)
431
+ curIdx = self . index ( after: curIdx)
431
432
}
432
433
433
434
while curIdx < endIndex {
434
- let char = utf8 [ curIdx]
435
+ let char = self [ curIdx]
435
436
advanceLengthByOne ( )
436
437
437
438
/// From https://docs.swift.org/swift-book/ReferenceManual/LexicalStructure.html#grammar_line-break
@@ -441,7 +442,7 @@ fileprivate extension String {
441
442
let isNewline = { ( ) -> Bool in
442
443
if char == 10 { return true }
443
444
if char == 13 {
444
- if curIdx < endIndex && utf8 [ curIdx] == 10 { advanceLengthByOne ( ) }
445
+ if curIdx < endIndex && self [ curIdx] == 10 { advanceLengthByOne ( ) }
445
446
return true
446
447
}
447
448
return false
@@ -456,11 +457,11 @@ fileprivate extension String {
456
457
}
457
458
458
459
func containsSwiftNewline( ) -> Bool {
459
- return utf8 . contains { $0 == 10 || $0 == 13 }
460
+ return self . contains { $0 == 10 || $0 == 13 }
460
461
}
461
462
}
462
463
463
- fileprivate extension TriviaPiece {
464
+ fileprivate extension RawTriviaPiece {
464
465
/// Walks and passes to `body` the `SourceLength` for every detected line,
465
466
/// with the newline character included.
466
467
/// - Returns: The leftover `SourceLength` at the end of the walk.
@@ -495,7 +496,7 @@ fileprivate extension TriviaPiece {
495
496
let . docLineComment( text) :
496
497
// Line comments are not supposed to contain newlines.
497
498
assert ( !text. containsSwiftNewline ( ) , " line comment created that contained a new-line character " )
498
- lineLength += SourceLength ( utf8Length: text. utf8 . count)
499
+ lineLength += SourceLength ( utf8Length: text. count)
499
500
case let . blockComment( text) ,
500
501
let . docBlockComment( text) ,
501
502
let . unexpectedText( text) :
@@ -505,7 +506,7 @@ fileprivate extension TriviaPiece {
505
506
}
506
507
}
507
508
508
- fileprivate extension Trivia {
509
+ fileprivate extension Array where Element == RawTriviaPiece {
509
510
/// Walks and passes to `body` the `SourceLength` for every detected line,
510
511
/// with the newline character included.
511
512
/// - Returns: The leftover `SourceLength` at the end of the walk.
@@ -530,9 +531,9 @@ fileprivate extension TokenSyntax {
530
531
body: ( SourceLength ) -> ( )
531
532
) -> SourceLength {
532
533
var curPrefix = prefix
533
- curPrefix = self . leadingTrivia . forEachLineLength ( prefix: curPrefix, body: body)
534
- curPrefix = self . text . forEachLineLength ( prefix: curPrefix, body: body)
535
- curPrefix = self . trailingTrivia . forEachLineLength ( prefix: curPrefix, body: body)
534
+ curPrefix = self . tokenView . leadingRawTriviaPieces . forEachLineLength ( prefix: curPrefix, body: body)
535
+ curPrefix = self . tokenView . rawText . forEachLineLength ( prefix: curPrefix, body: body)
536
+ curPrefix = self . tokenView . trailingRawTriviaPieces . forEachLineLength ( prefix: curPrefix, body: body)
536
537
return curPrefix
537
538
}
538
539
}
0 commit comments