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 18, 2019
1 parent 5894c95 commit 686dbbb
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lib/addressable/uri.rb
Original file line number Diff line number Diff line change
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,7 @@ 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 +1051,7 @@ 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 +1118,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 +1238,7 @@ 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 +1474,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 +1539,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 +1612,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 +1808,7 @@ 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 686dbbb

Please sign in to comment.