Commit 7eb17735 authored by Samuel Giddins's avatar Samuel Giddins

[PathList] Avoid intermediary array allocations

parent 3e27274c
require 'active_support/multibyte/unicode' require 'active_support/multibyte/unicode'
require 'find'
module Pod module Pod
class Sandbox class Sandbox
...@@ -50,23 +51,23 @@ module Pod ...@@ -50,23 +51,23 @@ module Pod
unless root.exist? unless root.exist?
raise Informative, "Attempt to read non existent folder `#{root}`." raise Informative, "Attempt to read non existent folder `#{root}`."
end end
escaped_root = escape_path_for_glob(root)
absolute_paths = Pathname.glob(escaped_root + '**/*', File::FNM_DOTMATCH).lazy
dirs_and_files = absolute_paths.reject { |path| path.basename.to_s =~ /^\.\.?$/ }
dirs, files = dirs_and_files.partition { |path| File.directory?(path) }
dirs = []
files = []
root_length = root.cleanpath.to_s.length + File::SEPARATOR.length root_length = root.cleanpath.to_s.length + File::SEPARATOR.length
sorted_relative_paths_from_full_paths = lambda do |paths| Find.find(root) do |f|
relative_paths = paths.lazy.map do |path| directory = File.directory?(f)
path_string = path.to_s f = f.slice(root_length, f.length - root_length)
path_string.slice(root_length, path_string.length - root_length) next if f.nil?
end
relative_paths.sort_by(&:upcase) (directory ? dirs : files) << f
end end
@dirs = sorted_relative_paths_from_full_paths.call(dirs) dirs.sort_by!(&:upcase)
@files = sorted_relative_paths_from_full_paths.call(files) files.sort_by!(&:upcase)
@dirs = dirs
@files = files
@glob_cache = {} @glob_cache = {}
end end
......
...@@ -175,10 +175,12 @@ module Pod ...@@ -175,10 +175,12 @@ module Pod
it 'orders paths case insensitively' do it 'orders paths case insensitively' do
root = fixture('banana-unordered') root = fixture('banana-unordered')
# Let Pathname.glob result be ordered case-sensitively # Let Find.find result be ordered case-sensitively
Pathname.stubs(:glob).returns([Pathname.new("#{root}/Classes/NSFetchRequest+Banana.h"), Find.stubs(:find).multiple_yields(
Pathname.new("#{root}/Classes/NSFetchedResultsController+Banana.h")]) "#{root}/Classes",
File.stubs(:directory?).returns(false) "#{root}/Classes/NSFetchRequest+Banana.h",
"#{root}/Classes/NSFetchedResultsController+Banana.h",
)
path_list = Sandbox::PathList.new(root) path_list = Sandbox::PathList.new(root)
path_list.files.should == %w(Classes/NSFetchedResultsController+Banana.h Classes/NSFetchRequest+Banana.h) path_list.files.should == %w(Classes/NSFetchedResultsController+Banana.h Classes/NSFetchRequest+Banana.h)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment