Skip to content

Commit 5ed7b35

Browse files
committed
Improve allocation of escape_into
In regex-syntax, Make escape_into allocate at most twice (all characters are escaped) instead of more if used on an unallocated String. This way, escape doesn't have to allocate.
1 parent c158597 commit 5ed7b35

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

regex-syntax/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub mod utf8;
175175
/// The string returned may be safely used as a literal in a regular
176176
/// expression.
177177
pub fn escape(text: &str) -> String {
178-
let mut quoted = String::with_capacity(text.len());
178+
let mut quoted = String::new();
179179
escape_into(text, &mut quoted);
180180
quoted
181181
}
@@ -185,6 +185,7 @@ pub fn escape(text: &str) -> String {
185185
/// This will append escape characters into the given buffer. The characters
186186
/// that are appended are safe to use as a literal in a regular expression.
187187
pub fn escape_into(text: &str, buf: &mut String) {
188+
buf.reserve(text.len());
188189
for c in text.chars() {
189190
if is_meta_character(c) {
190191
buf.push('\\');

0 commit comments

Comments
 (0)