about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
authorluigi <007.lva@gmail.com>2021-01-22 04:09:08 -0500
committerGitHub <noreply@github.com>2021-01-22 10:09:08 +0100
commiteb51e43fb4386120f77f2ff99581f15018a81bd4 (patch)
treeac52a3d084a3eafc1a1943be8ab2393465abc1ec /app
parent7d0031a515a9ccd552fab9ad55b6edb7e0e5ba32 (diff)
Optimize some regex matching (#15528)
* Use Regex#match?

* Replace =~ too

* Avoid to call match? from Nil

* Keep value of Regexp.last_match
Diffstat (limited to 'app')
-rw-r--r--app/lib/extractor.rb8
-rw-r--r--app/lib/feed_manager.rb6
-rw-r--r--app/lib/formatter.rb2
-rw-r--r--app/lib/request.rb2
-rw-r--r--app/lib/sanitize_config.rb6
-rw-r--r--app/models/concerns/omniauthable.rb2
-rw-r--r--app/services/account_search_service.rb2
-rw-r--r--app/services/search_service.rb2
-rw-r--r--app/validators/blacklisted_email_validator.rb2
-rw-r--r--app/validators/html_validator.rb2
10 files changed, 17 insertions, 17 deletions
diff --git a/app/lib/extractor.rb b/app/lib/extractor.rb
index 479689d60..6076458ad 100644
--- a/app/lib/extractor.rb
+++ b/app/lib/extractor.rb
@@ -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 << {
@@ -33,7 +33,7 @@ module Extractor
   end
 
   def extract_hashtags_with_indices(text, **)
-    return [] unless text =~ /#/
+    return [] unless /#/.match?(text)
 
     tags = []
     text.scan(Tag::HASHTAG_RE) do |hash_text, _|
@@ -41,7 +41,7 @@ module Extractor
       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
diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb
index f0ad3e21f..165338437 100644
--- a/app/lib/feed_manager.rb
+++ b/app/lib/feed_manager.rb
@@ -396,8 +396,8 @@ class FeedManager
 
     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
@@ -417,7 +417,7 @@ class FeedManager
       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
diff --git a/app/lib/formatter.rb b/app/lib/formatter.rb
index 7f217ae9f..24a34a059 100644
--- a/app/lib/formatter.rb
+++ b/app/lib/formatter.rb
@@ -222,7 +222,7 @@ class Formatter
 
     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
diff --git a/app/lib/request.rb b/app/lib/request.rb
index 38048dad7..125dee3ea 100644
--- a/app/lib/request.rb
+++ b/app/lib/request.rb
@@ -145,7 +145,7 @@ class Request
   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
diff --git a/app/lib/sanitize_config.rb b/app/lib/sanitize_config.rb
index 8f700197b..a2e1d9d01 100644
--- a/app/lib/sanitize_config.rb
+++ b/app/lib/sanitize_config.rb
@@ -28,9 +28,9 @@ class Sanitize
       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(' ')
diff --git a/app/models/concerns/omniauthable.rb b/app/models/concerns/omniauthable.rb
index 4ea219537..79d671d10 100644
--- a/app/models/concerns/omniauthable.rb
+++ b/app/models/concerns/omniauthable.rb
@@ -57,7 +57,7 @@ module Omniauthable
 
       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
diff --git a/app/services/account_search_service.rb b/app/services/account_search_service.rb
index 43e596040..6fe4b6593 100644
--- a/app/services/account_search_service.rb
+++ b/app/services/account_search_service.rb
@@ -175,7 +175,7 @@ class AccountSearchService < BaseService
   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?
diff --git a/app/services/search_service.rb b/app/services/search_service.rb
index 19500a8d4..1a76cbb38 100644
--- a/app/services/search_service.rb
+++ b/app/services/search_service.rb
@@ -72,7 +72,7 @@ class SearchService < BaseService
   end
 
   def url_query?
-    @resolve && @query =~ /\Ahttps?:\/\//
+    @resolve && /\Ahttps?:\/\//.match?(@query)
   end
 
   def url_resource_results
diff --git a/app/validators/blacklisted_email_validator.rb b/app/validators/blacklisted_email_validator.rb
index 16e3abf12..20a1587cc 100644
--- a/app/validators/blacklisted_email_validator.rb
+++ b/app/validators/blacklisted_email_validator.rb
@@ -22,7 +22,7 @@ class BlacklistedEmailValidator < ActiveModel::Validator
     domains = Rails.configuration.x.email_domains_blacklist.gsub('.', '\.')
     regexp  = Regexp.new("@(.+\\.)?(#{domains})", true)
 
-    @email =~ regexp
+    regexp.match?(@email)
   end
 
   def not_on_whitelist?
diff --git a/app/validators/html_validator.rb b/app/validators/html_validator.rb
index 1c9cd303c..b85b9769f 100644
--- a/app/validators/html_validator.rb
+++ b/app/validators/html_validator.rb
@@ -15,6 +15,6 @@ class HtmlValidator < ActiveModel::EachValidator
 
   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