Skip to content

Commit

Permalink
syntax: improve allocation of escape_into
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hhirtz authored Mar 24, 2020
1 parent c158597 commit 3ff6ae1
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion regex-syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub mod utf8;
/// The string returned may be safely used as a literal in a regular
/// expression.
pub fn escape(text: &str) -> String {
let mut quoted = String::with_capacity(text.len());
let mut quoted = String::new();
escape_into(text, &mut quoted);
quoted
}
Expand All @@ -185,6 +185,7 @@ pub fn escape(text: &str) -> String {
/// This will append escape characters into the given buffer. The characters
/// that are appended are safe to use as a literal in a regular expression.
pub fn escape_into(text: &str, buf: &mut String) {
buf.reserve(text.len());
for c in text.chars() {
if is_meta_character(c) {
buf.push('\\');
Expand Down

0 comments on commit 3ff6ae1

Please sign in to comment.