X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=ext%2Fcomments.c;h=03ba68009012eb329fb4f91b9402fe22bae36f48;hb=2a9ced258e6bbeea7e461f13ac6c01928809b871;hp=fb3ae82f653d727d751cc9975626caabfb1b5f8c;hpb=de09557fdbeb5648cc66ea5fca28b3d769003ee8;p=ruby-vorbistagger.git diff --git a/ext/comments.c b/ext/comments.c index fb3ae82..03ba680 100644 --- a/ext/comments.c +++ b/ext/comments.c @@ -24,7 +24,7 @@ #include "vcedit.h" -static ID id_casecmp, id_replace, id_compare; +static ID id_casecmp, id_replace, id_each, id_compare; void comments_init (VALUE self, vcedit_state *state) @@ -41,14 +41,17 @@ comments_init (VALUE self, vcedit_state *state) for (i = 0; i < vc->comments; i++) { VALUE k, v, pair; char *ptr, *content = vc->user_comments[i]; + int k_len, v_len; ptr = strchr (content, '='); assert (ptr); - k = rb_str_new (content, ptr - content); + k_len = ptr - content; + k = rb_str_new (content, k_len); OBJ_FREEZE (k); - v = rb_str_new2 (ptr + 1); + v_len = vc->comment_lengths[i] - k_len - 1; + v = rb_str_new (ptr + 1, v_len); pair = rb_ary_new3 (2, k, v); OBJ_FREEZE (pair); @@ -345,27 +348,34 @@ c_has_key (VALUE self, VALUE key) return Qfalse; } -static int -merge_cb (VALUE key, VALUE value, VALUE self) +static VALUE +merge_cb (VALUE ar, VALUE self) { - c_aset (self, key, value); + struct RArray *pair = RARRAY (ar); + + c_aset (self, pair->ptr[0], pair->ptr[1]); - return ST_CONTINUE; + return Qnil; } /* * call-seq: - * object.merge!(hash) -> object + * object.merge!(arg) -> object * - * Adds the key-value pairs from *hash* to *object*, overwriting existing + * Adds the key-value pairs from *arg* to *object*, overwriting existing * values if a key already existed in *object*. + * + * Note that *arg*'s each method needs to yield key-value pairs for this + * to work. This means that e.g. hashes and Ogg::Vorbis::Comments objects + * are supported as arguments. */ static VALUE -c_merge (VALUE self, VALUE hash) +c_merge (VALUE self, VALUE arg) { - Check_Type (hash, T_HASH); + if (!rb_respond_to (arg, id_each)) + rb_raise (rb_eArgError, "invalid argument"); - rb_hash_foreach (hash, merge_cb, self); + rb_iterate (rb_each, arg, merge_cb, self); return self; } @@ -536,6 +546,7 @@ Init_Comments (VALUE mVorbis) id_casecmp = rb_intern ("casecmp"); id_replace = rb_intern ("replace"); + id_each = rb_intern ("each"); id_compare = rb_intern ("<=>"); return c;