Skip to content

str: common logic for replace & replacen#56464

Closed
ljedrz wants to merge 1 commit intorust-lang:masterfrom
ljedrz:common_str_replace_logic
Closed

str: common logic for replace & replacen#56464
ljedrz wants to merge 1 commit intorust-lang:masterfrom
ljedrz:common_str_replace_logic

Conversation

@ljedrz
Copy link
Contributor

@ljedrz ljedrz commented Dec 3, 2018

The logic of str::replace and str::replacen is almost identical - we can use a common helper function. The initial capacity of the resulting string was different (0 vs. 32), but I propose a different approach: use the same capacity as in the original string; this will neither reallocate too often (or not at all if the substitute's length is the same as the substituted bit's), nor will it overallocate for short strings. It will also be an exact fit in case of no pattern hits.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-blocked Status: Blocked on something else such as an RFC or other implementation work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants