Skip to content

Commit

Permalink
Merge pull request sporkmonger#387 from isqad/master
Browse files Browse the repository at this point in the history
fix: avoid some allocations of strings
  • Loading branch information
sporkmonger committed Jul 3, 2021
2 parents 877ec17 + a87b243 commit e35926b
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module NormalizeCharacterClasses
"wais" => 210,
"ldap" => 389,
"prospero" => 1525
}
}.freeze

##
# Returns a URI object based on the parsed string.
Expand Down Expand Up @@ -472,7 +472,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 @@ -563,7 +567,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 @@ -1123,6 +1131,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 @@ -1142,7 +1151,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
@normalized_host
end

Expand Down

0 comments on commit e35926b

Please sign in to comment.