-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sometimes we need to replace multiple characters in a given `String`, but both `replacingOccurrences()` and `replacingCharacters` operate on a single `String`/`Character`, requiring multiple passes and thus becoming very inefficient. One such example is to replace all line breaking characters in a string into their non line breaking version, which requires 6 substitution "passes" (space, hyphen, em dash, en dash, question mark and closing brace). By using a `Scanner` as a matching mechanism, we can implement multi-replace in a single pass on the string, greatly improving efficiency. ## Changes - Add new `String.replacingOccurrencesOfCharacters(in:skippingCharactersIn:)` extension to allow replacing multiple characters in a string in a single pass. - Add new `String.nonLineBreaking(newlineCharacterReplacement:)` extension to convert a string into a non line breaking version and allow tweaking the newline replacement behavior. - Create new `Character.newlines` helper to contain all `Character`s in `CharacterSet.newlines`.
- Loading branch information
Showing
4 changed files
with
263 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Foundation | ||
|
||
extension Character { | ||
|
||
static let lineSeparator: Character = "\u{2028}" | ||
static let nonBreakingSpace: Character = "\u{00a0}" | ||
static let nonBreakingHyphen: Character = "\u{2011}" | ||
static let wordJoiner: Character = "\u{2060}" | ||
static let emDash: Character = "\u{2013}" // — | ||
static let enDash: Character = "\u{2014}" // – | ||
|
||
// from `CharacterSet.newlines` | ||
static let newlines: [Character] = ["\u{A}", "\u{B}", "\u{C}", "\u{D}", "\u{85}", "\u{2028}", "\u{2029}"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters