Skip to content

Commit

Permalink
Encoding: compare with case insensitive UTF-8 or UTF8 to avoid using …
Browse files Browse the repository at this point in the history
…iconv when not necessary (#6355)
  • Loading branch information
asterite authored and RX14 committed Jul 8, 2018
1 parent a441539 commit afeaee4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 12 additions & 0 deletions spec/std/io/io_spec.cr
Expand Up @@ -689,6 +689,18 @@ 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 "sets encoding to utf8 and stays as UTF-8" do
io = SimpleIOMemory.new(Base64.decode_string("ey8qx+Tl8fwg7+Dw4Ozl8vD7IOLo5+jy4CovfQ=="))
io.set_encoding("utf8")
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
5 changes: 4 additions & 1 deletion src/io.cr
Expand Up @@ -1023,7 +1023,10 @@ 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 invalid != :skip && (
encoding.compare("UTF-8", case_insensitive: true) == 0 ||
encoding.compare("UTF8", case_insensitive: true) == 0
)
@encoding = nil
else
@encoding = EncodingOptions.new(encoding, invalid)
Expand Down

0 comments on commit afeaee4

Please sign in to comment.