-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add String
multi-replace via Scanner
⏩
#227
Merged
Merged
Conversation
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
Codecov Report
@@ Coverage Diff @@
## master #227 +/- ##
==========================================
+ Coverage 94.96% 95.01% +0.05%
==========================================
Files 97 97
Lines 3254 3289 +35
==========================================
+ Hits 3090 3125 +35
Misses 164 164
Continue to review full report at Codecov.
|
nbarreto6
approved these changes
Apr 5, 2021
filipe-lemos
reviewed
Apr 5, 2021
diogoeiras
approved these changes
Apr 6, 2021
aclima93
reviewed
Apr 6, 2021
aclima93
reviewed
Apr 6, 2021
aclima93
approved these changes
Apr 6, 2021
df87dac
to
9f10d49
Compare
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()` extension to convert a string into a non line breaking version.
- Add new `newlineCharacterReplacement` parameter to `nonLineBreaking()` to allow tweaking the newline replacement behavior. - Create new `Character.newlines` helper to contain all `Character`s in `CharacterSet.newlines`. - Fix docs.
9f10d49
to
aaa0d0c
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checklist
master
Motivation and Context
Sometimes we need to replace multiple characters in a given
String
, but bothreplacingOccurrences()
andreplacingCharacters
operate on a singleString
/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.Description
Add new
String.replacingOccurrencesOfCharacters(in:skippingCharactersIn:)
extension to allow replacing multiple characters in a string in a single pass.Add new
String.nonLineBreaking()
extension to convert a string into a non line breaking version.