Skip to content

Commit 3ff6ae1

Browse files
authored
syntax: improve allocation of escape_into
This causes escape_into to reserve capacity instead of having escape do it. This is a bit more general and will benefit users of escape_into. PR #655
1 parent c158597 commit 3ff6ae1

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)