Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid some allocations of strings #387

Merged
merged 3 commits into from
Jul 3, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ module CharacterClasses
"wais" => 210,
"ldap" => 389,
"prospero" => 1525
}
}.freeze

##
# Returns a URI object based on the parsed string.
Expand Down Expand Up @@ -463,7 +463,11 @@ def self.unencode(uri, return_type=String, leave_encoded='')
uri = uri.dup
# Seriously, only use UTF-8. I'm really not kidding!
uri.force_encoding("utf-8")
leave_encoded = leave_encoded.dup.force_encoding("utf-8")

unless leave_encoded.empty?
leave_encoded = leave_encoded.dup.force_encoding("utf-8")
end

result = uri.gsub(/%[0-9a-f]{2}/iu) do |sequence|
c = sequence[1..3].to_i(16).chr
c.force_encoding("utf-8")
Expand Down Expand Up @@ -554,7 +558,11 @@ def self.normalize_component(component, character_class=
end.flatten.join('|')})"
end

character_class = /[^#{character_class}]#{leave_re}/
character_class = if leave_re
/[^#{character_class}]#{leave_re}/
else
/[^#{character_class}]/
end
end
# We can't perform regexps on invalid UTF sequences, but
# here we need to, so switch to ASCII.
Expand Down Expand Up @@ -1114,6 +1122,7 @@ def host
# @return [String] The host component, normalized.
def normalized_host
return nil unless self.host

@normalized_host ||= begin
if !self.host.strip.empty?
result = ::Addressable::IDNA.to_ascii(
Expand All @@ -1132,7 +1141,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.empty?
@normalized_host.force_encoding(Encoding::UTF_8)
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it guaranteed that @normalized_host will always respond to #empty?. The previous code guarded against nil (Maybe it wasn't necessary or the test coverage we have isn't sufficient, I don't know)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah...ok, but @normalized_host can be empty string, so it is proper way?

if @normalized_host && !@normalized_host.empty?
  @normalized_host.force_encoding(Encoding::UTF_8)
end

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if @normalized_host is an empty string but not UTF-8 encoded..?

Copy link
Contributor Author

@isqad isqad May 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it will not be have matter because url without host is wrong url

🤔

it is may be have sense to add condition on emptiness at this line:

authority << self.normalized_host

@normalized_host
end

Expand Down