os/mac/version: use named captures in regex

This commit is contained in:
Seeker 2021-01-18 10:26:31 -08:00
parent 5286be91cb
commit 266aba486e

View File

@ -37,13 +37,17 @@ module OS
def self.version_arch(value) def self.version_arch(value)
@all_archs_regex ||= begin @all_archs_regex ||= begin
all_archs = Hardware::CPU::ALL_ARCHS.map(&:to_s) all_archs = Hardware::CPU::ALL_ARCHS.map(&:to_s)
/^((#{Regexp.union(all_archs)})_)?([\w.]+)(-(#{Regexp.union(all_archs)}))?$/ /
^((?<prefix_arch>#{Regexp.union(all_archs)})_)?
(?<version>[\w.]+)
(-(?<suffix_arch>#{Regexp.union(all_archs)}))?$
/x
end end
match = @all_archs_regex.match(value) match = @all_archs_regex.match(value)
return [] unless match return [] unless match
version = match[3] version = match[:version]
arch = match[2] || match[5] arch = match[:prefix_arch] || match[:suffix_arch]
[version, arch] [version, arch]
end end