[前][次][番号順一覧][スレッド一覧]

ruby-changes:65932

From: Lukas <ko1@a...>
Date: Thu, 22 Apr 2021 14:57:32 +0900 (JST)
Subject: [ruby-changes:65932] c46a4b8c7f (master): [ruby/uri] Optimize URI#hostname and URI#hostname=

https://git.ruby-lang.org/ruby.git/commit/?id=c46a4b8c7f

From c46a4b8c7f434110c30c9587e02387283076579f Mon Sep 17 00:00:00 2001
From: Lukas Zapletal <lzap+git@r...>
Date: Wed, 7 Oct 2020 13:23:00 +0200
Subject: [ruby/uri] Optimize URI#hostname and URI#hostname=

https://github.com/ruby/uri/commit/3b7ccfd835
---
 lib/uri/generic.rb       | 4 ++--
 test/uri/test_generic.rb | 4 ++++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb
index 7de62b6..cfa0de6 100644
--- a/lib/uri/generic.rb
+++ b/lib/uri/generic.rb
@@ -643,7 +643,7 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/generic.rb#L643
     #
     def hostname
       v = self.host
-      /\A\[(.*)\]\z/ =~ v ? $1 : v
+      v&.start_with?('[') && v.end_with?(']') ? v[1..-2] : v
     end
 
     # Sets the host part of the URI as the argument with brackets for IPv6 addresses.
@@ -659,7 +659,7 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/generic.rb#L659
     # it is wrapped with brackets.
     #
     def hostname=(v)
-      v = "[#{v}]" if /\A\[.*\]\z/ !~ v && /:/ =~ v
+      v = "[#{v}]" if !(v&.start_with?('[') && v&.end_with?(']')) && v&.index(':')
       self.host = v
     end
 
diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb
index b449a0a..d122587 100644
--- a/test/uri/test_generic.rb
+++ b/test/uri/test_generic.rb
@@ -799,8 +799,12 @@ class URI::TestGeneric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/uri/test_generic.rb#L799
 
     u = URI("http://foo/bar")
     assert_equal("http://foo/bar", u.to_s)
+    u.hostname = "[::1]"
+    assert_equal("http://[::1]/bar", u.to_s)
     u.hostname = "::1"
     assert_equal("http://[::1]/bar", u.to_s)
+    u.hostname = ""
+    assert_equal("http:///bar", u.to_s)
   end
 
   def test_build
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]