Skip to content

Instantly share code, notes, and snippets.

@wtnabe
Last active July 26, 2016 07:01
Show Gist options
  • Save wtnabe/682899f226cd33f87b8f to your computer and use it in GitHub Desktop.
Save wtnabe/682899f226cd33f87b8f to your computer and use it in GitHub Desktop.
RubyでShift_JISの範囲に収まっていない文字を見える化したい
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
@wtnabe
Copy link
Author

wtnabe commented Mar 7, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment