From 410fc64b7b4e2d633b67268d40d1c773961a198a Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sun, 26 Nov 2023 15:36:02 -0800 Subject: [PATCH] Use encoding cache --- Library/Homebrew/extend/blank.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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