Fix crash on Find All including line ending

This commit is contained in:
1024jp 2023-10-01 12:37:05 +09:00
parent 294fc95551
commit c6b40dd9ee
3 changed files with 25 additions and 2 deletions

View File

@ -12,6 +12,7 @@ Change Log
### Fixes
- Fix an issue that the application crashed when performing the Find All command with a specific find string.
- Fix an issue that the Settings window disappeared when the application is inactive.
- [trivial] Fix an issue that unwanted debug log appeared in the Console.

View File

@ -104,6 +104,28 @@ extension LineRangeCacheable {
}
/// Return the range of the content lines including the given range.
///
/// Because this method count up all the line ranges up to the given index when not cached yet,
/// there is a large performance disadvantage when just a single line range is needed.
/// In addition, this method actually doesn't has much performance advantage becaouse it checks the line ending range.
///
/// - Parameter range: The range of character for finding the line range.
/// - Returns: The character range of the content line.
func lineContentRange(for range: NSRange) -> NSRange {
let lineRange = self.lineRange(for: range)
guard range.upperBound < lineRange.upperBound else { return lineRange }
let lineEndingRange = self.string.range(of: "\\R", options: [.backwards, .anchored, .regularExpression], range: lineRange)
guard !lineEndingRange.isNotFound else { return lineRange }
return NSRange(lineRange.lowerBound..<lineEndingRange.lowerBound)
}
/// Return the index of the first character of the line touched by the given index.
///
/// Because this method count up all the line ranges up to the given index when not cached yet,

View File

@ -540,8 +540,8 @@ final class TextFinder {
let matchedRange = matches[0]
// build a highlighted line string for result table
let lineRange = lineCounter.lineRange(for: matchedRange)
let lineString = (textFind.string as NSString).substring(with: lineRange).replacing(/\R$/, with: "")
let lineRange = lineCounter.lineContentRange(for: matchedRange)
let lineString = (textFind.string as NSString).substring(with: lineRange)
let attrLineString = NSMutableAttributedString(string: lineString)
for (index, range) in matches.enumerated() where !range.isEmpty {
attrLineString.addAttribute(.backgroundColor,