Forum: Ruby-dev '*' parameter in pack format 'M', 'm', or 'P' acts oddly

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1e7e007723cfe59320d994a7db4ab4cf?d=identicon&s=25 Yugui (Yuki Sonoda) (Guest)
on 2008-12-06 13:38
(Received via mailing list)
Yuguiです。

Array#packにおいて、"M<number>"や"m<number>"は出力の1行の長さを、
"P<number>"は対応する文字列(バイト列)の長さを表します。

にもかかわらず、"M*"や"m*", 
"P*"を指定するとアスタリスクが他のフォーマッ
トにおける場合同じく余っている配列要素の個数に展開されます。

結果として、何だか妙な挙動になります。
["abc"*5, "d", "e", "f"].pack('m*')
  #=> "YWJj\nYWJj\nYWJj\nYWJj\nYWJj\n"
["abc"*5, "d", "e", "f", "g", "h"].pack('m*')
  #=> "YWJjYWJj\nYWJjYWJj\nYWJj\n"

["a"].pack("P*")      #=> "\220\a9\001"
["a", "b"].pack("P*") #=> ArgumentError: too short buffer for P(1 for 2)


この3つのフォーマットではアスタリスクは単に無視するのが良いのではないか
と思いまして、添付のような修正を書いてみました。いかがでしょう?
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2008-12-08 14:17
(Received via mailing list)
$B$^$D$b$H(B $B$f$-$R$m$G$9(B

In message "Re: [ruby-dev:37289] '*' parameter in pack format 'M', 'm',
or 'P' acts oddly"
    on Sat, 6 Dec 2008 21:29:36 +0900, "Yugui (Yuki Sonoda)"
<yugui@yugui.jp> writes:

|Yugui$B$G$9!#(B
|
|Array#pack$B$K$*$$$F!"(B"M<number>"$B$d(B"m<number>"$B$O=PNO$N(B1$B9T$ND9$5$r!"(B
|"P<number>"$B$OBP1~$9$kJ8;zNs(B($B%P%$%HNs(B)$B$ND9$5$rI=$7$^$9!#(B
|
|$B$K$b$+$+$o$i$:!"(B"M*"$B$d(B"m*", 
"P*"$B$r;XDj$9$k$H%"%9%?%j%9%/$,B>$N%U%)!<%^%C(B
|$B%H$K$*$1$k>l9gF1$8$/M>$C$F$$$kG[NsMWAG$N8D?t$KE83+$5$l$^$9!#(B
|
|$B7k2L$H$7$F!"2?$@$+L/$J5sF0$K$J$j$^$9!#(B
|["abc"*5, "d", "e", "f"].pack('m*')
|  #=> "YWJj\nYWJj\nYWJj\nYWJj\nYWJj\n"
|["abc"*5, "d", "e", "f", "g", "h"].pack('m*')
|  #=> "YWJjYWJj\nYWJjYWJj\nYWJj\n"
|
|["a"].pack("P*")      #=> "\220\a9\001"
|["a", "b"].pack("P*") #=> ArgumentError: too short buffer for P(1 for 2)
|
|$B$3$N(B3$B$D$N%U%)!<%^%C%H$G$O%"%9%?%j%9%/$OC1$KL5;k$9$k$N$,NI$$$N$G$O$J$$$+(B
|$B$H;W$$$^$7$F!"E:IU$N$h$&$J=$@5$r=q$$$F$_$^$7$?!#$$$+$,$G$7$g$&(B?

$B$$$$$s$8$c$J$$$G$7$g$&$+!#%3%_%C%H$7$F$/$@$5$$!#(B
This topic is locked and can not be replied to.