String#replace_invalid_bytes that replaces invalid bytes in a string
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
# -*- coding: utf-8 -*- | |
class String | |
def replace_invalid_bytes(replace = nil) | |
if block_given? | |
if replace | |
warn 'block not used' if $VERBOSE | |
end | |
elsif replace.nil? | |
raise ArgumentError, 'an argument or a block must be given' | |
end | |
return self if valid_encoding? | |
if replace | |
r = String.try_convert(replace) or | |
raise TypeError, "can't convert #{replace.class} into String" | |
each_char.inject('') { |s, c| | |
s << (c.valid_encoding? ? c : r) | |
} | |
else | |
each_char.inject('') { |s, c| | |
s << (c.valid_encoding? ? c : yield(*c.bytes)) | |
} | |
end | |
end | |
end | |
puts "あい\x80\xffう".replace_invalid_bytes('[?]') | |
#=> あい[?][?]う | |
puts "あい\x80\xffう".replace_invalid_bytes { |b| "<%02X>" % b } | |
#=> あい<80><FF>う |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment