From 4e5038eb3eb4ec22f3af87b5acbacfaf8a1b9200 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Tue, 8 Mar 2022 19:36:08 -0500 Subject: [PATCH] Try and reduce memory allocs when (un)escaping strings (cherry picked from commit ca5049b6bc8e23689ba350673f04e158b225745c) --- common/string_utils.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/common/string_utils.cpp b/common/string_utils.cpp index 0511378a0f..5f83d5548b 100644 --- a/common/string_utils.cpp +++ b/common/string_utils.cpp @@ -142,6 +142,8 @@ wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext ) { wxString converted; + converted.reserve( aSource.length() ); + for( wxUniChar c: aSource ) { if( aContext == CTX_NETNAME ) @@ -232,8 +234,16 @@ wxString EscapeString( const wxString& aSource, ESCAPE_CONTEXT aContext ) wxString UnescapeString( const wxString& aSource ) { + size_t sourceLen = aSource.length(); + + // smallest escape string is three characters, shortcut everything else + if( sourceLen <= 2 ) + { + return aSource; + } + wxString newbuf; - size_t sourceLen = aSource.length(); + newbuf.reserve( sourceLen ); for( size_t i = 0; i < sourceLen; ++i ) {