diff --git a/Library/Homebrew/extend/blank.rb b/Library/Homebrew/extend/blank.rb index 735af26276..5aa09c033a 100644 --- a/Library/Homebrew/extend/blank.rb +++ b/Library/Homebrew/extend/blank.rb @@ -133,6 +133,12 @@ end class String BLANK_RE = /\A[[:space:]]*\z/.freeze + # This is a cache that is intentionally mutable + # rubocop:disable Style/MutableConstant + ENCODED_BLANKS_ = T.let(Hash.new do |h, enc| + h[enc] = Regexp.new(BLANK_RE.source.encode(enc), BLANK_RE.options | Regexp::FIXEDENCODING) + end, T::Hash[Encoding, Regexp]) + # rubocop:enable Style/MutableConstant # A string is blank if it's empty or contains whitespaces only: # @@ -153,7 +159,7 @@ class String begin BLANK_RE.match?(self) rescue Encoding::CompatibilityError - Regexp.new(BLANK_RE.source.encode(encoding), BLANK_RE.options | Regexp::FIXEDENCODING).match?(self) + T.must(ENCODED_BLANKS_[encoding]).match?(self) end end