Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization: Use match? instead of match #7003

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/linguist/heuristics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def matches?(filename, candidates)
# Internal: Perform the heuristic
def call(data)
matched = @rules.find do |rule|
rule['pattern'].match(data)
rule['pattern'].match?(data)
end
if !matched.nil?
languages = matched['language']
Expand All @@ -145,14 +145,14 @@ def initialize(pats)
@pats = pats
end

def match(input)
return !@pats.any? { |pat| !pat.match(input) }
def match?(input)
return @pats.all? { |pat| pat.match?(input) }
end

end

class AlwaysMatch
def match(input)
def match?(input)
return true
end
end
Expand All @@ -163,8 +163,8 @@ def initialize(pat)
@pat = pat
end

def match(input)
return !@pat.match(input)
def match?(input)
return !@pat.match?(input)
end

end
Expand Down
2 changes: 1 addition & 1 deletion test/test_heuristics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_symlink_empty
def test_no_match_if_regexp_timeout
skip("This test requires Ruby 3.2.0 or later") if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.2.0')

Regexp.any_instance.stubs(:match).raises(Regexp::TimeoutError)
Regexp.any_instance.stubs(:match?).raises(Regexp::TimeoutError)
assert_equal [], Heuristics.call(file_blob("#{fixtures_path}/Generic/stl/STL/cube1.stl"), [Language["STL"]])
end

Expand Down
Loading