Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Optimize some regex matching (mastodon#15528)
Browse files Browse the repository at this point in the history
* Use Regex#match?

* Replace =~ too

* Avoid to call match? from Nil

* Keep value of Regexp.last_match
  • Loading branch information
007lva authored Jan 22, 2021
1 parent 7d0031a commit eb51e43
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions app/lib/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ module Extractor

# :yields: username, list_slug, start, end
def extract_mentions_or_lists_with_indices(text)
return [] unless text =~ Twitter::Regex[:at_signs]
return [] unless Twitter::Regex[:at_signs].match?(text)

possible_entries = []

text.to_s.scan(Account::MENTION_RE) do |screen_name, _|
match_data = $LAST_MATCH_INFO
after = $'
unless after =~ Twitter::Regex[:end_mention_match]
unless Twitter::Regex[:end_mention_match].match?(after)
start_position = match_data.char_begin(1) - 1
end_position = match_data.char_end(1)
possible_entries << {
Expand All @@ -33,15 +33,15 @@ def extract_mentions_or_lists_with_indices(text)
end

def extract_hashtags_with_indices(text, **)
return [] unless text =~ /#/
return [] unless /#/.match?(text)

tags = []
text.scan(Tag::HASHTAG_RE) do |hash_text, _|
match_data = $LAST_MATCH_INFO
start_position = match_data.char_begin(1) - 1
end_position = match_data.char_end(1)
after = $'
if after =~ %r{\A://}
if %r{\A://}.match?(after)
hash_text.match(/(.+)(https?\Z)/) do |matched|
hash_text = matched[1]
end_position -= matched[2].char_length
Expand Down
6 changes: 3 additions & 3 deletions app/lib/feed_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ def phrase_filtered?(status, receiver_id, context)

active_filters.map! do |filter|
if filter.whole_word
sb = filter.phrase =~ /\A[[:word:]]/ ? '\b' : ''
eb = filter.phrase =~ /[[:word:]]\z/ ? '\b' : ''
sb = /\A[[:word:]]/.match?(filter.phrase) ? '\b' : ''
eb = /[[:word:]]\z/.match?(filter.phrase) ? '\b' : ''

/(?mix:#{sb}#{Regexp.escape(filter.phrase)}#{eb})/
else
Expand All @@ -417,7 +417,7 @@ def phrase_filtered?(status, receiver_id, context)
status.media_attachments.map(&:description).join("\n\n"),
].compact.join("\n\n")

!combined_regex.match(combined_text).nil?
combined_regex.match?(combined_text)
end

# Adds a status to an account's feed, returning true if a status was
Expand Down
2 changes: 1 addition & 1 deletion app/lib/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def utf8_friendly_extractor(text, options = {})

escaped = text.chars.map do |c|
output = begin
if c.ord.to_s(16).length > 2 && UNICODE_ESCAPE_BLACKLIST_RE.match(c).nil?
if c.ord.to_s(16).length > 2 && !UNICODE_ESCAPE_BLACKLIST_RE.match?(c)
CGI.escape(c)
else
c
Expand Down
2 changes: 1 addition & 1 deletion app/lib/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def use_proxy?
end

def block_hidden_service?
!Rails.configuration.x.access_to_hidden_service && /\.(onion|i2p)$/.match(@url.host)
!Rails.configuration.x.access_to_hidden_service && /\.(onion|i2p)$/.match?(@url.host)
end

module ClientLimit
Expand Down
6 changes: 3 additions & 3 deletions app/lib/sanitize_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ module Config
return unless class_list

class_list.keep_if do |e|
next true if e =~ /^(h|p|u|dt|e)-/ # microformats classes
next true if e =~ /^(mention|hashtag)$/ # semantic classes
next true if e =~ /^(ellipsis|invisible)$/ # link formatting classes
next true if /^(h|p|u|dt|e)-/.match?(e) # microformats classes
next true if /^(mention|hashtag)$/.match?(e) # semantic classes
next true if /^(ellipsis|invisible)$/.match?(e) # link formatting classes
end

node['class'] = class_list.join(' ')
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/omniauthable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def create_for_oauth(auth)

user = User.new(user_params_from_auth(email, auth))

user.account.avatar_remote_url = auth.info.image if auth.info.image =~ /\A#{URI::DEFAULT_PARSER.make_regexp(%w(http https))}\z/
user.account.avatar_remote_url = auth.info.image if /\A#{URI::DEFAULT_PARSER.make_regexp(%w(http https))}\z/.match?(auth.info.image)
user.skip_confirmation!
user.save!
user
Expand Down
2 changes: 1 addition & 1 deletion app/services/account_search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def exact_match?
end

def username_complete?
query.include?('@') && "@#{query}" =~ /\A#{Account::MENTION_RE}\Z/
query.include?('@') && "@#{query}".match?(/\A#{Account::MENTION_RE}\Z/)
end

def likely_acct?
Expand Down
2 changes: 1 addition & 1 deletion app/services/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def default_results
end

def url_query?
@resolve && @query =~ /\Ahttps?:\/\//
@resolve && /\Ahttps?:\/\//.match?(@query)
end

def url_resource_results
Expand Down
2 changes: 1 addition & 1 deletion app/validators/blacklisted_email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def on_blacklist?
domains = Rails.configuration.x.email_domains_blacklist.gsub('.', '\.')
regexp = Regexp.new("@(.+\\.)?(#{domains})", true)

@email =~ regexp
regexp.match?(@email)
end

def not_on_whitelist?
Expand Down
2 changes: 1 addition & 1 deletion app/validators/html_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def validate_each(record, attribute, value)

def html_errors(str)
fragment = Nokogiri::HTML.fragment(options[:wrap_with] ? "<#{options[:wrap_with]}>#{str}</#{options[:wrap_with]}>" : str)
fragment.errors.select { |error| ERROR_RE =~ error.message }
fragment.errors.select { |error| ERROR_RE.match?(error.message) }
end
end
2 changes: 1 addition & 1 deletion config/initializers/open_uri_redirection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
module OpenURI
def self.redirectable?(uri1, uri2) # :nodoc:
uri1.scheme.casecmp(uri2.scheme).zero? ||
(/\A(?:http|https|ftp)\z/i =~ uri1.scheme && /\A(?:http|https|ftp)\z/i =~ uri2.scheme)
(/\A(?:http|https|ftp)\z/i.match?(uri1.scheme) && /\A(?:http|https|ftp)\z/i.match?(uri2.scheme))
end
end
2 changes: 1 addition & 1 deletion config/initializers/rack_attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def paging_request?
API_DELETE_STATUS_REGEX = /\A\/api\/v1\/statuses\/[\d]+/.freeze

throttle('throttle_api_delete', limit: 30, period: 30.minutes) do |req|
req.authenticated_user_id if (req.post? && req.path =~ API_DELETE_REBLOG_REGEX) || (req.delete? && req.path =~ API_DELETE_STATUS_REGEX)
req.authenticated_user_id if (req.post? && req.path.match?(API_DELETE_REBLOG_REGEX)) || (req.delete? && req.path.match?(API_DELETE_STATUS_REGEX))
end

throttle('throttle_sign_up_attempts/ip', limit: 25, period: 5.minutes) do |req|
Expand Down
2 changes: 1 addition & 1 deletion lib/mastodon/domains_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def crawl(start = nil)

work_unit = ->(domain) do
next if stats.key?(domain)
next if options[:exclude_suspended] && domain.match(blocked_domains)
next if options[:exclude_suspended] && domain.match?(blocked_domains)

stats[domain] = nil

Expand Down

0 comments on commit eb51e43

Please sign in to comment.