From fe7f80f647a11fb71e9f922706e1138fa31225c7 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Mon, 10 May 2021 14:02:07 -0400 Subject: [PATCH] Refactor out and correctly create path regex --- Library/Homebrew/dev-cmd/bottle.rb | 6 ++++-- Library/Homebrew/keg_relocate.rb | 15 ++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/dev-cmd/bottle.rb b/Library/Homebrew/dev-cmd/bottle.rb index 2232274b35..496c5a7b68 100644 --- a/Library/Homebrew/dev-cmd/bottle.rb +++ b/Library/Homebrew/dev-cmd/bottle.rb @@ -153,7 +153,9 @@ module Homebrew until io.eof? str = io.readline.chomp next if ignores.any? { |i| i =~ str } - next unless str.match? Keg.relocatable_path_regex(string) + + path_regex = Keg::Relocation.path_regex(string) + next unless str.match? path_regex offset, match = str.split(" ", 2) @@ -162,7 +164,7 @@ module Homebrew # Each item in the list should be checked separately match.split(":").each do |sub_match| # Not all items in the list may be matches - next unless sub_match.match? Keg.relocatable_path_regex(string) + next unless sub_match.match? path_regex next if linked_libraries.include? sub_match # Don't bother reporting a string if it was found by otool # Do not report matches to files that do not exist. diff --git a/Library/Homebrew/keg_relocate.rb b/Library/Homebrew/keg_relocate.rb index dd55d55140..6cc7ffa5ba 100644 --- a/Library/Homebrew/keg_relocate.rb +++ b/Library/Homebrew/keg_relocate.rb @@ -24,7 +24,7 @@ class Keg sig { params(key: Symbol, old_value: T.any(String, Regexp), new_value: String).void } def add_replacement_pair(key, old_value, new_value) - @replacement_map[key] = [path_replacement_regex(old_value), new_value] + @replacement_map[key] = [self.class.path_regex(old_value), new_value] end sig { params(key: Symbol).returns(T::Array[T.any(String, Regexp)]) } @@ -48,10 +48,15 @@ class Keg any_changed end - sig { params(text: T.any(String, Regexp)).returns(Regexp) } - def self.path_replacement_regex(value) - value = Regexp.escape(value) if value.is_a? String - Regexp.new(RELOCATABLE_PATH_REGEX_PREFIX.source + Regexp.escape(value)) + sig { params(value: T.any(String, Regexp)).returns(Regexp) } + def self.path_regex(value) + value = case value + when String + Regexp.escape(value) + when Regexp + value.source + end + Regexp.new(RELOCATABLE_PATH_REGEX_PREFIX.source + value) end end