[前][次][番号順一覧][スレッド一覧]

ruby-changes:60675

From: Nobuyoshi <ko1@a...>
Date: Mon, 6 Apr 2020 22:22:45 +0900 (JST)
Subject: [ruby-changes:60675] 827e88119c (master): Moved `Dir.[]` to dir.rb

https://git.ruby-lang.org/ruby.git/commit/?id=827e88119c

From 827e88119ce9be18b491502a7b54c5c549eed2a9 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 25 Mar 2020 00:53:11 +0900
Subject: Moved `Dir.[]` to dir.rb


diff --git a/dir.c b/dir.c
index 685f955..339db2f 100644
--- a/dir.c
+++ b/dir.c
@@ -2882,18 +2882,19 @@ rb_push_glob(VALUE str, VALUE base, int flags) /* '\0' is delimiter */ https://github.com/ruby/ruby/blob/trunk/dir.c#L2882
 }
 
 static VALUE
-dir_globs(long argc, const VALUE *argv, VALUE base, int flags)
+dir_globs(VALUE args, VALUE base, int flags)
 {
     VALUE ary = rb_ary_new();
     long i;
 
-    for (i = 0; i < argc; ++i) {
+    for (i = 0; i < RARRAY_LEN(args); ++i) {
 	int status;
-	VALUE str = argv[i];
+	VALUE str = RARRAY_AREF(args, i);
 	FilePathValue(str);
 	status = push_glob(ary, str, base, flags);
 	if (status) GLOB_JUMP_TAG(status);
     }
+    RB_GC_GUARD(args);
 
     return ary;
 }
@@ -2920,41 +2921,15 @@ dir_glob_option_sort(VALUE sort) https://github.com/ruby/ruby/blob/trunk/dir.c#L2921
     return (sort ? 0 : FNM_GLOB_NOSORT);
 }
 
-static void
-dir_glob_options(VALUE opt, VALUE *base, int *sort, int *flags)
-{
-    static ID kw[3];
-    VALUE args[3];
-    if (!kw[0]) {
-        kw[0] = rb_intern_const("base");
-        kw[1] = rb_intern_const("sort");
-        kw[2] = rb_intern_const("flags");
-    }
-    rb_get_kwargs(opt, kw, 0, flags ? 3 : 2, args);
-    *base = dir_glob_option_base(args[0]);
-    if (sort && args[1] == Qfalse) *sort |= FNM_GLOB_NOSORT;
-    if (flags && args[2] != Qundef) *flags = NUM2INT(args[2]);
-}
-
-/*
- *  call-seq:
- *     Dir[ string [, string ...] [, base: path] [, sort: true] ] -> array
- *
- *  Equivalent to calling
- *  <code>Dir.glob([</code><i>string,...</i><code>], 0)</code>.
- *
- */
 static VALUE
-dir_s_aref(int argc, VALUE *argv, VALUE obj)
+dir_s_aref(rb_execution_context_t *ec, VALUE obj, VALUE args, VALUE base, VALUE sort)
 {
-    VALUE opts, base;
-    int sort = 0;
-    argc = rb_scan_args(argc, argv, "*:", NULL, &opts);
-    dir_glob_options(opts, &base, &sort, NULL);
-    if (argc == 1) {
-	return rb_push_glob(argv[0], base, sort);
+    const int flags = dir_glob_option_sort(sort);
+    base = dir_glob_option_base(base);
+    if (RARRAY_LEN(args) == 1) {
+	return rb_push_glob(RARRAY_AREF(args, 0), base, flags);
     }
-    return dir_globs(argc, argv, base, sort);
+    return dir_globs(args, base, flags);
 }
 
 static VALUE
@@ -2967,9 +2942,7 @@ dir_s_glob(rb_execution_context_t *ec, VALUE obj, VALUE str, VALUE rflags, VALUE https://github.com/ruby/ruby/blob/trunk/dir.c#L2942
 	ary = rb_push_glob(str, base, flags);
     }
     else {
-        VALUE v = rb_ary_replace(rb_ary_tmp_new(0), ary);
-	ary = dir_globs(RARRAY_LEN(v), RARRAY_CONST_PTR(v), base, flags);
-	RB_GC_GUARD(v);
+        ary = dir_globs(ary, base, flags);
     }
 
     if (rb_block_given_p()) {
@@ -3500,7 +3473,6 @@ Init_Dir(void) https://github.com/ruby/ruby/blob/trunk/dir.c#L3473
     rb_define_singleton_method(rb_cDir,"unlink", dir_s_rmdir, 1);
     rb_define_singleton_method(rb_cDir,"home", dir_s_home, -1);
 
-    rb_define_singleton_method(rb_cDir,"[]", dir_s_aref, -1);
     rb_define_singleton_method(rb_cDir,"exist?", rb_file_directory_p, 1);
     rb_define_singleton_method(rb_cDir,"exists?", rb_dir_exists_p, 1);
     rb_define_singleton_method(rb_cDir,"empty?", rb_dir_s_empty_p, 1);
diff --git a/dir.rb b/dir.rb
index 5784ce1..e8e846a 100644
--- a/dir.rb
+++ b/dir.rb
@@ -35,6 +35,14 @@ class Dir https://github.com/ruby/ruby/blob/trunk/dir.rb#L35
     __builtin_dir_initialize(name, encoding)
   end
 
+  #    Dir[ string [, string ...] [, base: path] [, sort: true] ] -> array
+  #
+  # Equivalent to calling
+  # <code>Dir.glob([</code><i>string,...</i><code>], 0)</code>.
+  def self.[](*args, base: nil, sort: true)
+    __builtin_dir_s_aref(args, base, sort)
+  end
+
   #    Dir.glob( pattern, [flags], [base: path] [, sort: true] )                       -> array
   #    Dir.glob( pattern, [flags], [base: path] [, sort: true] ) { |filename| block }  -> nil
   #
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]