Skip to content

Commit

Permalink
String#end_with?('/') rather than == '/'
Browse files Browse the repository at this point in the history
  • Loading branch information
robotdana committed Jul 11, 2023
1 parent 34328e0 commit 9b70c4d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 6 deletions.
19 changes: 19 additions & 0 deletions bin/benchmark
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,25 @@ benchmark('bsearch-or-include') do
end
end

benchmark('add-trailing-slash') do
Benchmark.ips do |x|
x.config(config)
@string_slash = '/'
@string_trailing_slash = 'D:/'
@string_no_trailing_slash = '/bin'

x.report(:string_slash_end_with) { (@string_slash.end_with?('/') ? @string_slash : "#{@string_slash}/") + "str" }
x.report(:string_trailing_slash_end_with) { (@string_trailing_slash.end_with?('/') ? @string_trailing_slash : "#{@string_trailing_slash}/") + "str" }
x.report(:string_no_trailing_slash_end_with) { (@string_no_trailing_slash.end_with?('/') ? @string_no_trailing_slash : "#{@string_no_trailing_slash}/") + "str" }

x.report(:string_slash_file_join) { ::File.join(@string_slash, "str") }
x.report(:string_trailing_slash_file_join) { ::File.join(@string_trailing_slash, "str") }
x.report(:string_no_trailing_slash_file_join) { ::File.join(@string_no_trailing_slash, "str") }

x.compare!
end
end

benchmark('one-element-hash') do
Benchmark.ips do |x|
x.config(config)
Expand Down
2 changes: 1 addition & 1 deletion lib/path_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def each(root = '.', &block)
return self unless root_candidate.exists?
return self unless recursive_match?(root_candidate.parent, dir_matcher)

relative_root = root == '/' ? root : "#{root}/"
relative_root = root.end_with?('/') ? root : "#{root}/"

recursive_each(root_candidate, relative_root, dir_matcher, file_matcher, &block)

Expand Down
10 changes: 5 additions & 5 deletions lib/path_list/candidate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ def full_path_downcase
# the containing directory as a Candidate,
# or nil if this is already the root
def parent
puts "@full_path: #{@full_path}"
return if @full_path == '/'
puts "#{__FILE__}:#{__LINE__}, @full_path: #{@full_path}"
return if @full_path.end_with?('/') # '/' on unix X:/ on win

puts "::File.dirname(@full_path): #{::File.dirname(@full_path)}"
puts "#{__FILE__}:#{__LINE__}, ::File.dirname(@full_path): #{::File.dirname(@full_path)}"
self.class.new(::File.dirname(@full_path), true)
end

# @return [Array<Candidate>]
# the children of this as Candidates
def child_candidates
@child_candidates ||= begin
prepend_path = @full_path == '/' ? '' : @full_path
prepend_path = @full_path.end_with?('/') ? @full_path : "#{@full_path}/"

children.map { |filename| self.class.new("#{prepend_path}/#{filename}") }
children.map { |filename| self.class.new("#{prepend_path}#{filename}") }
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/path_list/matcher/exact_string/case_insensitive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def initialize(item, polarity)
# @param (see Matcher#match)
# @return (see Matcher#match)
def match(candidate)
puts "#{__FILE__}:#{__LINE__}, @item: #{@item}"
puts "#{__FILE__}:#{__LINE__}, candidate.full_path_downcase: #{candidate.full_path_downcase}"
return @polarity if @item == candidate.full_path_downcase
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/path_list/matcher/exact_string/set/case_insensitive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def initialize(set, polarity)
# @param (see Matcher#match)
# @return (see Matcher#match)
def match(candidate)
puts "#{__FILE__}:#{__LINE__}, @set: #{@set}"
puts "#{__FILE__}:#{__LINE__}, candidate.full_path_downcase: #{candidate.full_path_downcase}"
@polarity if @set.include?(candidate.full_path_downcase)
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/path_list/matcher/path_regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def self.build(regexp_tokens, polarity)
# @param (see Matcher#match)
# @return (see Matcher#match)
def match(candidate)
puts "#{__FILE__}:#{__LINE__}, @regexp: #{@regexp}"
puts "#{__FILE__}:#{__LINE__}, candidate.full_path: #{candidate.full_path}"
@polarity if @regexp.match?(candidate.full_path)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/path_list/matcher/path_regexp/case_insensitive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class CaseInsensitive < PathRegexp
# @param (see Matcher#match)
# @return (see Matcher#match)
def match(candidate)
puts "#{__FILE__}:#{__LINE__}, @regexp: #{@regexp}"
puts "#{__FILE__}:#{__LINE__}, candidate.full_path: #{candidate.full_path}"
@polarity if @regexp.match?(candidate.full_path_downcase)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/path_list/token_regexp/compress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class << self
# @param parts [Array<Symbol, String, EscapedString>]
# @return [Array<Symbol, String, EscapedString>]
def compress!(parts)
puts "#{__FILE__}:#{__LINE__}, parts (before): #{parts}"
loop do
next if compress_any!(parts)
next if compress_any_dir!(parts)
Expand All @@ -17,6 +18,7 @@ def compress!(parts)

break
end
puts "#{__FILE__}:#{__LINE__}, parts (after): #{parts}"
end

private
Expand Down

0 comments on commit 9b70c4d

Please sign in to comment.