@@ -386,45 +386,52 @@ private func diagnoseFailedCheck(
386386 return
387387 }
388388
389- for i in 0 ..< min ( buffer. count, 4096 ) {
390- let strIndex = buffer. index ( buffer. startIndex, offsetBy: i)
391- let char = buffer [ strIndex]
392- if char == " \n " {
393- NumLinesForward += 1
394- }
389+ let exampleStringLen = exampleString. count
390+ exampleString. withCString { ( exampleBuf) -> Void in
391+ for i in 0 ..< min ( buffer. count, 4096 ) {
392+ let strIndex = buffer. index ( buffer. startIndex, offsetBy: i)
393+ let char = buffer [ strIndex]
394+ if char == " \n " {
395+ NumLinesForward += 1
396+ }
395397
396- // Patterns have leading whitespace stripped, so skip whitespace when
397- // looking for something which looks like a pattern.
398- if char == " " || char == " \t " {
399- continue ;
400- }
398+ // Patterns have leading whitespace stripped, so skip whitespace when
399+ // looking for something which looks like a pattern.
400+ if char == " " || char == " \t " {
401+ continue ;
402+ }
401403
402- let subEndIdx : String . Index
403- if buffer. count < exampleString. count + i {
404- subEndIdx = buffer. endIndex
405- } else {
406- subEndIdx = buffer. index ( buffer. startIndex, offsetBy: exampleString. count + i)
407- }
408- let subBuffer = buffer [ strIndex..< subEndIdx]
409-
410- // Compute the "quality" of this match as an arbitrary combination of
411- // the match distance and the number of lines skipped to get to this
412- // match.
413- let distance = editDistance ( from: subBuffer, to: exampleString)
414- let quality = Double ( distance) + ( Double ( NumLinesForward) / 100.0 )
415- if quality < BestQuality || BestLine == nil {
416- BestLine = i
417- BestQuality = quality
404+ let subEndIdx : String . Index
405+ if buffer. count < exampleString. count + i {
406+ subEndIdx = buffer. endIndex
407+ } else {
408+ subEndIdx = buffer. index ( buffer. startIndex, offsetBy: exampleString. count + i)
409+ }
410+ let subBuffer = buffer [ strIndex..< subEndIdx]
411+
412+ // Compute the "quality" of this match as an arbitrary combination of
413+ // the match distance and the number of lines skipped to get to this
414+ // match.
415+ let bodyBufLen = subBuffer. count
416+ let distance = subBuffer. withCString { ( bodyBuf) -> Int in
417+ return editDistance ( from: bodyBuf, fromLength: bodyBufLen,
418+ to: exampleBuf, toLength: exampleStringLen)
419+ }
420+ let quality = Double ( distance) + ( Double ( NumLinesForward) / 100.0 )
421+ if quality < BestQuality || BestLine == nil {
422+ BestLine = i
423+ BestQuality = quality
424+ }
418425 }
419- }
420426
421- if let Best = BestLine, BestQuality < 50 {
422- buffer. utf8CString. withUnsafeBufferPointer { buf in
423- let otherPatternLoc = CheckLocation . inBuffer (
424- buf. baseAddress!. advanced ( by: Best) ,
425- UnsafeBufferPointer ( start: buf. baseAddress? . advanced ( by: Best) , count: buf. count - Best)
426- )
427- diagnose ( . note, at: otherPatternLoc, with: " possible intended match here " , options: options)
427+ if let Best = BestLine, BestQuality < 50 {
428+ buffer. utf8CString. withUnsafeBufferPointer { buf in
429+ let otherPatternLoc = CheckLocation . inBuffer (
430+ buf. baseAddress!. advanced ( by: Best) ,
431+ UnsafeBufferPointer ( start: buf. baseAddress? . advanced ( by: Best) , count: buf. count - Best)
432+ )
433+ diagnose ( . note, at: otherPatternLoc, with: " possible intended match here " , options: options)
434+ }
428435 }
429436 }
430437}
0 commit comments