Skip to content

Commit 53ab42b

Browse files
committed
[GR-11887] Specs and fix for rb_str_export_to_enc.
PullRequest: truffleruby/756
2 parents 42f9cf7 + 4efe068 commit 53ab42b

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

lib/truffle/truffle/cext.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,9 @@ def rb_str_conv_enc_opts(str, from, to, ecflags, ecopts)
931931

932932
ec = Encoding::Converter.new(from, to, ecopts || ecflags)
933933
dest = ''
934-
status = ec.primitive_convert str, dest, nil, nil, ec.options
934+
# This C API will (unlike primitive convert) not alter the source
935+
# string, so we need to duplicate it.
936+
status = ec.primitive_convert str.dup, dest, nil, nil, ec.options
935937
status == :finished ? dest : str
936938
end
937939

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ VALUE string_spec_rb_str_encode(VALUE self, VALUE str, VALUE enc, VALUE flags, V
176176
return rb_str_encode(str, enc, FIX2INT(flags), opts);
177177
}
178178

179+
VALUE string_spec_rb_str_export_to_enc(VALUE self, VALUE str, VALUE enc) {
180+
return rb_str_export_to_enc(str, rb_to_encoding(enc));
181+
}
182+
179183
VALUE string_spec_rb_str_new_cstr(VALUE self, VALUE str) {
180184
if(NIL_P(str)) {
181185
return rb_str_new_cstr("");
@@ -435,6 +439,7 @@ void Init_string_spec(void) {
435439
rb_define_method(cls, "rb_str_new_offset", string_spec_rb_str_new_offset, 3);
436440
rb_define_method(cls, "rb_str_new2", string_spec_rb_str_new2, 1);
437441
rb_define_method(cls, "rb_str_encode", string_spec_rb_str_encode, 4);
442+
rb_define_method(cls, "rb_str_export_to_enc", string_spec_rb_str_export_to_enc, 2);
438443
rb_define_method(cls, "rb_str_new_cstr", string_spec_rb_str_new_cstr, 1);
439444
rb_define_method(cls, "rb_external_str_new", string_spec_rb_external_str_new, 1);
440445
rb_define_method(cls, "rb_external_str_new_cstr", string_spec_rb_external_str_new_cstr, 1);

spec/ruby/optional/capi/string_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,27 @@ def inspect
877877
end
878878
end
879879

880+
describe "rb_str_export_to_enc" do
881+
it "returns a copy of an ascii string converted to the new encoding" do
882+
source = "A simple string".encode(Encoding::US_ASCII)
883+
result = @s.rb_str_export_to_enc(source, Encoding::UTF_8)
884+
result.should == source.encode(Encoding::UTF_8)
885+
result.encoding.should == Encoding::UTF_8
886+
end
887+
888+
it "returns the source string if it can not be converted" do
889+
source = ["00ff"].pack("H*");
890+
result = @s.rb_str_export_to_enc(source, Encoding::UTF_8)
891+
result.should equal(source)
892+
end
893+
894+
it "does not alter the source string if it can not be converted" do
895+
source = ["00ff"].pack("H*");
896+
result = @s.rb_str_export_to_enc(source, Encoding::UTF_8)
897+
source.bytes.should == [0, 255]
898+
end
899+
end
900+
880901
describe "rb_sprintf" do
881902
it "replaces the parts like sprintf" do
882903
@s.rb_sprintf1("Awesome %s is replaced", "string").should == "Awesome string is replaced"

0 commit comments

Comments
 (0)