From ba568c0ea3c84c2e2fa79168df2c970812403c8e Mon Sep 17 00:00:00 2001 From: naruse Date: Tue, 20 Dec 2016 16:02:21 +0000 Subject: Zlib.gzip uses kwargs instead of argc [Feature #13020] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/zlib/zlib.c | 25 ++++++++++++++++++++++--- test/zlib/test_zlib.rb | 6 +++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 4052e49b4b..b08f4296e4 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -4276,6 +4276,10 @@ zlib_gzip_end(struct gzfile *gz) 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 klass) struct gzfile *gz = &gz0; long len; int err; - VALUE src, level, strategy; - - rb_scan_args(argc, argv, "12", &src, &level, &strategy); + VALUE src, opts, level=Qnil, strategy=Qnil; + + 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) /* 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 */ } diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb index 7f9ec981fd..06e85f6315 100644 --- a/test/zlib/test_zlib.rb +++ b/test/zlib/test_zlib.rb @@ -1134,19 +1134,19 @@ if defined? Zlib 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*") -- cgit v1.2.3