From ccf32a5ca4ce29f9019cf8bae9f3c0f69e27bd22 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Thu, 3 Nov 2022 23:34:09 -0700 Subject: [PATCH] [ruby/erb] Do not allocate a new String if not needed [Feature #19102]https://github.com/ruby/erb/commit/ecebf8075c --- ext/erb/erb.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/erb/erb.c b/ext/erb/erb.c index 9376fa5dcb..4adab8ad33 100644 --- a/ext/erb/erb.c +++ b/ext/erb/erb.c @@ -55,18 +55,18 @@ optimized_escape_html(VALUE str) } } - VALUE escaped; + VALUE escaped = str; if (RSTRING_LEN(str) < (dest - buf)) { escaped = rb_str_new(buf, dest - buf); preserve_original_state(str, escaped); } - else { - escaped = rb_str_dup(str); - } ALLOCV_END(vbuf); return escaped; } +// ERB::Util.html_escape is different from CGI.escapeHTML in the following two parts: +// * ERB::Util.html_escape converts an argument with #to_s first (only if it's not T_STRING) +// * ERB::Util.html_escape does not allocate a new string when nothing needs to be escaped static VALUE erb_escape_html(VALUE self, VALUE str) {