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

[PathList] Avoid intermediary array allocations

parent 3e27274c
require 'active_support/multibyte/unicode'
require 'find'
module Pod
class Sandbox
......@@ -50,23 +51,23 @@ module Pod
unless root.exist?
raise Informative, "Attempt to read non existent folder `#{root}`."
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
sorted_relative_paths_from_full_paths = lambda do |paths|
relative_paths = paths.lazy.map do |path|
path_string = path.to_s
path_string.slice(root_length, path_string.length - root_length)
end
relative_paths.sort_by(&:upcase)
Find.find(root) do |f|
directory = File.directory?(f)
f = f.slice(root_length, f.length - root_length)
next if f.nil?
(directory ? dirs : files) << f
end
@dirs = sorted_relative_paths_from_full_paths.call(dirs)
@files = sorted_relative_paths_from_full_paths.call(files)
dirs.sort_by!(&:upcase)
files.sort_by!(&:upcase)
@dirs = dirs
@files = files
@glob_cache = {}
end
......
......@@ -175,10 +175,12 @@ module Pod
it 'orders paths case insensitively' do
root = fixture('banana-unordered')
# Let Pathname.glob result be ordered case-sensitively
Pathname.stubs(:glob).returns([Pathname.new("#{root}/Classes/NSFetchRequest+Banana.h"),
Pathname.new("#{root}/Classes/NSFetchedResultsController+Banana.h")])
File.stubs(:directory?).returns(false)
# Let Find.find result be ordered case-sensitively
Find.stubs(:find).multiple_yields(
"#{root}/Classes",
"#{root}/Classes/NSFetchRequest+Banana.h",
"#{root}/Classes/NSFetchedResultsController+Banana.h",
)
path_list = Sandbox::PathList.new(root)
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