なかだです。

  Array#pack と String#unpack の P と p ですが、NULL を指定した
り受け取ったりすることはできないんでしょうか。あと 1.7 の方の p
では NULL が返されると結果がゴミになりそうな。


Index: pack.c =================================================================== RCS file: /cvs/ruby/src/ruby/pack.c,v retrieving revision 1.17.2.2 diff -u -2 -p -r1.17.2.2 pack.c --- pack.c 2001/02/08 09:17:57 1.17.2.2 +++ pack.c 2001/04/21 07:49:00 @@ -852,12 +852,29 @@ pack_pack(ary, fmt) case 'P': - len = 1; - /* FALL THROUGH */ + { + char *t = NULL; + from = NEXTFROM; + if (!NIL_P(from)) { + if (TYPE(from) != T_STRING) { + from = rb_str_to_str(from); + } + t = rb_str2cstr(from, &plen); + if (plen < len) { + rb_raise(rb_eArgError, "too short buffer for P(%d for %d)", plen, len); + } + rb_str_associate(res, from); + } + rb_str_cat(res, (char*)&t, sizeof(char*)); + } + break; + case 'p': while (len-- > 0) { char *t; from = NEXTFROM; - if (NIL_P(from)) t = ""; - else { + if (!NIL_P(from)) { + if (TYPE(from) != T_STRING) { + from = rb_str_to_str(from); + } t = STR2CSTR(from); rb_str_associate(res, from); @@ -1612,9 +1629,10 @@ pack_unpack(str, fmt) if (sizeof(char *) <= send - s) { char *t; - VALUE str = rb_str_new(0, 0); + VALUE str = Qnil; memcpy(&t, s, sizeof(char *)); s += sizeof(char *); - if (t) - rb_str_cat(str, t, len); + if (t) { + str = rb_str_new(t, len); + } rb_ary_push(ary, str); } @@ -1629,9 +1647,9 @@ pack_unpack(str, fmt) else { char *t; - VALUE str = rb_str_new(0, 0); + VALUE str = Qnil; memcpy(&t, s, sizeof(char *)); s += sizeof(char *); if (t) { - rb_str_cat2(str, t); + str = rb_str_new2(t); } rb_ary_push(ary, str);
Index: pack.c =================================================================== RCS file: /cvs/ruby/src/ruby/pack.c,v retrieving revision 1.24 diff -u -2 -p -r1.24 pack.c --- pack.c 2001/03/26 08:57:10 1.24 +++ pack.c 2001/04/21 07:45:17 @@ -852,15 +852,32 @@ pack_pack(ary, fmt) case 'P': - len = 1; - /* FALL THROUGH */ + { + char *t = NULL; + from = NEXTFROM; + if (!NIL_P(from)) { + if (TYPE(from) != T_STRING) { + from = rb_str_to_str(from); + } + t = rb_str2cstr(from, &plen); + if (plen < len) { + rb_raise(rb_eArgError, "too short buffer for P(%d for %d)", plen, len); + } + rb_str_associate(res, from); + } + rb_str_cat(res, (char*)&t, sizeof(char*)); + } + break; + case 'p': while (len-- > 0) { - char *t; + char *t = NULL; from = NEXTFROM; - if (NIL_P(from)) { - from = rb_str_new(0, 0); + if (!NIL_P(from)) { + if (TYPE(from) != T_STRING) { + from = rb_str_to_str(from); + } + t = STR2CSTR(from); + rb_str_associate(res, from); } - t = STR2CSTR(from); - rb_str_associate(res, from); rb_str_cat(res, (char*)&t, sizeof(char*)); } @@ -1640,5 +1657,5 @@ pack_unpack(str, fmt) } else { - tmp = rb_str_new(0, 0); + tmp = Qnil; } rb_ary_push(ary, tmp); @@ -1677,4 +1694,7 @@ pack_unpack(str, fmt) } tmp = rb_str_new2(t); + } + else { + tmp = Qnil; } rb_ary_push(ary, tmp);
-- --- 僕の前にBugはない。 --- 僕の後ろにBugはできる。 中田 伸悦