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

ruby-changes:37663

From: nobu <ko1@a...>
Date: Wed, 25 Feb 2015 16:00:04 +0900 (JST)
Subject: [ruby-changes:37663] nobu:r49744 (trunk): dir.c: same encoding to the pattern

nobu	2015-02-25 15:59:39 +0900 (Wed, 25 Feb 2015)

  New Revision: 49744

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49744

  Log:
    dir.c: same encoding to the pattern
    
    * dir.c (push_pattern, push_glob): make globbed file names same
      encoding to the given pattern.

  Modified files:
    trunk/ChangeLog
    trunk/dir.c
    trunk/test/ruby/test_dir_m17n.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 49743)
+++ ChangeLog	(revision 49744)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Feb 25 15:59:35 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* dir.c (push_pattern, push_glob): make globbed file names same
+	  encoding to the given pattern.
+
 Wed Feb 25 15:27:16 2015  NARUSE, Yui  <naruse@r...>
 
 	* tool/merger.rb: support 2.1+ versioning scheme.
Index: dir.c
===================================================================
--- dir.c	(revision 49743)
+++ dir.c	(revision 49744)
@@ -1895,7 +1895,15 @@ rb_glob(const char *path, void (*func)(c https://github.com/ruby/ruby/blob/trunk/dir.c#L1895
 static void
 push_pattern(const char *path, VALUE ary, void *enc)
 {
-    rb_ary_push(ary, rb_external_str_new_with_enc(path, strlen(path), enc));
+#ifdef __APPLE__
+    VALUE name = rb_utf8_str_new_cstr(path);
+    rb_encoding *eenc = rb_default_internal_encoding();
+    OBJ_TAINT(name);
+    name = rb_str_conv_enc(name, NULL, eenc ? eenc : enc);
+#else
+    VALUE name = rb_external_str_new_with_enc(path, strlen(path), enc);
+#endif
+    rb_ary_push(ary, name);
 }
 
 static int
@@ -2000,19 +2008,19 @@ static int https://github.com/ruby/ruby/blob/trunk/dir.c#L2008
 push_glob(VALUE ary, VALUE str, int flags)
 {
     struct glob_args args;
-#ifdef __APPLE__
-    rb_encoding *enc = rb_utf8_encoding();
-
-    str = rb_str_encode_ospath(str);
-#else
     rb_encoding *enc = rb_enc_get(str);
 
+#ifdef __APPLE__
+    str = rb_str_encode_ospath(str);
+#endif
     if (enc == rb_usascii_encoding()) enc = rb_filesystem_encoding();
     if (enc == rb_usascii_encoding()) enc = rb_ascii8bit_encoding();
-#endif
     args.func = push_pattern;
     args.value = ary;
     args.enc = enc;
+#ifdef __APPLE__
+    enc = rb_utf8_encoding();
+#endif
 
     RB_GC_GUARD(str);
     return ruby_brace_glob0(RSTRING_PTR(str), flags | GLOB_VERBOSE,
Index: test/ruby/test_dir_m17n.rb
===================================================================
--- test/ruby/test_dir_m17n.rb	(revision 49743)
+++ test/ruby/test_dir_m17n.rb	(revision 49744)
@@ -362,6 +362,15 @@ class TestDir_M17N < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/ruby/test_dir_m17n.rb#L362
     end
   end
 
+  def test_glob_encoding
+    with_tmpdir do
+      %W"file_one.ext file_two.ext".each {|f| open(f, "w") {}}
+      a = "file_one*".force_encoding Encoding::IBM437
+      b = "file_two*".force_encoding Encoding::EUC_JP
+      assert_equal([a, b].map(&:encoding), Dir[a, b].map(&:encoding))
+    end
+  end
+
   def test_entries_compose
     bug7267 = '[ruby-core:48745] [Bug #7267]'
 

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

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