[MacRuby] #608: rb_str_format() does not handle the "field width" flag for strings
#608: rb_str_format() does not handle the "field width" flag for strings ----------------------------------------+----------------------------------- Reporter: martinlagardette@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Ruby: {{{ ruby -e 'puts "%-10s%s" % ["a", "b"]' a b }}} MacRuby SVN r3441: {{{ macruby -e 'puts "%-10s%s" % ["a", "b"]' ab }}} This happens because `sprintf.cpp:rb_str_format:467` transforms `%s` into `%@`, and asks for formating to `CFStringCreateWithFormat()`, which does not handle the "field width" flag for Cocoa objects (`%@`). A possible fix might be to explicitly call `-[NSObject description]` and then get the UTF8/16 C string (which means `%s` would not be transformed into `%@`), but I'm not sure this is a pretty fix :D -- Ticket URL: <http://www.macruby.org/trac/ticket/608> MacRuby <http://macruby.org/>
#608: rb_str_format() does not handle the "field width" flag for strings ----------------------------------------+----------------------------------- Reporter: martinlagardette@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Comment(by danielcavanagh@…): you're right it's not pretty, and it's not enough too, because %b is also broken (because it too is tranformed to %@) and to support it fully we need to duplicate a lot of the cf sprintf code (absolute references code and named references are missing too), plus the full generic number printing code (precision, infinite negatives, padding, and so on) so a complete macruby sprintf has to be written, basically, which is what i'm doing at the moment. i've got it mostly working and am just ironing out some bugs. i've attached a draft patch if anyone is interested. no guarantees it compiles... :p -- Ticket URL: <http://www.macruby.org/trac/ticket/608#comment:1> MacRuby <http://macruby.org/>
#608: rb_str_format() does not handle the "field width" flag for strings ----------------------------------------+----------------------------------- Reporter: martinlagardette@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Comment(by danielcavanagh@…): ok, i've finished and the diff above has been updated. as i explained before, to support %b we have to unfortunately fully reimplement sprintf. with this diff, sprintf now matches ruby 1.9's, except for these differences: - this implementation fully supports named (eg. "%<key>d") arguments. ruby 1.9 only supports them for the type itself, not for the width and precision - this implementation is more permissive of the structure of the format string. for instance, ruby 1.9 will raise if there are multiple widths or precisions, etc., or if they aren't in their 'proper' positions. there's no technical reason for this, it's just a relic of the past, so i've allowed everything to be specified anywhere and multiple times (except for the type itself, of course) and the last one wins. for instance, "%.8#+.6d" would raise an arg error in ruby 1.9, but this sprintf would pass them and the precision would be set to 6 - the char type "%c" allows a multi-char string as an argument and simply chops off the end. ruby 1.9 raises on multi-char strings. i'm wary of this difference. any thoughts? it passes the ruby 1.9 sprintf tests except for the above-mentioned lack of raising, and what appears to be a float rounding difference. i believe this to be a platform difference (as this impl. simply reuses c's sprintf, the same as ruby 1.9), so i've ignored that one. still to be implemented is proper ruby 1.9 encoding support, but that can wait til macruby in general supports it -- Ticket URL: <http://www.macruby.org/trac/ticket/608#comment:2> MacRuby <http://macruby.org/>
#608: rb_str_format() does not handle the "field width" flag for strings ----------------------------------------+----------------------------------- Reporter: martinlagardette@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Comment(by lsansonetti@…): Excellent work! I have a few open-questions: * It looks like the patch does not apply on trunk (as of r3472). Can you upload a new version? * Did you try running spec:ci to see if no regression was introduced? * Could you follow the project's coding style (described in HACKING.rdoc)? More specifically, always use braces for conditionals and moving else statements to a new line. -- Ticket URL: <http://www.macruby.org/trac/ticket/608#comment:3> MacRuby <http://macruby.org/>
#608: rb_str_format() does not handle the "field width" flag for strings ----------------------------------------+----------------------------------- Reporter: martinlagardette@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ----------------------------------------+----------------------------------- Comment(by danielcavanagh@…): sorry about that! i didn't realise HACKING.rdoc was there. i originally had braces around everything but at the end i removed them from single- line conditionals because they were ugly ;) the diff has been updated to 3474, it should follow HACKING.rdoc now, and spec:ci passes except for some random Dispatch errors, but since that's being worked on i'm going to assume it isn't sprintf's fault. let me know if that's not the case and i'll dig into it a bit more -- Ticket URL: <http://www.macruby.org/trac/ticket/608#comment:4> MacRuby <http://macruby.org/>
On Feb 10, 2010, at 4:20 PM, MacRuby wrote:
i originally had braces around everything but at the end i removed them from single- line conditionals because they were ugly ;)
They *are* ugly, but nowhere near as ugly as what happens when someone goes to add another line to an existing conditional body, or what happens when they get an else accidentally paired up with the wrong conditional. There are good defensive coding reasons for the ugliness, in other words. :) - Jordan
#608: rb_str_format() does not handle the "field width" flag for strings ----------------------------------------+----------------------------------- Reporter: martinlagardette@… | Owner: lsansonetti@… Type: defect | Status: closed Priority: minor | Milestone: MacRuby 0.6 Component: MacRuby | Resolution: fixed Keywords: | ----------------------------------------+----------------------------------- Changes (by lsansonetti@…): * status: new => closed * resolution: => fixed * milestone: => MacRuby 0.6 Comment: Thanks a lot for this nice work. Committed as r3487 :-) -- Ticket URL: <http://www.macruby.org/trac/ticket/608#comment:5> MacRuby <http://macruby.org/>
participants (2)
-
Jordan K. Hubbard
-
MacRuby