Skip to content

Commit

Permalink
Check to see if @normalized_* variables are already UTF-8 before
Browse files Browse the repository at this point in the history
calling force_encoding on them

This prevents unnecessary mutation of these variables when already set
and appropriately encoded. Necessary for our use case as we freeze our
Addressable::URI objects to ensure they don't get changed when passed around
  • Loading branch information
ACBullen committed Apr 23, 2019
1 parent 3bf9c6e commit a57778a
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions lib/addressable/uri.rb
Expand Up @@ -865,7 +865,9 @@ def normalized_scheme
end
end
# All normalized values should be UTF-8
@normalized_scheme.force_encoding(Encoding::UTF_8) if @normalized_scheme
if @normalized_scheme && @normalized_scheme.encoding != Encoding::UTF_8
@normalized_scheme.force_encoding(Encoding::UTF_8)
end
@normalized_scheme
end

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

Expand Down Expand Up @@ -977,7 +981,9 @@ def normalized_password
end
end
# All normalized values should be UTF-8
if @normalized_password
if @normalized_password && (
@normalized_password.encoding != Encoding::UTF_8
)
@normalized_password.force_encoding(Encoding::UTF_8)
end
@normalized_password
Expand Down Expand Up @@ -1047,7 +1053,9 @@ def normalized_userinfo
end
end
# All normalized values should be UTF-8
if @normalized_userinfo
if @normalized_userinfo && (
@normalized_userinfo.encoding != Encoding::UTF_8
)
@normalized_userinfo.force_encoding(Encoding::UTF_8)
end
@normalized_userinfo
Expand Down Expand Up @@ -1114,7 +1122,9 @@ def normalized_host
end
end
# All normalized values should be UTF-8
@normalized_host.force_encoding(Encoding::UTF_8) if @normalized_host
if @normalized_host && @normalized_host.encoding != Encoding::UTF_8
@normalized_host.force_encoding(Encoding::UTF_8)
end
@normalized_host
end

Expand Down Expand Up @@ -1232,7 +1242,9 @@ def normalized_authority
authority
end
# All normalized values should be UTF-8
if @normalized_authority
if @normalized_authority && (
@normalized_authority.encoding != Encoding::UTF_8
)
@normalized_authority.force_encoding(Encoding::UTF_8)
end
@normalized_authority
Expand Down Expand Up @@ -1468,7 +1480,9 @@ def normalized_site
site_string
end
# All normalized values should be UTF-8
@normalized_site.force_encoding(Encoding::UTF_8) if @normalized_site
if @normalized_site && @normalized_site.encoding != Encoding::UTF_8
@normalized_site.force_encoding(Encoding::UTF_8)
end
@normalized_site
end

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

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

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

0 comments on commit a57778a

Please sign in to comment.