From 0e4029f6656c24eb1911e19dbe1bc14d9c3d2843 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Mon, 7 Jul 2025 14:28:07 +0900 Subject: [PATCH] Don't call needless encoding_updated Needless encoding_updated call may have performance penalty a bit. --- lib/rexml/encoding.rb | 7 ++----- test/test_source.rb | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/rexml/encoding.rb b/lib/rexml/encoding.rb index f8459316..7eb05f4d 100644 --- a/lib/rexml/encoding.rb +++ b/lib/rexml/encoding.rb @@ -13,12 +13,9 @@ def encoding=(encoding) raise ArgumentError, "Bad encoding name #{original_encoding}" end end + encoding = encoding.upcase if encoding return false if defined?(@encoding) and encoding == @encoding - if encoding - @encoding = encoding.upcase - else - @encoding = 'UTF-8' - end + @encoding = encoding || "UTF-8" true end diff --git a/test/test_source.rb b/test/test_source.rb index b309105a..86755f37 100644 --- a/test/test_source.rb +++ b/test/test_source.rb @@ -12,6 +12,21 @@ def setup assert_equal("UTF-8", @source.encoding) end + test("encoding_updated") do + def @source.n_encoding_updated_called + @n_encoding_updated_called + end + def @source.encoding_updated + super + @n_encoding_updated_called ||= 0 + @n_encoding_updated_called += 1 + end + @source.encoding = "shift-jis" + assert_equal(1, @source.n_encoding_updated_called) + @source.encoding = "Shift-JIS" + assert_equal(1, @source.n_encoding_updated_called) + end + test("Encoding") do @source.encoding = Encoding::UTF_8 assert_equal("UTF-8", @source.encoding)