diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-01-04 23:22:21 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-04 23:22:21 +0900 |
commit | c7b832730158da5372aeeff512508b2eab4e6fcc (patch) | |
tree | e27364f1710f94418cbfcc1fc579cd9609c35208 /activesupport/lib | |
parent | cb86b95b609c8aa52411322f5a8e0f128696e068 (diff) | |
parent | 7b44ffa21efe3b9608254701ffdf44f743b1a324 (diff) | |
download | rails-c7b832730158da5372aeeff512508b2eab4e6fcc.tar.gz rails-c7b832730158da5372aeeff512508b2eab4e6fcc.tar.bz2 rails-c7b832730158da5372aeeff512508b2eab4e6fcc.zip |
Merge pull request #31049 from gwincr11/cg-blank
Add support for multiple encodings in String.blank?
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/blank.rb | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index e42ad852dd..2ca431ab10 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "active_support/core_ext/regexp" +require "concurrent/map" class Object # An object is blank if it's false, empty, or a whitespace string. @@ -102,6 +103,9 @@ end class String BLANK_RE = /\A[[:space:]]*\z/ + ENCODED_BLANKS = Concurrent::Map.new do |h, enc| + h[enc] = Regexp.new(BLANK_RE.source.encode(enc), BLANK_RE.options | Regexp::FIXEDENCODING) + end # A string is blank if it's empty or contains whitespaces only: # @@ -119,7 +123,12 @@ class String # The regexp that matches blank strings is expensive. For the case of empty # strings we can speed up this method (~3.5x) with an empty? call. The # penalty for the rest of strings is marginal. - empty? || BLANK_RE.match?(self) + empty? || + begin + BLANK_RE.match?(self) + rescue Encoding::CompatibilityError + ENCODED_BLANKS[self.encoding].match?(self) + end end end |