Optimize replaceAll (#931)

This commit is contained in:
1024jp 2020-03-26 16:15:25 +09:00
parent 97ca807e40
commit 65214d64a7
2 changed files with 4 additions and 3 deletions

View File

@ -9,6 +9,7 @@ Change Log
- Adjust the text baseline to draw characters vertically center in lines.
- Adjust the invisible characters position in the vertical text orientation.
- Optimize the performance of “Replace All” with a large number of matches.
- Duplicate lines more intelligently.
- Use mirrored pilcrow sign “⁋” for an invisible line ending symbol in RTL mode.
- Improve drawing performance.

View File

@ -338,16 +338,16 @@ final class TextFind {
length = scopeRange.length
} else {
// build replacementString
var replacedString = (self.string as NSString).substring(with: scopeRange) as NSString
let replacedString = NSMutableString(string: (self.string as NSString).substring(with: scopeRange))
for item in items.reversed() {
block(.replacementProgress, &ioStop)
if ioStop { return }
// -> Do not convert to Range<Index>. It can fail when the range is smaller than String.Character.
let substringRange = item.range.shifted(offset: -scopeRange.location)
replacedString = replacedString.replacingCharacters(in: substringRange, with: item.string) as NSString
replacedString.replaceCharacters(in: substringRange, with: item.string)
}
replacementItems.append(ReplacementItem(string: replacedString as String, range: scopeRange))
replacementItems.append(ReplacementItem(string: replacedString.copy() as! String, range: scopeRange))
length = replacedString.length
}