Skip to content

Commit

Permalink
[GR-20329] Implement rb_fstring.
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/1641
  • Loading branch information
bjfish committed May 28, 2020
2 parents 17657aa + f7c1549 commit 541659c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -22,6 +22,7 @@ Compatibility:
* Fix detection of `#find_type` in FFI to ignore `MakeMakefile#find_type` from `mkmf` (#1896, #2010).
* Implemented `rb_uv_to_utf8` (#1998).
* Implemented `rb_str_cat_cstr`.
* Implemented `rb_fstring`.

Performance:

Expand Down
7 changes: 7 additions & 0 deletions spec/ruby/optional/capi/ext/string_spec.c
Expand Up @@ -49,6 +49,12 @@ VALUE string_spec_rb_str_set_len_RSTRING_LEN(VALUE self, VALUE str, VALUE len) {
return INT2FIX(RSTRING_LEN(str));
}

VALUE rb_fstring(VALUE str); /* internal.h, used in ripper */

VALUE string_spec_rb_str_fstring(VALUE self, VALUE str) {
return rb_fstring(str);
}

VALUE string_spec_rb_str_buf_new(VALUE self, VALUE len, VALUE str) {
VALUE buf;

Expand Down Expand Up @@ -460,6 +466,7 @@ void Init_string_spec(void) {
VALUE cls = rb_define_class("CApiStringSpecs", rb_cObject);
rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2);
rb_define_method(cls, "rb_cstr_to_inum", string_spec_rb_cstr_to_inum, 3);
rb_define_method(cls, "rb_fstring", string_spec_rb_str_fstring, 1);
rb_define_method(cls, "rb_str2inum", string_spec_rb_str2inum, 2);
rb_define_method(cls, "rb_str_append", string_spec_rb_str_append, 2);
rb_define_method(cls, "rb_str_buf_new", string_spec_rb_str_buf_new, 2);
Expand Down
19 changes: 19 additions & 0 deletions spec/ruby/optional/capi/string_spec.rb
Expand Up @@ -464,6 +464,25 @@ def inspect
end
end

describe "rb_fstring" do
it 'returns self if the String is frozen' do
input = 'foo'.freeze
output = @s.rb_fstring(input)

output.should equal(input)
output.should.frozen?
end

it 'returns a frozen copy if the String is not frozen' do
input = 'foo'
output = @s.rb_fstring(input)

output.should.frozen?
output.should_not equal(input)
output.should == 'foo'
end
end

describe "rb_str_subseq" do
it "returns a byte-indexed substring" do
str = "\x00\x01\x02\x03\x04".force_encoding("binary")
Expand Down
4 changes: 4 additions & 0 deletions src/main/c/cext/string.c
Expand Up @@ -121,6 +121,10 @@ VALUE rb_str_to_str(VALUE string) {
return rb_convert_type(string, T_STRING, "String", "to_str");
}

VALUE rb_fstring(VALUE str) {
return RUBY_INVOKE(str, "-@");
}

VALUE rb_str_buf_new(long capacity) {
VALUE str = rb_str_new(NULL, capacity);
rb_str_set_len(str, 0);
Expand Down

0 comments on commit 541659c

Please sign in to comment.