Skip to content

Commit 60d9f34

Browse files
aardvark179eregon
authored andcommitted
Add a spec for formatting non-VALUEs as pointers in rb_sprintf.
1 parent 4095b26 commit 60d9f34

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

optional/capi/ext/string_spec.c

+10
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,15 @@ static VALUE string_spec_rb_sprintf7(VALUE self, VALUE str, VALUE obj) {
488488
return results;
489489
}
490490

491+
static VALUE string_spec_rb_sprintf8(VALUE self, VALUE str, VALUE num) {
492+
VALUE results = rb_ary_new();
493+
rb_ary_push(results, rb_sprintf(RSTRING_PTR(str), FIX2LONG(num)));
494+
char cstr[256];
495+
int len = snprintf(cstr, 256, RSTRING_PTR(str), FIX2LONG(num));
496+
rb_ary_push(results, rb_str_new(cstr, len));
497+
return results;
498+
}
499+
491500
PRINTF_ARGS(static VALUE string_spec_rb_vsprintf_worker(char* fmt, ...), 1, 2);
492501
static VALUE string_spec_rb_vsprintf_worker(char* fmt, ...) {
493502
va_list varargs;
@@ -648,6 +657,7 @@ void Init_string_spec(void) {
648657
rb_define_method(cls, "rb_sprintf5", string_spec_rb_sprintf5, 3);
649658
rb_define_method(cls, "rb_sprintf6", string_spec_rb_sprintf6, 3);
650659
rb_define_method(cls, "rb_sprintf7", string_spec_rb_sprintf7, 2);
660+
rb_define_method(cls, "rb_sprintf8", string_spec_rb_sprintf8, 2);
651661
rb_define_method(cls, "rb_vsprintf", string_spec_rb_vsprintf, 4);
652662
rb_define_method(cls, "rb_str_equal", string_spec_rb_str_equal, 2);
653663
rb_define_method(cls, "rb_usascii_str_new", string_spec_rb_usascii_str_new, 2);

optional/capi/string_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,11 @@ def inspect
10951095
res = @s.rb_sprintf7("%p", "Hello")
10961096
res[0].should == res[1]
10971097
end
1098+
1099+
it "can format a raw number a pointer and gives the same output as sprintf in C" do
1100+
res = @s.rb_sprintf7("%p", 0x223643);
1101+
res[0].should == res[1]
1102+
end
10981103
end
10991104

11001105
describe "rb_vsprintf" do

0 commit comments

Comments
 (0)