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

ruby-changes:45054

From: naruse <ko1@a...>
Date: Wed, 21 Dec 2016 01:02:26 +0900 (JST)
Subject: [ruby-changes:45054] naruse:r57126 (trunk): Zlib.gzip uses kwargs instead of argc [Feature #13020]

naruse	2016-12-21 01:02:21 +0900 (Wed, 21 Dec 2016)

  New Revision: 57126

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

  Log:
    Zlib.gzip uses kwargs instead of argc [Feature #13020]

  Modified files:
    trunk/ext/zlib/zlib.c
    trunk/test/zlib/test_zlib.rb
Index: ext/zlib/zlib.c
===================================================================
--- ext/zlib/zlib.c	(revision 57125)
+++ ext/zlib/zlib.c	(revision 57126)
@@ -4276,6 +4276,10 @@ zlib_gzip_end(struct gzfile *gz) https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L4276
     zstream_end(&gz->z);
 }
 
+#define OPTHASH_GIVEN_P(opts) \
+    (argc > 0 && !NIL_P((opts) = rb_check_hash_type(argv[argc-1])) && (--argc, 1))
+static ID id_level, id_strategy;
+
 /*
  * call-seq:
  *   Zlib.gzip(src, level=nil, strategy=nil) -> String
@@ -4305,9 +4309,22 @@ zlib_s_gzip(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L4309
     struct gzfile *gz = &gz0;
     long len;
     int err;
-    VALUE src, level, strategy;
+    VALUE src, opts, level=Qnil, strategy=Qnil;
 
-    rb_scan_args(argc, argv, "12", &src, &level, &strategy);
+    if (OPTHASH_GIVEN_P(opts)) {
+	ID keyword_ids[2];
+	VALUE kwargs[2];
+	keyword_ids[0] = id_level;
+	keyword_ids[1] = id_strategy;
+	rb_get_kwargs(opts, keyword_ids, 0, 2, kwargs);
+	if (kwargs[0] != Qundef) {
+	    level = kwargs[0];
+	}
+	if (kwargs[1] != Qundef) {
+	    strategy = kwargs[1];
+	}
+    }
+    rb_scan_args(argc, argv, "10", &src);
     StringValue(src);
     gzfile_init(gz, &deflate_funcs, zlib_gzip_end);
     gz->level = ARG_LEVEL(level);
@@ -4690,6 +4707,8 @@ Init_zlib(void) https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L4707
     /* OS code for unknown hosts */
     rb_define_const(mZlib, "OS_UNKNOWN", INT2FIX(OS_UNKNOWN));
 
+    id_level = rb_intern("level");
+    id_strategy = rb_intern("strategy");
 #endif /* GZIP_SUPPORT */
 }
 
Index: test/zlib/test_zlib.rb
===================================================================
--- test/zlib/test_zlib.rb	(revision 57125)
+++ test/zlib/test_zlib.rb	(revision 57126)
@@ -1134,19 +1134,19 @@ if defined? Zlib https://github.com/ruby/ruby/blob/trunk/test/zlib/test_zlib.rb#L1134
       expected = %w[1f8b08000000000000ff4bcbcf07002165738c03000000].pack("H*")
       assert_equal expected, actual
 
-      actual = Zlib.gzip("foo".freeze, 0)
+      actual = Zlib.gzip("foo".freeze, level: 0)
       actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
       actual[9] = "\xff" # replace OS
       expected = %w[1f8b08000000000000ff010300fcff666f6f2165738c03000000].pack("H*")
       assert_equal expected, actual
 
-      actual = Zlib.gzip("foo".freeze, 9)
+      actual = Zlib.gzip("foo".freeze, level: 9)
       actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
       actual[9] = "\xff" # replace OS
       expected = %w[1f8b08000000000002ff4bcbcf07002165738c03000000].pack("H*")
       assert_equal expected, actual
 
-      actual = Zlib.gzip("foo".freeze, 9, Zlib::FILTERED)
+      actual = Zlib.gzip("foo".freeze, level: 9, strategy: Zlib::FILTERED)
       actual[4, 4] = "\x00\x00\x00\x00" # replace mtime
       actual[9] = "\xff" # replace OS
       expected = %w[1f8b08000000000002ff4bcbcf07002165738c03000000].pack("H*")

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

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