From 7b44ffa21efe3b9608254701ffdf44f743b1a324 Mon Sep 17 00:00:00 2001 From: "Cory Gwin @gwincr11" Date: Fri, 3 Nov 2017 21:26:36 -0400 Subject: Add support for multiple encodings in String.blank? Motivation: - When strings are encoded with `.encode("UTF-16LE")` `.blank?` throws an `Encoding::CompatibilityError` exception. - We tested multiple implementation to see what the fastest implementation was, rescueing the execption seems to be the fastest option we could find. Related Issues: - #28953 Changes: - Add a rescue to catch the exception. - Added a `Concurrent::Map` to store a cache of encoded regex objects for requested encoding types. - Use the new `Concurrent::Map` cache to return the correct regex for the string being checked. --- activesupport/test/core_ext/object/blank_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/test/core_ext/object') diff --git a/activesupport/test/core_ext/object/blank_test.rb b/activesupport/test/core_ext/object/blank_test.rb index 749e59ec00..35a3e5922e 100644 --- a/activesupport/test/core_ext/object/blank_test.rb +++ b/activesupport/test/core_ext/object/blank_test.rb @@ -16,8 +16,8 @@ class BlankTest < ActiveSupport::TestCase end end - BLANK = [ EmptyTrue.new, nil, false, "", " ", " \n\t \r ", " ", "\u00a0", [], {} ] - NOT = [ EmptyFalse.new, Object.new, true, 0, 1, "a", [nil], { nil => 0 }, Time.now ] + BLANK = [ EmptyTrue.new, nil, false, "", " ", " \n\t \r ", " ", "\u00a0", [], {}, " ".encode("UTF-16LE")] + NOT = [ EmptyFalse.new, Object.new, true, 0, 1, "a", [nil], { nil => 0 }, Time.now , "my value".encode("UTF-16LE")] def test_blank BLANK.each { |v| assert_equal true, v.blank?, "#{v.inspect} should be blank" } -- cgit v1.2.3