@@ -38,11 +38,7 @@ def normalize(value)
38
38
#
39
39
# Attempt to normalize and validate the given string.
40
40
#
41
- # On Ruby 1.8 the string is checked by stripping out all non UTF-8
42
- # characters and comparing the converted string to the original. If they
43
- # do not match then the string is considered invalid.
44
- #
45
- # On Ruby 1.9+, the string is validate by checking that the string encoding
41
+ # The string is validated by checking that the string encoding
46
42
# is UTF-8 and that the string content matches the encoding. If the string
47
43
# is not an expected encoding then it is converted to UTF-8.
48
44
#
@@ -51,27 +47,16 @@ def normalize(value)
51
47
# @param value [String]
52
48
# @return [void]
53
49
54
- if RUBY_VERSION . match? ( /^1\. 8/ )
55
- require 'iconv'
56
-
57
- def normalize_string ( value )
58
- converted = Iconv . conv ( 'UTF-8//IGNORE' , 'UTF-8' , value )
59
- raise NormalizationError , "String #{ value . inspect } is not valid UTF-8" if converted != value
50
+ def normalize_string ( value )
51
+ value = value . encode ( Encoding ::UTF_8 )
60
52
61
- value
53
+ unless value . valid_encoding?
54
+ raise NormalizationError , "String #{ value . inspect } doesn't match the reported encoding #{ value . encoding } "
62
55
end
63
- else
64
- def normalize_string ( value )
65
- value = value . encode ( Encoding ::UTF_8 )
66
56
67
- unless value . valid_encoding?
68
- raise NormalizationError , "String #{ value . inspect } doesn't match the reported encoding #{ value . encoding } "
69
- end
70
-
71
- value
72
- rescue EncodingError
73
- raise NormalizationError , "String encoding #{ value . encoding } is not UTF-8 and could not be converted to UTF-8"
74
- end
57
+ value
58
+ rescue EncodingError
59
+ raise NormalizationError , "String encoding #{ value . encoding } is not UTF-8 and could not be converted to UTF-8"
75
60
end
76
61
77
62
# Validate all elements of the array.
0 commit comments