Skip to content

Commit

Permalink
handle -unix, -dos, -mac special encoding suffixes
Browse files Browse the repository at this point in the history
  • Loading branch information
quix committed Aug 3, 2011
1 parent ffd31a3 commit 2b78929
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/live_ast/reader.rb
Expand Up @@ -12,7 +12,15 @@ def self.read(file)
# magic comment overrides BOM
encoding = contents[MAGIC_COMMENT, 1] || utf8 || "US-ASCII"

contents.force_encoding(encoding)
contents.force_encoding(strip_special(encoding))
end

def self.strip_special(encoding)
if encoding =~ /\Autf8-mac\Z/i
"UTF8-MAC"
else
encoding.sub(/-(unix|dos|mac)\Z/i, "")
end
end
end
end
4 changes: 4 additions & 0 deletions test/encoding_test.rb
Expand Up @@ -5,6 +5,10 @@ class AllEncodingTest < RegularTest
default US-ASCII
usascii US-ASCII
utf8 UTF-8
utf8unix UTF-8
utf8mac UTF-8
utf8mac_alt UTF8-MAC
utf8dos UTF-8
utf8bom UTF-8
utf8bom_only UTF-8
usascii_with_utf8bom US-ASCII
Expand Down
6 changes: 6 additions & 0 deletions test/encoding_test/utf8dos.rb
@@ -0,0 +1,6 @@
# -*- coding: utf-8-dos -*-
module EncodingTest
def utf8dos_string
"大きな箱とねこ。"
end
end
6 changes: 6 additions & 0 deletions test/encoding_test/utf8mac.rb
@@ -0,0 +1,6 @@
# -*- coding: utf-8-mac -*-
module EncodingTest
def utf8mac_string
"大きな箱とねこ。"
end
end
6 changes: 6 additions & 0 deletions test/encoding_test/utf8mac_alt.rb
@@ -0,0 +1,6 @@
# -*- coding: utf8-mac -*-
module EncodingTest
def utf8mac_alt_string
"大きな箱とねこ。"
end
end
6 changes: 6 additions & 0 deletions test/encoding_test/utf8unix.rb
@@ -0,0 +1,6 @@
# -*- coding: utf-8-unix -*-
module EncodingTest
def utf8unix_string
"大きな箱とねこ。"
end
end

0 comments on commit 2b78929

Please sign in to comment.