Created
February 20, 2012 12:48
-
-
Save ogijun/1869060 to your computer and use it in GitHub Desktop.
Guessing the encoding of given string by trying only Japanese major encodings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ugly_guess_jp str | |
%w[UTF-8 EUC-JP Shift_JIS eucJP-ms Windows-31J].find do |encoding| | |
begin | |
tested = str.force_encoding(encoding) | |
tested.valid_encoding? && tested.encode('UTF-8').valid_encoding? | |
rescue Encoding::UndefinedConversionError => e | |
nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment