Skip to content

Instantly share code, notes, and snippets.

@zzak
Created June 17, 2015 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzak/2fe2ddfc5656b12fb48c to your computer and use it in GitHub Desktop.
Save zzak/2fe2ddfc5656b12fb48c to your computer and use it in GitHub Desktop.
--- mrbgem_dir_glob.rb 2015-06-17 12:55:13.000000000 -0400
+++ rbx_dir_glob.rb 2015-06-17 12:55:28.000000000 -0400
@@ -47,7 +47,7 @@
def call(env, parent)
path = path_join(parent, @name)
- if File.exists? path
+ if File.exist? path
env.matches << path
end
end
@@ -61,7 +61,7 @@
class RecursiveDirectories < Node
def call(env, start)
- return if !start || !File.exists?(start)
+ return if !start || !File.exist?(start)
# Even though the recursive entry is zero width
# in this case, its left separator is still the
@@ -85,7 +85,7 @@
while ent = dir.read
next if ent == "." || ent == ".."
full = path_join(path, ent)
- stat = File.lstat full
+ stat = File::Stat.lstat full
if stat.directory? and (allow_dots or ent.getbyte(0) != 46) # ?.
stack << full
@@ -119,7 +119,7 @@
dir = Dir.new(".")
while ent = dir.read
next if ent == "." || ent == ".."
- stat = File.lstat ent
+ stat = File::Stat.lstat ent
if stat.directory? and (allow_dots or ent.getbyte(0) != 46) # ?.
stack << ent
@@ -134,7 +134,7 @@
while ent = dir.read
next if ent == "." || ent == ".."
full = path_join(path, ent)
- stat = File.lstat full
+ stat = File::Stat.lstat full
if stat.directory? and ent.getbyte(0) != 46 # ?.
stack << full
@@ -165,7 +165,7 @@
end
def call(env, path)
- return if path and !File.exists?("#{path}/.")
+ return if path and !File.exist?("#{path}/.")
dir = Dir.new(path ? path : ".")
while ent = dir.read
@@ -183,7 +183,7 @@
class EntryMatch < Match
def call(env, path)
- return if path and !File.exists?("#{path}/.")
+ return if path and !File.exist?("#{path}/.")
begin
dir = Dir.new(path ? path : ".")
@@ -202,7 +202,7 @@
class DirectoriesOnly < Node
def call(env, path)
- if path and File.exists?("#{path}/.")
+ if path and File.exist?("#{path}/.")
env.matches << "#{path}/"
end
end
@@ -217,16 +217,17 @@
end
def self.path_split(str)
+ start = 0
ret = []
+
last_match = nil
- str2 = str.dup
- while match = str2.match(%r!/+!)
- cur_start, cur_end = match.begin(0), match.end(0)
- ret << str2.slice(0, cur_end - 1)
- ret << str2.slice(cur_start, cur_end - cur_start)
+ while match = %r!/+!.match_from(str, start)
+ cur_start, cur_end = match.full
+ ret << str.byteslice(start, cur_start - start)
+ ret << str.byteslice(cur_start, cur_end - cur_start)
- str2 = str2[cur_end..-1]
+ start = cur_end
last_match = match
end
@@ -272,7 +273,7 @@
last = RecursiveDirectories.new last, flags
end
elsif /^[^\*\?\]]+$/.match(dir)
- while parts[-2] && /^[^\*\?\]]+$/.match(parts[-2])
+ while /^[^\*\?\]]+$/.match(parts[-2])
next_sep = parts.pop
next_sect = parts.pop
@@ -298,15 +299,33 @@
env.matches
end
+ total = Rubinius::Config['glob.cache']
+
+ case total
+ when Fixnum
+ if total == 0
+ @glob_cache = nil
+ else
+ @glob_cache = Rubinius::LRUCache.new(total)
+ end
+ when false
+ @glob_cache = nil
+ else
+ @glob_cache = Rubinius::LRUCache.new(50)
+ end
+
+ def self.glob_cache
+ @glob_cache
+ end
+
def self.glob(pattern, flags, matches=[])
- # Rubygems typicall uses Dir[] as basicly a glorified File.exists?
- # to check for multiple extensions. So we went ahead and sped up
- # that specific case.
+ # Rubygems uses Dir[] as a glorified File.exist? to check for multiple
+ # extensions. So we went ahead and sped up that specific case.
if flags == 0 and
m = /^([a-zA-Z0-9_.\/\s]*[a-zA-Z0-9_.])(?:\{([^{}\/\*\?]*)\})?$/.match(pattern)
# no meta characters, so this is a glorified
- # File.exists? check. We allow for a brace expansion
+ # File.exist? check. We allow for a brace expansion
# only as a suffix.
if braces = m[2]
@@ -314,17 +333,17 @@
braces.split(",").each do |s|
path = "#{stem}#{s}"
- if File.exists? path
+ if File.exist? path
matches << path
end
end
# Split strips an empty closing part, so we need to add it back in
if braces.getbyte(-1) == 44 # ?,
- matches << stem if File.exists? stem
+ matches << stem if File.exist? stem
end
else
- matches << pattern if File.exists?(pattern)
+ matches << pattern if File.exist?(pattern)
end
return matches
@@ -332,16 +351,27 @@
ec_key = nil
+ if gc = @glob_cache
+ ec_key = [pattern, flags]
+ if patterns = gc.retrieve(ec_key)
+ patterns.each do |node|
+ run node, matches
+ end
+
+ return matches
+ end
+ end
+
if pattern.include? "{"
patterns = compile(pattern, flags)
- gc[ec_key] = patterns if ec_key
+ gc.set ec_key, patterns if ec_key
patterns.each do |node|
run node, matches
end
elsif node = single_compile(pattern, flags)
- gc[ec_key] = [node] if ec_key
+ gc.set ec_key, [node] if ec_key
run node, matches
else
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment