From 2aef6785b25c5e8afb4d44cc480a3e3495d7852a Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 7 May 2024 00:22:22 +0900 Subject: [PATCH] Use `require_relative` in the Regexp::Parser codebase If there are many searches in the `$LOAD_PATH` in the user environment, require will perform unnecessary searches that are not needed. In contrast, `require_relative` is efficient because it uses a relative path. --- lib/regexp_parser.rb | 12 ++-- lib/regexp_parser/expression.rb | 68 +++++++++---------- lib/regexp_parser/parser.rb | 4 +- .../scanner/errors/scanner_error.rb | 2 +- lib/regexp_parser/scanner/scanner.rl | 6 +- lib/regexp_parser/syntax.rb | 2 +- lib/regexp_parser/syntax/token.rb | 26 +++---- lib/regexp_parser/syntax/versions.rb | 2 +- 8 files changed, 61 insertions(+), 61 deletions(-) diff --git a/lib/regexp_parser.rb b/lib/regexp_parser.rb index 4e68b04..a32396d 100644 --- a/lib/regexp_parser.rb +++ b/lib/regexp_parser.rb @@ -1,6 +1,6 @@ -require 'regexp_parser/version' -require 'regexp_parser/token' -require 'regexp_parser/scanner' -require 'regexp_parser/syntax' -require 'regexp_parser/lexer' -require 'regexp_parser/parser' +require_relative 'regexp_parser/version' +require_relative 'regexp_parser/token' +require_relative 'regexp_parser/scanner' +require_relative 'regexp_parser/syntax' +require_relative 'regexp_parser/lexer' +require_relative 'regexp_parser/parser' diff --git a/lib/regexp_parser/expression.rb b/lib/regexp_parser/expression.rb index a383ea3..e865e43 100644 --- a/lib/regexp_parser/expression.rb +++ b/lib/regexp_parser/expression.rb @@ -1,37 +1,37 @@ -require 'regexp_parser/error' +require_relative 'error' -require 'regexp_parser/expression/shared' -require 'regexp_parser/expression/base' -require 'regexp_parser/expression/quantifier' -require 'regexp_parser/expression/subexpression' -require 'regexp_parser/expression/sequence' -require 'regexp_parser/expression/sequence_operation' +require_relative 'expression/shared' +require_relative 'expression/base' +require_relative 'expression/quantifier' +require_relative 'expression/subexpression' +require_relative 'expression/sequence' +require_relative 'expression/sequence_operation' -require 'regexp_parser/expression/classes/alternation' -require 'regexp_parser/expression/classes/anchor' -require 'regexp_parser/expression/classes/backreference' -require 'regexp_parser/expression/classes/character_set' -require 'regexp_parser/expression/classes/character_set/intersection' -require 'regexp_parser/expression/classes/character_set/range' -require 'regexp_parser/expression/classes/character_type' -require 'regexp_parser/expression/classes/conditional' -require 'regexp_parser/expression/classes/escape_sequence' -require 'regexp_parser/expression/classes/free_space' -require 'regexp_parser/expression/classes/group' -require 'regexp_parser/expression/classes/keep' -require 'regexp_parser/expression/classes/literal' -require 'regexp_parser/expression/classes/posix_class' -require 'regexp_parser/expression/classes/root' -require 'regexp_parser/expression/classes/unicode_property' +require_relative 'expression/classes/alternation' +require_relative 'expression/classes/anchor' +require_relative 'expression/classes/backreference' +require_relative 'expression/classes/character_set' +require_relative 'expression/classes/character_set/intersection' +require_relative 'expression/classes/character_set/range' +require_relative 'expression/classes/character_type' +require_relative 'expression/classes/conditional' +require_relative 'expression/classes/escape_sequence' +require_relative 'expression/classes/free_space' +require_relative 'expression/classes/group' +require_relative 'expression/classes/keep' +require_relative 'expression/classes/literal' +require_relative 'expression/classes/posix_class' +require_relative 'expression/classes/root' +require_relative 'expression/classes/unicode_property' -require 'regexp_parser/expression/methods/construct' -require 'regexp_parser/expression/methods/human_name' -require 'regexp_parser/expression/methods/match' -require 'regexp_parser/expression/methods/match_length' -require 'regexp_parser/expression/methods/negative' -require 'regexp_parser/expression/methods/options' -require 'regexp_parser/expression/methods/parts' -require 'regexp_parser/expression/methods/printing' -require 'regexp_parser/expression/methods/strfregexp' -require 'regexp_parser/expression/methods/tests' -require 'regexp_parser/expression/methods/traverse' +require_relative 'expression/methods/construct' +require_relative 'expression/methods/human_name' +require_relative 'expression/methods/match' +require_relative 'expression/methods/match_length' +require_relative 'expression/methods/negative' +require_relative 'expression/methods/options' +require_relative 'expression/methods/parts' +require_relative 'expression/methods/printing' +require_relative 'expression/methods/strfregexp' +require_relative 'expression/methods/tests' +require_relative 'expression/methods/traverse' diff --git a/lib/regexp_parser/parser.rb b/lib/regexp_parser/parser.rb index 91154f5..2813696 100644 --- a/lib/regexp_parser/parser.rb +++ b/lib/regexp_parser/parser.rb @@ -1,5 +1,5 @@ -require 'regexp_parser/error' -require 'regexp_parser/expression' +require_relative 'error' +require_relative 'expression' class Regexp::Parser include Regexp::Expression diff --git a/lib/regexp_parser/scanner/errors/scanner_error.rb b/lib/regexp_parser/scanner/errors/scanner_error.rb index 99c4cc1..be0fb26 100644 --- a/lib/regexp_parser/scanner/errors/scanner_error.rb +++ b/lib/regexp_parser/scanner/errors/scanner_error.rb @@ -1,4 +1,4 @@ -require 'regexp_parser/error' +require_relative '../../../regexp_parser/error' class Regexp::Scanner # General scanner error (catch all) diff --git a/lib/regexp_parser/scanner/scanner.rl b/lib/regexp_parser/scanner/scanner.rl index e5bce69..42ae78c 100644 --- a/lib/regexp_parser/scanner/scanner.rl +++ b/lib/regexp_parser/scanner/scanner.rl @@ -640,9 +640,9 @@ *|; }%% -require 'regexp_parser/scanner/errors/scanner_error' -require 'regexp_parser/scanner/errors/premature_end_error' -require 'regexp_parser/scanner/errors/validation_error' +require_relative 'scanner/errors/scanner_error' +require_relative 'scanner/errors/premature_end_error' +require_relative 'scanner/errors/validation_error' class Regexp::Scanner # Scans the given regular expression text, or Regexp object and collects the diff --git a/lib/regexp_parser/syntax.rb b/lib/regexp_parser/syntax.rb index e7ebd10..735316a 100644 --- a/lib/regexp_parser/syntax.rb +++ b/lib/regexp_parser/syntax.rb @@ -1,4 +1,4 @@ -require 'regexp_parser/error' +require_relative 'error' module Regexp::Syntax class SyntaxError < Regexp::Parser::Error; end diff --git a/lib/regexp_parser/syntax/token.rb b/lib/regexp_parser/syntax/token.rb index fd6389a..63f59c0 100644 --- a/lib/regexp_parser/syntax/token.rb +++ b/lib/regexp_parser/syntax/token.rb @@ -20,19 +20,19 @@ module FreeSpace # Load all the token files, they will populate the Map constant. -require 'regexp_parser/syntax/token/anchor' -require 'regexp_parser/syntax/token/assertion' -require 'regexp_parser/syntax/token/backreference' -require 'regexp_parser/syntax/token/posix_class' -require 'regexp_parser/syntax/token/character_set' -require 'regexp_parser/syntax/token/character_type' -require 'regexp_parser/syntax/token/conditional' -require 'regexp_parser/syntax/token/escape' -require 'regexp_parser/syntax/token/group' -require 'regexp_parser/syntax/token/keep' -require 'regexp_parser/syntax/token/meta' -require 'regexp_parser/syntax/token/quantifier' -require 'regexp_parser/syntax/token/unicode_property' +require_relative '../syntax/token/anchor' +require_relative '../syntax/token/assertion' +require_relative '../syntax/token/backreference' +require_relative '../syntax/token/posix_class' +require_relative '../syntax/token/character_set' +require_relative '../syntax/token/character_type' +require_relative '../syntax/token/conditional' +require_relative '../syntax/token/escape' +require_relative '../syntax/token/group' +require_relative '../syntax/token/keep' +require_relative '../syntax/token/meta' +require_relative '../syntax/token/quantifier' +require_relative '../syntax/token/unicode_property' # After loading all the tokens the map is full. Extract all tokens and types diff --git a/lib/regexp_parser/syntax/versions.rb b/lib/regexp_parser/syntax/versions.rb index 3ee37e7..35d1af3 100644 --- a/lib/regexp_parser/syntax/versions.rb +++ b/lib/regexp_parser/syntax/versions.rb @@ -3,6 +3,6 @@ # # Aliases for the latest patch version are provided as 'ruby/n.n', # e.g. 'ruby/1.9' refers to Ruby v1.9.3. -Dir[File.expand_path('../versions/*.rb', __FILE__)].sort.each { |f| require f } +Dir[File.expand_path('../versions/*.rb', __FILE__)].sort.each { |f| require_relative f } Regexp::Syntax::CURRENT = Regexp::Syntax.for("ruby/#{RUBY_VERSION}")