From: Akinori MUSHA Date: 2001-06-10T22:18:54+09:00 Subject: [ruby-dev:13535] File::fnmatch to go  そろそろ File::fnmatch の件を決着させたいので最終提案です。  すばり File::fnmatch と File::FNM_* 定数をそのまま提供します。 fnmatch に ? を付けたのと付けないのは両方あっていいと思いました。 FNM_NOCASE -> FNM_CASEFOLD というのは、 glibc や *BSD の libc が そうなっているからという理由です。(  Dir::glob にオプションを与えられるようにする件は、とりあえず File::fnmatch があればしのげるので、何か理由が見つかるまでは 凍結します。 def Dir.glob2(pattern, flags = File::FNM_PERIOD) if block_given? Dir.glob(".* *") { |f| yield f if File.fnmatch?(pattern, f, flags) } else Dir.glob(".* *").select { |f| File.fnmatch?(pattern, f, flags) } end end  1.7 系に入れてしまってよいでしょうか? -- / /__ __ Akinori.org / MUSHA.org / ) ) ) ) / FreeBSD.org / Ruby-lang.org Akinori MUSHA aka / (_ / ( (__( @ iDaemons.org / and.or.jp "Freeze this moment a little bit longer, make each impression a little bit stronger.. Experience slips away -- Time stand still" Index: dir.c =================================================================== RCS file: /src/ruby/dir.c,v retrieving revision 1.47 diff -u -u -r1.47 dir.c --- dir.c 2001/05/27 17:24:24 1.47 +++ dir.c 2001/06/10 13:15:31 @@ -68,7 +68,7 @@ #define FNM_NOESCAPE 0x01 #define FNM_PATHNAME 0x02 #define FNM_PERIOD 0x04 -#define FNM_NOCASE 0x08 +#define FNM_CASEFOLD 0x08 #define FNM_NOMATCH 1 #define FNM_ERROR 2 @@ -100,7 +100,7 @@ int flags; { int not, ok = 0; - int nocase = flags & FNM_NOCASE; + int nocase = flags & FNM_CASEFOLD; int escape = !(flags & FNM_NOESCAPE); not = *pat == '!' || *pat == '^'; @@ -145,7 +145,7 @@ int escape = !(flags & FNM_NOESCAPE); int pathname = flags & FNM_PATHNAME; int period = flags & FNM_PERIOD; - int nocase = flags & FNM_NOCASE; + int nocase = flags & FNM_CASEFOLD; while (c = *pat++) { switch (c) { @@ -764,7 +764,7 @@ void (*func)(); VALUE arg; { - glob_helper(path, FNM_PERIOD|FNM_NOCASE, func, arg); + glob_helper(path, FNM_PERIOD|FNM_CASEFOLD, func, arg); } static void push_pattern _((const char *path, VALUE ary)); @@ -921,6 +921,30 @@ return rb_ensure(rb_Array, dir, dir_close, dir); } +static VALUE +file_s_fnmatch(argc, argv, obj) + int argc; + VALUE *argv; + VALUE obj; +{ + VALUE pattern, path; + VALUE rflags; + int flags; + + if (rb_scan_args(argc, argv, "21", &pattern, &path, &rflags) == 3) + flags = NUM2INT(rflags); + else + flags = 0; + + StringValue(pattern); + StringValue(path); + + if (fnmatch(RSTRING(pattern)->ptr, RSTRING(path)->ptr, flags) == 0) + return Qtrue; + + return Qfalse; +} + void Init_Dir() { @@ -954,4 +978,12 @@ rb_define_singleton_method(rb_cDir,"glob", dir_s_glob, 1); rb_define_singleton_method(rb_cDir,"[]", dir_s_glob, 1); + + rb_define_singleton_method(rb_cFile,"fnmatch", file_s_fnmatch, -1); + rb_define_singleton_method(rb_cFile,"fnmatch?", file_s_fnmatch, -1); + + rb_define_const(rb_cFile, "FNM_NOESCAPE", INT2FIX(FNM_NOESCAPE)); + rb_define_const(rb_cFile, "FNM_PATHNAME", INT2FIX(FNM_PATHNAME)); + rb_define_const(rb_cFile, "FNM_PERIOD", INT2FIX(FNM_PERIOD)); + rb_define_const(rb_cFile, "FNM_CASEFOLD", INT2FIX(FNM_CASEFOLD)); }