Project

General

Profile

Actions

Bug #10454

closed

%i doesn't have any special behavior in rb_sprintf et al.

Added by silverhammermba (Max Anselm) about 10 years ago. Updated about 10 years ago.

Status:
Closed
Assignee:
Target version:
-
ruby -v:
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-linux]
[ruby-core:66003]

Description

README.EXT claims:

In the format string, %i is used for Object#to_s (or Object#inspect if ‘+’ flag is set) output (and related argument must be a VALUE).

But if you look at the code, this isn't true. %d and %i are handled the same in BSD_vfprintf (which is where all the printf functions end up). Has this ever been the case? I went back to when this documentation was added and it still looks like they're handled the same.

Updated by nobu (Nobuyoshi Nakada) about 10 years ago

  • Status changed from Open to Closed

It's true, see ruby__sfvextra().

Updated by silverhammermba (Max Anselm) about 10 years ago

Well I can't get it to work for some reason. When I run this code:

#include <ruby.h>

VALUE func(VALUE arg)
{
	VALUE str = rb_sprintf("%i %+i : %d %+d", Qtrue, Qtrue, Qtrue, Qtrue);
	rb_funcall(rb_mKernel, rb_intern("puts"), 1, str);
	rb_warn("%i %+i : %d %+d", Qtrue, Qtrue, Qtrue, Qtrue);
	rb_raise(rb_eRuntimeError, "%i %+i : %d %+d", Qtrue, Qtrue, Qtrue, Qtrue);
}

int main(int argc, char* argv[])
{
	ruby_init();

	int state;
	rb_protect(func, Qnil, &state);

	return ruby_cleanup(state);
}

I get

20 +20 : 20 +20
<main>: warning: 20 +20 : 20 +20
<main>: 20 +20 : 20 +20 (RuntimeError)

Updated by nobu (Nobuyoshi Nakada) about 10 years ago

Use "%"PRIsVALUE instead of "%i".

Updated by silverhammermba (Max Anselm) about 10 years ago

Ah, I see now. I needed to use %li. It seems like README.EXT should mention PRIsVALUE instead of %i.

Updated by nobu (Nobuyoshi Nakada) about 10 years ago

It varies with the platforms.
You should avoid all %is.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0