From: nobu.nakada@... Date: 2002-03-13T09:11:24+09:00 Subject: [ruby-dev:16359] Re: map in printf format なかだです。 At Wed, 13 Mar 2002 03:58:07 +0900, Yukihiro Matsumoto wrote: > |Pythonの"%(x)s"形式のフォーマット(正式にはなんて言うんだか?)を > |見て、実装してみました。 > | > |それと"%2$s%s"としたときに、現状では二番目と三番目の引数を使っ > |てますが、glibcなどでは二番目と最初のが使われるようです。この議 > |論はなんか昔あったような気がしますが、"*"の後の"$"ではカウンタ > |が進まないことと一貫してないように思えるので、揃えてみました。 > > えーと,前者は要りません。あれは#{}がないPythonなりのハック > だと思うので。 まぁ、そもそも#{}があればprintf系はほとんど要らないというのもあ りますが、gettextのようにメッセージを分離したいという場合には便 利だったりしないでしょうか。 > 後者は(もしその挙動が正しければ)取り込みたいです。どういう挙 > 動が正しいのか確認する手段はありますかね。glibcに合わせれば > それでよいのかな。 とりあえずglibc 2.2.4では以下のコードで2123と出力されますが、 The Open Group Base Specifications Issue 6では混ぜるな、としっ かり書いてありました。使った場合の動作は決まってないようですが、 最後の例のように"%n$"は次の変換指定に影響するけど"*m$"はしない というのは、ちょっと不思議な気がします。 The format can contain either numbered argument conversion specifications (that is, "%n$" and "*m$"), or unnumbered argument conversion specifications (that is, % and * ), but not both. The only exception to this is that %% can be mixed with the "%n$" form. The results of mixing numbered and unnumbered argument specifications in a format string are undefined. $ cat test.c #include int main() { printf("%2$d%d%d%d\n", 1, 2, 3, 4); return 0; } $ gcc test.c && ./a.out 2123 $ ./miniruby -ve 'printf("%2$d%d\n", 1, 2, 3)' 23 $ ./miniruby -ve 'printf("%2$d%d%d%d\n", 1, 2, 3, 4)' -e:1:in `printf': too few argument. (ArgumentError) from -e:1 $ ./miniruby -ve 'printf("%2$*1$d%d\n", 10, 2, 3)' 23 -- --- 僕の前にBugはない。 --- 僕の後ろにBugはできる。 中田 伸悦