Skip to content
Browse files

dir.c: FNM_EXTGLOB

* dir.c (file_s_fnmatch): match with expanding braces if FNM_EXTGLOB
  is set.  [ruby-core:40037] [Feature #5422]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37463 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information...
1 parent 7ba2f1a commit e59d5667c7769ffbe77f50687cb4393ca9280b2b @nobu nobu committed Nov 4, 2012
Showing with 48 additions and 4 deletions.
  1. +5 −0 ChangeLog
  2. +5 −0 NEWS
  3. +26 −3 dir.c
  4. +6 −1 test/ruby/envutil.rb
  5. +6 −0 test/ruby/test_fnmatch.rb
View
5 ChangeLog
@@ -1,3 +1,8 @@
+Sun Nov 4 10:19:03 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * dir.c (file_s_fnmatch): match with expanding braces if FNM_EXTGLOB
+ is set. [ruby-core:40037] [Feature #5422]
+
Sat Nov 3 23:38:15 2012 Tadayoshi Funaba <tadf@dotrb.org>
* complex.c: modified doc.
View
5 NEWS
@@ -31,6 +31,11 @@ with all sufficient information, see the ChangeLog file.
* aliased method:
* ENV.to_h is a new alias for ENV.to_hash
+ * File:
+ * extended method:
+ * File.fnmatch? now expands braces in the pattern if
+ File::FNM_EXTGLOB option is given.
+
* Hash
* added method:
* added Hash#to_h as explicit conversion method, like Array#to_a.
View
29 dir.c
@@ -85,6 +85,7 @@ char *strchr(char*,char);
#define FNM_PATHNAME 0x02
#define FNM_DOTMATCH 0x04
#define FNM_CASEFOLD 0x08
+#define FNM_EXTGLOB 0x10
#if CASEFOLD_FILESYSTEM
#define FNM_SYSCASE FNM_CASEFOLD
#else
@@ -1912,6 +1913,15 @@ dir_entries(int argc, VALUE *argv, VALUE io)
return rb_ensure(rb_Array, dir, dir_close, dir);
}
+static int
+fnmatch_brace(const char *pattern, VALUE val, void *enc)
+{
+ struct brace_args *arg = (struct brace_args *)val;
+ VALUE path = arg->value;
+
+ return (fnmatch(pattern, enc, RSTRING_PTR(path), arg->flags) == 0);
+}
+
/*
* call-seq:
* File.fnmatch( pattern, path, [flags] ) -> (true or false)
@@ -2008,9 +2018,21 @@ file_s_fnmatch(int argc, VALUE *argv, VALUE obj)
StringValue(pattern);
FilePathStringValue(path);
- if (fnmatch(RSTRING_PTR(pattern), rb_enc_get(pattern), RSTRING_PTR(path),
- flags) == 0)
- return Qtrue;
+ if (flags & FNM_EXTGLOB) {
+ struct brace_args args;
+
+ args.value = path;
+ args.flags = flags;
+ if (ruby_brace_expand(RSTRING_PTR(pattern), flags, fnmatch_brace,
+ (VALUE)&args, rb_enc_get(pattern)) > 0)
+ return Qtrue;
+ }
+ else {
+ if (fnmatch(RSTRING_PTR(pattern), rb_enc_get(pattern), RSTRING_PTR(path),
+ flags) == 0)
+ return Qtrue;
+ }
+ RB_GC_GUARD(pattern);
return Qfalse;
}
@@ -2111,5 +2133,6 @@ Init_Dir(void)
rb_file_const("FNM_PATHNAME", INT2FIX(FNM_PATHNAME));
rb_file_const("FNM_DOTMATCH", INT2FIX(FNM_DOTMATCH));
rb_file_const("FNM_CASEFOLD", INT2FIX(FNM_CASEFOLD));
+ rb_file_const("FNM_EXTGLOB", INT2FIX(FNM_EXTGLOB));
rb_file_const("FNM_SYSCASE", INT2FIX(FNM_SYSCASE));
}
View
7 test/ruby/envutil.rb
@@ -229,7 +229,7 @@ def assert_file
AssertFile
end
- class << (AssertFile = Object.new)
+ class << (AssertFile = Struct.new(:message).new)
include Assertions
def assert_file_predicate(predicate, *args)
if /\Anot_/ =~ predicate
@@ -241,9 +241,14 @@ def assert_file_predicate(predicate, *args)
mesg = "Expected file " << args.shift.inspect
mesg << mu_pp(args) unless args.empty?
mesg << "#{neg} to be #{predicate}"
+ mesg << " #{message}" if message
assert(result, mesg)
end
alias method_missing assert_file_predicate
+
+ def for(message)
+ clone.tap {|a| a.message = message}
+ end
end
end
end
View
6 test/ruby/test_fnmatch.rb
@@ -1,4 +1,5 @@
require 'test/unit'
+require_relative 'envutil'
class TestFnmatch < Test::Unit::TestCase
@@ -103,4 +104,9 @@ def test_fnmatch
assert(File.fnmatch('**/foo', 'c:/root/foo', File::FNM_PATHNAME))
end
+ def test_extglob
+ feature5422 = '[ruby-core:40037]'
+ assert_file.for(feature5422).not_fnmatch?( "{.g,t}*", ".gem")
+ assert_file.for(feature5422).fnmatch?("{.g,t}*", ".gem", File::FNM_EXTGLOB)
+ end
end

0 comments on commit e59d566

Please sign in to comment.
Something went wrong with that request. Please try again.