Last active
July 26, 2016 07:01
-
-
Save wtnabe/682899f226cd33f87b8f to your computer and use it in GitHub Desktop.
RubyでShift_JISの範囲に収まっていない文字を見える化したい
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
begin | |
open(f, 'rb:Shift_JIS:UTF-8').read | |
rescue Encoding::UndefinedConversionError => e | |
fulltilde = "\x81\x60".force_encoding("Windows-31J") | |
tmptab = "\v".force_encoding("Windows-31J") | |
w = open(f, 'rb:Windows-31J').read.gsub(fulltilde, tmptab) | |
s = w.encode('Shift_JIS', undef: :replace, replace: '??'.colorize(:magenta)) | |
diff = s.encode('Windows-31J', undef: :replace, replace: '??'.colorize(:magenta)).lines - w.lines | |
if diff.size > 0 | |
puts "" | |
puts f | |
puts diff.map {|e| e.gsub(tmptab, fulltilde).encode('UTF-8')} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
colorize は https://github.com/fazibear/colorize です