Skip to content

Commit

Permalink
Encoding: compare with case insensitive UTF-8 to avoid using iconv wh…
Browse files Browse the repository at this point in the history
…en not necessary
  • Loading branch information
asterite committed Jul 7, 2018
1 parent fa8a586 commit b215bbc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions spec/std/io/io_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,12 @@ describe IO do
end
end

it "sets encoding to utf-8 and stays as UTF-8" do
io = SimpleIOMemory.new(Base64.decode_string("ey8qx+Tl8fwg7+Dw4Ozl8vD7IOLo5+jy4CovfQ=="))
io.set_encoding("utf-8")
io.encoding.should eq("UTF-8")
end

it "does skips when converting to UTF-8" do
io = SimpleIOMemory.new(Base64.decode_string("ey8qx+Tl8fwg7+Dw4Ozl8vD7IOLo5+jy4CovfQ=="))
io.set_encoding("UTF-8", invalid: :skip)
Expand Down
2 changes: 1 addition & 1 deletion src/io.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ abstract class IO
# String operations (`gets`, `gets_to_end`, `read_char`, `<<`, `print`, `puts`
# `printf`) will use this encoding.
def set_encoding(encoding : String, invalid : Symbol? = nil)
if (encoding == "UTF-8") && (invalid != :skip)
if encoding.compare("UTF-8", case_insensitive: true) == 0 && invalid != :skip
@encoding = nil
else
@encoding = EncodingOptions.new(encoding, invalid)
Expand Down

0 comments on commit b215bbc

Please sign in to comment.