Skip to content
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 3 commits into from
Apr 8, 2021

Conversation

p4checo
Copy link
Member

@p4checo p4checo commented Apr 4, 2021

Checklist

Motivation and Context

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.

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.

@p4checo p4checo requested a review from a team April 4, 2021 00:33
@codecov
Copy link

codecov bot commented Apr 4, 2021

Codecov Report

Merging #227 (aaa0d0c) into master (45bdc59) will increase coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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              
Impacted Files Coverage Δ
Sources/Extensions/Foundation/String.swift 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 45bdc59...aaa0d0c. Read the comment docs.

@p4checo p4checo force-pushed the add-string-multi-replace-using-scanner branch from df87dac to 9f10d49 Compare April 6, 2021 18:02
p4checo added 3 commits April 7, 2021 16:28
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.
@p4checo p4checo force-pushed the add-string-multi-replace-using-scanner branch from 9f10d49 to aaa0d0c Compare April 7, 2021 15:28
@p4checo p4checo merged commit 408a301 into master Apr 8, 2021
@p4checo p4checo deleted the add-string-multi-replace-using-scanner branch April 8, 2021 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants