From b79faa1fdea52f825a0ff774fe8254f49a5aee4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Joubert?= <14306+czj@users.noreply.github.com> Date: Tue, 10 Nov 2026 20:00:17 +0100 Subject: [PATCH 1/2] Allocate less in URI.parse INITIAL_SCHEMES has been empty since 86de79c moved it to common.rb, before any scheme registers, so every URI.for went through Schemes.escape, which builds two strings. Map the bundled schemes to their constant names in INITIAL_SCHEMES, and keep looking the class up in Schemes so a re-registered scheme still wins. set_userinfo no longer splits a nil userinfo, whose [nil, nil] array was discarded. Assisted-by: Claude Opus 5.5 [Claude Code] --- lib/uri/common.rb | 10 ++++------ lib/uri/generic.rb | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/uri/common.rb b/lib/uri/common.rb index 971a684..624209b 100644 --- a/lib/uri/common.rb +++ b/lib/uri/common.rb @@ -163,7 +163,8 @@ def self.scheme_list end # :stopdoc: - INITIAL_SCHEMES = scheme_list + # Constant names of the bundled schemes, by lowercase name, to skip Schemes.escape. + INITIAL_SCHEMES = %w[file ftp http https ldap ldaps mailto ws wss].map { |scheme| [scheme, scheme.upcase.to_sym] }.to_h private_constant :INITIAL_SCHEMES Ractor.make_shareable(INITIAL_SCHEMES) if defined?(Ractor) # :startdoc: @@ -185,11 +186,8 @@ def self.scheme_list # # => # # def self.for(scheme, *arguments, default: Generic) - const_name = Schemes.escape(scheme) - - uri_class = INITIAL_SCHEMES[const_name] - uri_class ||= Schemes.find(const_name) - uri_class ||= default + const_name = INITIAL_SCHEMES[scheme] || Schemes.escape(scheme) + uri_class = Schemes.find(const_name) || default return uri_class.new(scheme, *arguments) end diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index fce8a8a..30d9cec 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -507,7 +507,7 @@ def password=(password) # See also URI::Generic.userinfo=. # def set_userinfo(user, password = nil) - unless password + if user && !password user, password = split_userinfo(user) end @user = user From 13c3b9ca859fa44ea2bf3702792f9b28ec4e21c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Joubert?= <14306+czj@users.noreply.github.com> Date: Wed, 11 Nov 2026 09:44:06 +0100 Subject: [PATCH 2/2] Test that a re-registered bundled scheme wins in URI.parse Assisted-by: Claude Opus 5.5 [Claude Code] --- test/uri/test_common.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/uri/test_common.rb b/test/uri/test_common.rb index 5692640..6dfc7ee 100644 --- a/test/uri/test_common.rb +++ b/test/uri/test_common.rb @@ -114,6 +114,15 @@ def test_register_scheme_lowercase assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort) end + def test_register_scheme_overrides_bundled_scheme + http = Class.new(URI::HTTP) + EnvUtil.suppress_warning { URI.register_scheme 'HTTP', http } + assert_equal http, URI.parse('http://localhost').class + assert_equal http, URI.for('http', nil, 'localhost', nil, nil, '/', nil, nil, nil).class + ensure + EnvUtil.suppress_warning { URI.register_scheme 'HTTP', URI::HTTP } + end + def test_register_scheme_with_symbols # Valid schemes from https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml list = []