1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
# coding: utf-8
module EncodingHelper
def binary_string(str)
str = str.force_encoding("binary") if str.respond_to?(:force_encoding)
str
end
# On M17N aware VMs, recursively checks strings and containers with strings
# to ensure everything is UTF-8 encoded
#
def check_utf8(obj)
return unless RUBY_VERSION >= "1.9"
case obj
when Array
obj.each { |item| check_utf8(item) }
when Hash
obj.each { |key, value|
check_utf8(key)
check_utf8(value)
}
when String
assert_equal obj.encoding, Encoding.find("utf-8")
assert obj.valid_encoding?
else
return
end
end
# On M17N aware VMs, recursively checks strings and containers with strings
# to ensure everything is Binary encoded
#
def check_binary(obj)
return unless RUBY_VERSION >= "1.9"
case obj
when Array
obj.each { |item| check_utf8(item) }
when Hash
obj.each { |key, value|
check_utf8(key)
check_utf8(value)
}
when String
assert_equal obj.encoding, Encoding.find("binary")
assert obj.valid_encoding?
else
return
end
end
end
|