[#12164] patch for ext/gdbm — Koji Arai <JCA02266@...>

新井です。

24 messages 2001/02/04
[#12168] Re: patch for ext/gdbm — matz@... (Yukihiro Matsumoto) 2001/02/05

まつもと ゆきひろです

[#12176] Re: patch for ext/gdbm — Koji Arai <JCA02266@...> 2001/02/05

新井です。

[#12179] Re: patch for ext/gdbm — matz@... (Yukihiro Matsumoto) 2001/02/06

まつもと ゆきひろです

[#12219] Re: patch for ext/gdbm — Koji Arai <JCA02266@...> 2001/02/12

新井です。

[#12220] Re: patch for ext/gdbm — Koji Arai <JCA02266@...> 2001/02/12

新井です。

[#12256] set_trace_func — keiju@... (Keiju ISHITSUKA)

けいじゅ@日本ラショナルソフトウェアです.

15 messages 2001/02/17

[#12293] crash on proc without a block — Kenichi Komiya <kom@...1.accsnet.ne.jp>

15 messages 2001/02/25

[#12323] Re: [ruby-list:28364] class definition extension — "K.Kosako" <kosako@...>

ruby-listから移動しました。

13 messages 2001/02/28
[#12324] Re: [ruby-list:28364] class definition extension — matz@... (Yukihiro Matsumoto) 2001/02/28

まつもと ゆきひろです

[ruby-dev:12197] String#fnmatch

From: "Akinori MUSHA" <knu@...>
Date: 2001-02-08 18:35:55 UTC
List: ruby-dev #12197
 ちょっとしたユーティリティを書いていて、 String#fnmatch() が
欲しくなって書いてみたんですが、どんなもんでしょうか。

 使い方は、

	"README.txt".fnmatch("*.txt")		#=> true
	"Ruby".fnmatch("r*y")			#=> false
	"Ruby".fnmatch("r*y", FNM_NOCASE)	#=> true

のように fnmatch(3) そのままです。

 作るなら Dir::glob() との兼ね合いで Dir::fnmatch(pat, str) に
すべきかとも思ったんですが、純粋な文字列処理だし、 Dir でも File
でもないかな、と。

 ちなみに、 Ruby は自前で fnmatch() を用意しているので、プラット
フォームの違いを意識する必要はありません。 :>


 ユーザが指定した(排他)ファイルマスクとマッチするかを調べるのに
使おうと思っていますが、なかなか便利だと思います。賛同者求ム。

-- 
                     /
                    /__  __            Akinori.org / MUSHA.org
                   / )  )  ) )  /     FreeBSD.org / Ruby-lang.org
Akinori MUSHA aka / (_ /  ( (__(  @ iDaemons.org / and.or.jp

"We're only at home when we're on the run, on the wing, on the fly"

Index: dir.c
===================================================================
RCS file: /src/ruby/dir.c,v
retrieving revision 1.25
diff -u -r1.25 dir.c
--- dir.c	2001/02/08 09:19:16	1.25
+++ dir.c	2001/02/08 18:04:52
@@ -61,14 +61,6 @@
 
 #include <ctype.h>
 
-#define FNM_NOESCAPE	0x01
-#define FNM_PATHNAME	0x02
-#define FNM_PERIOD	0x04
-#define FNM_NOCASE	0x08
-
-#define FNM_NOMATCH	1
-#define FNM_ERROR	2
-
 #define downcase(c) (nocase && ISUPPER(c) ? tolower(c) : (c))
 
 #if defined DOSISH
@@ -128,8 +120,8 @@
 
 #define PERIOD(s) (period && *(s) == '.' && \
 		  ((s) == string || pathname && isdirsep(*(s))))
-static int
-fnmatch(pat, string, flags)
+int
+rb_fnmatch(pat, string, flags)
     char *pat;
     char *string;
     int flags;
@@ -174,7 +166,7 @@
 	    pat--;
 	    while (*s) {
 		if ((c == '[' || downcase(*s) == test) &&
-		    !fnmatch(pat, s, flags & ~FNM_PERIOD))
+		    !rb_fnmatch(pat, s, flags & ~FNM_PERIOD))
 		    return 0;
 		else if (pathname && isdirsep(*s))
 		    break;
@@ -618,7 +610,7 @@
 		    free(buf);
 		    continue;
 		}
-		if (fnmatch(magic, dp->d_name, flag) == 0) {
+		if (rb_fnmatch(magic, dp->d_name, flag) == 0) {
 		    buf = ALLOC_N(char, strlen(base)+NAMLEN(dp)+2);
 		    sprintf(buf, "%s%s%s", base, (BASE)?"/":"", dp->d_name);
 		    if (!m) {
Index: intern.h
===================================================================
RCS file: /src/ruby/intern.h,v
retrieving revision 1.39
diff -u -r1.39 intern.h
--- intern.h	2001/02/02 11:38:10	1.39
+++ intern.h	2001/02/08 18:04:52
@@ -97,6 +97,15 @@
 void rb_define_singleton_method _((VALUE,const char*,VALUE(*)(),int));
 void rb_define_private_method _((VALUE,const char*,VALUE(*)(),int));
 VALUE rb_singleton_class _((VALUE));
+/* dir.c */
+#define FNM_NOESCAPE	0x01
+#define FNM_PATHNAME	0x02
+#define FNM_PERIOD	0x04
+#define FNM_NOCASE	0x08
+
+#define FNM_NOMATCH	1
+#define FNM_ERROR	2
+int rb_fnmatch _((char*, char*, int));
 /* enum.c */
 VALUE rb_enum_length _((VALUE));
 /* error.c */
Index: string.c
===================================================================
RCS file: /src/ruby/string.c,v
retrieving revision 1.57
diff -u -r1.57 string.c
--- string.c	2001/02/08 09:19:17	1.57
+++ string.c	2001/02/08 18:04:53
@@ -1351,6 +1351,29 @@
 }
 
 static VALUE
+rb_str_fnmatch(argc, argv, str)
+    int argc;
+    VALUE *argv;
+    VALUE str;
+{
+    VALUE pattern;
+    VALUE flags;
+    int f;
+
+    if (rb_scan_args(argc, argv, "11", &pattern, &flags) == 2) {
+	f = NUM2INT(flags);
+    }
+    else {
+	f = 0;
+    }
+
+    if (rb_fnmatch(RSTRING(pattern)->ptr, RSTRING(str)->ptr, f) == 0)
+	return Qtrue;
+
+    return Qfalse;
+}
+
+static VALUE
 uscore_get()
 {
     VALUE line;
@@ -2812,6 +2835,12 @@
     rb_define_method(rb_cString, "index", rb_str_index_m, -1);
     rb_define_method(rb_cString, "rindex", rb_str_rindex, -1);
     rb_define_method(rb_cString, "replace", rb_str_replace_m, 1);
+    rb_define_method(rb_cString, "fnmatch", rb_str_fnmatch, -1);
+
+    rb_define_const(rb_cString, "FNM_NOESCAPE", INT2FIX(FNM_NOESCAPE));
+    rb_define_const(rb_cString, "FNM_PATHNAME", INT2FIX(FNM_PATHNAME));
+    rb_define_const(rb_cString, "FNM_PERIOD", INT2FIX(FNM_PERIOD));
+    rb_define_const(rb_cString, "FNM_NOCASE", INT2FIX(FNM_NOCASE));
 
     rb_define_method(rb_cString, "to_i", rb_str_to_i, 0);
     rb_define_method(rb_cString, "to_f", rb_str_to_f, 0);

In This Thread

Prev Next