Merge pull request #31049 from gwincr11/cg-blank
Add support for multiple encodings in String.blank?
This commit is contained in:
commit
c7b8327301
@ -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 @@ class Hash
|
||||
|
||||
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 @@ def blank?
|
||||
# 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
|
||||
|
||||
|
@ -16,8 +16,8 @@ def empty?
|
||||
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" }
|
||||
|
Loading…
Reference in New Issue
Block a user