Skip to content

Commit 179369a

Browse files
committed
[GR-20446] Implement rb_str_vcatf
PullRequest: truffleruby/2814
2 parents cd7e743 + 17cc6b7 commit 179369a

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Compatibility:
1616
* Implement `Process::Status.wait` (#2378).
1717
* Update `rb_str_modify` and `rb_str_modify_expand` to raise a `FrozenError` when given a frozen string (#2392).
1818
* Implement `rb_fiber_*` functions (#2402).
19+
* Implement `rb_str_vcatf`.
1920

2021
Performance:
2122

lib/cext/ABI_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7
1+
8

spec/ruby/optional/capi/ext/string_spec.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,18 @@ static VALUE string_spec_rb_utf8_str_new_cstr(VALUE self) {
501501
return rb_utf8_str_new_cstr("nokogiri");
502502
}
503503

504+
static VALUE call_rb_str_vcatf(VALUE mesg, const char *fmt, ...){
505+
va_list ap;
506+
va_start(ap, fmt);
507+
VALUE result = rb_str_vcatf(mesg, fmt, ap);
508+
va_end(ap);
509+
return result;
510+
}
511+
512+
static VALUE string_spec_rb_str_vcatf(VALUE self, VALUE mesg) {
513+
return call_rb_str_vcatf(mesg, "fmt %d %d number", 42, 7);
514+
}
515+
504516
void Init_string_spec(void) {
505517
VALUE cls = rb_define_class("CApiStringSpecs", rb_cObject);
506518
rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2);
@@ -590,6 +602,7 @@ void Init_string_spec(void) {
590602
rb_define_method(cls, "rb_utf8_str_new_static", string_spec_rb_utf8_str_new_static, 0);
591603
rb_define_method(cls, "rb_utf8_str_new", string_spec_rb_utf8_str_new, 0);
592604
rb_define_method(cls, "rb_utf8_str_new_cstr", string_spec_rb_utf8_str_new_cstr, 0);
605+
rb_define_method(cls, "rb_str_vcatf", string_spec_rb_str_vcatf, 1);
593606
}
594607

595608
#ifdef __cplusplus

spec/ruby/optional/capi/string_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,4 +1142,14 @@ def inspect
11421142
str.encoding.should == Encoding::UTF_8
11431143
end
11441144
end
1145+
1146+
describe "rb_str_vcatf" do
1147+
it "appends the message to the string" do
1148+
@s.rb_str_vcatf("").should == "fmt 42 7 number"
1149+
1150+
str = "test "
1151+
@s.rb_str_vcatf(str)
1152+
str.should == "test fmt 42 7 number"
1153+
end
1154+
end
11451155
end

src/main/c/cext/printf.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,10 @@ void rb_tr_init_printf(void) {
139139
register_printf_specifier('P', rb_tr_fprintf_value, rb_tr_fprintf_value_arginfo);
140140
#endif
141141
}
142+
143+
VALUE rb_str_vcatf(VALUE str, const char *fmt, va_list args) {
144+
StringValue(str);
145+
VALUE result = rb_vsprintf(fmt, args);
146+
rb_str_concat(str, result);
147+
return str;
148+
}

0 commit comments

Comments
 (0)