Skip to content

Commit

Permalink
move forcing utf8 encoding into a helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballlover723 committed Aug 31, 2021
1 parent 52c524b commit 8194318
Showing 1 changed file with 20 additions and 38 deletions.
58 changes: 20 additions & 38 deletions lib/addressable/uri.rb
Expand Up @@ -900,9 +900,7 @@ def normalized_scheme
end
end
# All normalized values should be UTF-8
if @normalized_scheme && @normalized_scheme.encoding != Encoding::UTF_8
@normalized_scheme.force_encoding(Encoding::UTF_8)
end
force_utf8_encoding_if_needed(@normalized_scheme)
@normalized_scheme
end

Expand Down Expand Up @@ -957,9 +955,7 @@ def normalized_user
end
end
# All normalized values should be UTF-8
if @normalized_user && @normalized_user.encoding != Encoding::UTF_8
@normalized_user.force_encoding(Encoding::UTF_8)
end
force_utf8_encoding_if_needed(@normalized_user)
@normalized_user
end

Expand Down Expand Up @@ -1016,11 +1012,7 @@ def normalized_password
end
end
# All normalized values should be UTF-8
if @normalized_password && (
@normalized_password.encoding != Encoding::UTF_8
)
@normalized_password.force_encoding(Encoding::UTF_8)
end
force_utf8_encoding_if_needed(@normalized_password)
@normalized_password
end

Expand Down Expand Up @@ -1088,11 +1080,7 @@ def normalized_userinfo
end
end
# All normalized values should be UTF-8
if @normalized_userinfo && (
@normalized_userinfo.encoding != Encoding::UTF_8
)
@normalized_userinfo.force_encoding(Encoding::UTF_8)
end
force_utf8_encoding_if_needed(@normalized_userinfo)
@normalized_userinfo
end

Expand Down Expand Up @@ -1159,9 +1147,7 @@ def normalized_host
end
end
# All normalized values should be UTF-8
if @normalized_host && @normalized_host.encoding != Encoding::UTF_8
@normalized_host.force_encoding(Encoding::UTF_8)
end
force_utf8_encoding_if_needed(@normalized_host)
@normalized_host
end

Expand Down Expand Up @@ -1279,11 +1265,7 @@ def normalized_authority
authority
end
# All normalized values should be UTF-8
if @normalized_authority && (
@normalized_authority.encoding != Encoding::UTF_8
)
@normalized_authority.force_encoding(Encoding::UTF_8)
end
force_utf8_encoding_if_needed(@normalized_authority)
@normalized_authority
end

Expand Down Expand Up @@ -1517,9 +1499,7 @@ def normalized_site
site_string
end
# All normalized values should be UTF-8
if @normalized_site && @normalized_site.encoding != Encoding::UTF_8
@normalized_site.force_encoding(Encoding::UTF_8)
end
force_utf8_encoding_if_needed(@normalized_site)
@normalized_site
end

Expand Down Expand Up @@ -1582,9 +1562,7 @@ def normalized_path
result
end
# All normalized values should be UTF-8
if @normalized_path && @normalized_path.encoding != Encoding::UTF_8
@normalized_path.force_encoding(Encoding::UTF_8)
end
force_utf8_encoding_if_needed(@normalized_path)
@normalized_path
end

Expand Down Expand Up @@ -1660,9 +1638,7 @@ def normalized_query(*flags)
component == "" ? nil : component
end
# All normalized values should be UTF-8
if @normalized_query && @normalized_query.encoding != Encoding::UTF_8
@normalized_query.force_encoding(Encoding::UTF_8)
end
force_utf8_encoding_if_needed(@normalized_query)
@normalized_query
end

Expand Down Expand Up @@ -1858,11 +1834,7 @@ def normalized_fragment
component == "" ? nil : component
end
# All normalized values should be UTF-8
if @normalized_fragment && (
@normalized_fragment.encoding != Encoding::UTF_8
)
@normalized_fragment.force_encoding(Encoding::UTF_8)
end
force_utf8_encoding_if_needed(@normalized_fragment)
@normalized_fragment
end

Expand Down Expand Up @@ -2570,5 +2542,15 @@ def remove_composite_values
remove_instance_variable(:@uri_string) if defined?(@uri_string)
remove_instance_variable(:@hash) if defined?(@hash)
end

##
# Converts the string to be UTF-8 if it is not already UTF-8
#
# @api private
def force_utf8_encoding_if_needed(str)
if str && str.encoding != Encoding::UTF_8
str.force_encoding(Encoding::UTF_8)
end
end
end
end

0 comments on commit 8194318

Please sign in to comment.