@@ -899,7 +899,7 @@ def normalized_scheme
899
899
end
900
900
end
901
901
# All normalized values should be UTF-8
902
- @normalized_scheme . force_encoding ( Encoding :: UTF_8 ) if @normalized_scheme
902
+ force_utf8_encoding_if_needed ( @normalized_scheme )
903
903
@normalized_scheme
904
904
end
905
905
@@ -954,7 +954,7 @@ def normalized_user
954
954
end
955
955
end
956
956
# All normalized values should be UTF-8
957
- @normalized_user . force_encoding ( Encoding :: UTF_8 ) if @normalized_user
957
+ force_utf8_encoding_if_needed ( @normalized_user )
958
958
@normalized_user
959
959
end
960
960
@@ -1011,9 +1011,7 @@ def normalized_password
1011
1011
end
1012
1012
end
1013
1013
# All normalized values should be UTF-8
1014
- if @normalized_password
1015
- @normalized_password . force_encoding ( Encoding ::UTF_8 )
1016
- end
1014
+ force_utf8_encoding_if_needed ( @normalized_password )
1017
1015
@normalized_password
1018
1016
end
1019
1017
@@ -1081,9 +1079,7 @@ def normalized_userinfo
1081
1079
end
1082
1080
end
1083
1081
# All normalized values should be UTF-8
1084
- if @normalized_userinfo
1085
- @normalized_userinfo . force_encoding ( Encoding ::UTF_8 )
1086
- end
1082
+ force_utf8_encoding_if_needed ( @normalized_userinfo )
1087
1083
@normalized_userinfo
1088
1084
end
1089
1085
@@ -1150,9 +1146,7 @@ def normalized_host
1150
1146
end
1151
1147
end
1152
1148
# All normalized values should be UTF-8
1153
- if @normalized_host && !@normalized_host . empty?
1154
- @normalized_host . force_encoding ( Encoding ::UTF_8 )
1155
- end
1149
+ force_utf8_encoding_if_needed ( @normalized_host )
1156
1150
@normalized_host
1157
1151
end
1158
1152
@@ -1270,9 +1264,7 @@ def normalized_authority
1270
1264
authority
1271
1265
end
1272
1266
# All normalized values should be UTF-8
1273
- if @normalized_authority
1274
- @normalized_authority . force_encoding ( Encoding ::UTF_8 )
1275
- end
1267
+ force_utf8_encoding_if_needed ( @normalized_authority )
1276
1268
@normalized_authority
1277
1269
end
1278
1270
@@ -1506,7 +1498,7 @@ def normalized_site
1506
1498
site_string
1507
1499
end
1508
1500
# All normalized values should be UTF-8
1509
- @normalized_site . force_encoding ( Encoding :: UTF_8 ) if @normalized_site
1501
+ force_utf8_encoding_if_needed ( @normalized_site )
1510
1502
@normalized_site
1511
1503
end
1512
1504
@@ -1569,7 +1561,7 @@ def normalized_path
1569
1561
result
1570
1562
end
1571
1563
# All normalized values should be UTF-8
1572
- @normalized_path . force_encoding ( Encoding :: UTF_8 ) if @normalized_path
1564
+ force_utf8_encoding_if_needed ( @normalized_path )
1573
1565
@normalized_path
1574
1566
end
1575
1567
@@ -1645,7 +1637,7 @@ def normalized_query(*flags)
1645
1637
component == "" ? nil : component
1646
1638
end
1647
1639
# All normalized values should be UTF-8
1648
- @normalized_query . force_encoding ( Encoding :: UTF_8 ) if @normalized_query
1640
+ force_utf8_encoding_if_needed ( @normalized_query )
1649
1641
@normalized_query
1650
1642
end
1651
1643
@@ -1841,9 +1833,7 @@ def normalized_fragment
1841
1833
component == "" ? nil : component
1842
1834
end
1843
1835
# All normalized values should be UTF-8
1844
- if @normalized_fragment
1845
- @normalized_fragment . force_encoding ( Encoding ::UTF_8 )
1846
- end
1836
+ force_utf8_encoding_if_needed ( @normalized_fragment )
1847
1837
@normalized_fragment
1848
1838
end
1849
1839
@@ -2556,5 +2546,15 @@ def remove_composite_values
2556
2546
remove_instance_variable ( :@uri_string ) if defined? ( @uri_string )
2557
2547
remove_instance_variable ( :@hash ) if defined? ( @hash )
2558
2548
end
2549
+
2550
+ ##
2551
+ # Converts the string to be UTF-8 if it is not already UTF-8
2552
+ #
2553
+ # @api private
2554
+ def force_utf8_encoding_if_needed ( str )
2555
+ if str && str . encoding != Encoding ::UTF_8
2556
+ str . force_encoding ( Encoding ::UTF_8 )
2557
+ end
2558
+ end
2559
2559
end
2560
2560
end
0 commit comments