1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
nobu 2012-11-04 01:19:11 +00:00
parent 7ba2f1a9dd
commit e59d5667c7
5 changed files with 48 additions and 4 deletions

View file

@ -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> Sat Nov 3 23:38:15 2012 Tadayoshi Funaba <tadf@dotrb.org>
* complex.c: modified doc. * complex.c: modified doc.

5
NEWS
View file

@ -31,6 +31,11 @@ with all sufficient information, see the ChangeLog file.
* aliased method: * aliased method:
* ENV.to_h is a new alias for ENV.to_hash * 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 * Hash
* added method: * added method:
* added Hash#to_h as explicit conversion method, like Array#to_a. * added Hash#to_h as explicit conversion method, like Array#to_a.

23
dir.c
View file

@ -85,6 +85,7 @@ char *strchr(char*,char);
#define FNM_PATHNAME 0x02 #define FNM_PATHNAME 0x02
#define FNM_DOTMATCH 0x04 #define FNM_DOTMATCH 0x04
#define FNM_CASEFOLD 0x08 #define FNM_CASEFOLD 0x08
#define FNM_EXTGLOB 0x10
#if CASEFOLD_FILESYSTEM #if CASEFOLD_FILESYSTEM
#define FNM_SYSCASE FNM_CASEFOLD #define FNM_SYSCASE FNM_CASEFOLD
#else #else
@ -1912,6 +1913,15 @@ dir_entries(int argc, VALUE *argv, VALUE io)
return rb_ensure(rb_Array, dir, dir_close, dir); 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: * call-seq:
* File.fnmatch( pattern, path, [flags] ) -> (true or false) * File.fnmatch( pattern, path, [flags] ) -> (true or false)
@ -2008,9 +2018,21 @@ file_s_fnmatch(int argc, VALUE *argv, VALUE obj)
StringValue(pattern); StringValue(pattern);
FilePathStringValue(path); FilePathStringValue(path);
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), if (fnmatch(RSTRING_PTR(pattern), rb_enc_get(pattern), RSTRING_PTR(path),
flags) == 0) flags) == 0)
return Qtrue; return Qtrue;
}
RB_GC_GUARD(pattern);
return Qfalse; return Qfalse;
} }
@ -2111,5 +2133,6 @@ Init_Dir(void)
rb_file_const("FNM_PATHNAME", INT2FIX(FNM_PATHNAME)); rb_file_const("FNM_PATHNAME", INT2FIX(FNM_PATHNAME));
rb_file_const("FNM_DOTMATCH", INT2FIX(FNM_DOTMATCH)); rb_file_const("FNM_DOTMATCH", INT2FIX(FNM_DOTMATCH));
rb_file_const("FNM_CASEFOLD", INT2FIX(FNM_CASEFOLD)); rb_file_const("FNM_CASEFOLD", INT2FIX(FNM_CASEFOLD));
rb_file_const("FNM_EXTGLOB", INT2FIX(FNM_EXTGLOB));
rb_file_const("FNM_SYSCASE", INT2FIX(FNM_SYSCASE)); rb_file_const("FNM_SYSCASE", INT2FIX(FNM_SYSCASE));
} }

View file

@ -229,7 +229,7 @@ module Test
AssertFile AssertFile
end end
class << (AssertFile = Object.new) class << (AssertFile = Struct.new(:message).new)
include Assertions include Assertions
def assert_file_predicate(predicate, *args) def assert_file_predicate(predicate, *args)
if /\Anot_/ =~ predicate if /\Anot_/ =~ predicate
@ -241,9 +241,14 @@ module Test
mesg = "Expected file " << args.shift.inspect mesg = "Expected file " << args.shift.inspect
mesg << mu_pp(args) unless args.empty? mesg << mu_pp(args) unless args.empty?
mesg << "#{neg} to be #{predicate}" mesg << "#{neg} to be #{predicate}"
mesg << " #{message}" if message
assert(result, mesg) assert(result, mesg)
end end
alias method_missing assert_file_predicate alias method_missing assert_file_predicate
def for(message)
clone.tap {|a| a.message = message}
end
end end
end end
end end

View file

@ -1,4 +1,5 @@
require 'test/unit' require 'test/unit'
require_relative 'envutil'
class TestFnmatch < Test::Unit::TestCase class TestFnmatch < Test::Unit::TestCase
@ -103,4 +104,9 @@ class TestFnmatch < Test::Unit::TestCase
assert(File.fnmatch('**/foo', 'c:/root/foo', File::FNM_PATHNAME)) assert(File.fnmatch('**/foo', 'c:/root/foo', File::FNM_PATHNAME))
end 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 end