Hi,

At Sun, 25 Apr 2004 14:34:03 +0900,
daz wrote in [ruby-talk:98298]:
> The A and Z string field formats should IMO be recovered from
> left to right.  Doesn't the term "string" relate here to a
> string element within a packed field.  The packed field just
> happens to be a Ruby String.

Sounds nice.


Index: pack.c =================================================================== RCS file: /cvs/ruby/src/ruby/pack.c,v retrieving revision 1.69 diff -u -2 -p -r1.69 pack.c --- pack.c 18 Apr 2004 23:19:45 -0000 1.69 +++ pack.c 25 Apr 2004 06:39:33 -0000 @@ -435,5 +435,5 @@ static unsigned long utf8_to_uv _((char* * X | Back up a byte * x | Null byte - * Z | Same as ``A'' + * Z | Same as ``a'', except that null is added with * */ @@ -524,6 +524,9 @@ pack_pack(ary, fmt) case 'A': /* ASCII string (space padded) */ case 'Z': /* null terminated ASCII string */ - if (plen >= len) + if (plen >= len) { rb_str_buf_cat(res, ptr, len); + if (p[-1] == '*' && type == 'Z') + rb_str_buf_cat(res, nul10, 1); + } else { rb_str_buf_cat(res, ptr, plen); @@ -1174,4 +1177,5 @@ infected_str_new(ptr, len, str) * "abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "] * "abc \0\0".unpack('a3a3') #=> ["abc", " \000\000"] + * "abc \0abc \0".unpack('Z*Z*') #=> ["abc ", "abc "] * "aa".unpack('b8B8') #=> ["10000110", "01100001"] * "aaa".unpack('h2H2c') #=> ["16", "61", 97] @@ -1285,4 +1289,5 @@ infected_str_new(ptr, len, str) * -------+---------+----------------------------------------- * Z | String | with trailing nulls removed + * | | upto first null with * * -------+---------+----------------------------------------- * @ | --- | skip to the offset given by the @@ -1377,5 +1382,13 @@ pack_unpack(str, fmt) case 'Z': if (len > send - s) len = send - s; - { + if (star) { + char *t = s; + + while (t < send && *t) t++; + rb_ary_push(ary, infected_str_new(s, t - s, str)); + if (t < send) t++; + s = t; + } + else { long end = len; char *t = s + len - 1;
-- Nobu Nakada