Skip to content

Commit

Permalink
dir.c: FNM_EXTGLOB
Browse files Browse the repository at this point in the history
* 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
nobu committed Nov 4, 2012
1 parent 7ba2f1a commit e59d566
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 26 additions & 3 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
}
7 changes: 6 additions & 1 deletion test/ruby/envutil.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/ruby/test_fnmatch.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test/unit'
require_relative 'envutil'

class TestFnmatch < Test::Unit::TestCase

Expand Down Expand Up @@ -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.