@@ -38,11 +38,7 @@ def normalize(value)
3838 #
3939 # Attempt to normalize and validate the given string.
4040 #
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
4642 # is UTF-8 and that the string content matches the encoding. If the string
4743 # is not an expected encoding then it is converted to UTF-8.
4844 #
@@ -51,27 +47,16 @@ def normalize(value)
5147 # @param value [String]
5248 # @return [void]
5349
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 )
6052
61- value
53+ unless value . valid_encoding?
54+ raise NormalizationError , "String #{ value . inspect } doesn't match the reported encoding #{ value . encoding } "
6255 end
63- else
64- def normalize_string ( value )
65- value = value . encode ( Encoding ::UTF_8 )
6656
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"
7560 end
7661
7762 # Validate all elements of the array.
0 commit comments