Skip to content

Commit

Permalink
Use '?' for replacement char sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m committed Jun 22, 2024
1 parent 4365e7b commit 1839164
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
8 changes: 3 additions & 5 deletions spec/core/string/encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,9 @@
end

it "replaces undefined encoding in destination with default replacement" do
NATFIXME 'undef option' do
encoded = "B\ufffd".encode(Encoding::US_ASCII, undef: :replace)
encoded.should == "B?".encode(Encoding::US_ASCII)
encoded.encode("UTF-8").should == "B?"
end
encoded = "B\ufffd".encode(Encoding::US_ASCII, undef: :replace)
encoded.should == "B?".encode(Encoding::US_ASCII)
encoded.encode("UTF-8").should == "B?"
end

it "replaces undefined encoding in destination with a specified replacement" do
Expand Down
6 changes: 2 additions & 4 deletions spec/core/string/shared/encode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,8 @@
end

it "replaces invalid characters in the destination encoding" do
NATFIXME 'encode options' do
xFF = [0xFF].pack('C').force_encoding('utf-8')
"ab#{xFF}c".send(@method, Encoding::ISO_8859_1, invalid: :replace).should == "ab?c"
end
xFF = [0xFF].pack('C').force_encoding('utf-8')
"ab#{xFF}c".send(@method, Encoding::ISO_8859_1, invalid: :replace).should == "ab?c"
end

it "calls #to_hash to convert the options object" do
Expand Down
10 changes: 8 additions & 2 deletions src/encoding_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ Value EncodingObject::encode(Env *env, EncodingObject *orig_encoding, StringObje
temp_string.append(options.replace_option);
continue;
}
unicode_codepoint = 0xFFFD;
if (is_single_byte_encoding())
unicode_codepoint = '?';
else
unicode_codepoint = 0xFFFD;
}
} else {
unicode_codepoint = orig_encoding->to_unicode_codepoint(source_codepoint);
Expand Down Expand Up @@ -134,7 +137,10 @@ Value EncodingObject::encode(Env *env, EncodingObject *orig_encoding, StringObje
temp_string.append(options.replace_option);
continue;
}
destination_codepoint = 0xFFFD;
if (is_single_byte_encoding())
destination_codepoint = '?';
else
destination_codepoint = 0xFFFD;
}
}

Expand Down

0 comments on commit 1839164

Please sign in to comment.