Skip to content

Commit ff1ba50

Browse files
committed
Specs and fix for rb_str_export_to_enc.
1 parent 2f8f3cc commit ff1ba50

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

lib/truffle/truffle/cext.rb

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

910910
ec = Encoding::Converter.new(from, to, ecopts || ecflags)
911911
dest = ''
912-
status = ec.primitive_convert str, dest, nil, nil, ec.options
912+
# This C API will (unlike primitive convert) not alter the source
913+
# string, so we need to duplicate it.
914+
status = ec.primitive_convert str.dup, dest, nil, nil, ec.options
913915
status == :finished ? dest : str
914916
end
915917

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,26 @@ 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+
end
886+
887+
it "returns the source string if it can not be converted" do
888+
source = ["00ff"].pack("H*");
889+
result = @s.rb_str_export_to_enc(source, Encoding::UTF_8)
890+
result.should == source
891+
end
892+
893+
it "does not alter the source string if it can not be converted" do
894+
source = ["00ff"].pack("H*");
895+
result = @s.rb_str_export_to_enc(source, Encoding::UTF_8)
896+
source.bytes.should == [0, 255]
897+
end
898+
end
899+
880900
describe "rb_sprintf" do
881901
it "replaces the parts like sprintf" do
882902
@s.rb_sprintf1("Awesome %s is replaced", "string").should == "Awesome string is replaced"

0 commit comments

Comments
 (0)