@@ -81,7 +81,7 @@ class URI
8181 IRI = Regexp . compile ( "^#{ SCHEME } :(?:#{ IHIER_PART } )(?:\\ ?#{ IQUERY } )?(?:\\ ##{ IFRAGMENT } )?$" ) . freeze
8282
8383 # Split an IRI into it's component parts
84- IRI_PARTS = /^(?:([^:\/ ?#]+):)?(?:\/ \/ ([^\/ ?#]*))?([^?#]*)(\? [^#]*)?(#.*)?$/
84+ IRI_PARTS = /^(?:([^:\/ ?#]+):)?(?:\/ \/ ([^\/ ?#]*))?([^?#]*)(\? [^#]*)?(#.*)?$/ . freeze
8585
8686 # Remove dot expressions regular expressions
8787 RDS_2A = /^\. ?\. \/ (.*)$/ . freeze
@@ -141,10 +141,9 @@ def self.cache
141141 #
142142 # @param (see #initialize)
143143 # @return [RDF::URI] an immutable, frozen URI object
144- def self . intern ( *args )
145- str = args . first
144+ def self . intern ( str , *args )
146145 args << { } unless args . last . is_a? ( Hash ) # FIXME: needed until #to_hash is removed to avoid DEPRECATION warning.
147- ( cache [ ( str = str . to_s ) . to_sym ] ||= self . new ( *args ) ) . freeze
146+ ( cache [ ( str = str . to_s ) . to_sym ] ||= self . new ( str , *args ) ) . freeze
148147 end
149148
150149 ##
@@ -224,6 +223,7 @@ def self.normalize_path(path)
224223 # @param [Boolean] validate (false)
225224 # @param [Boolean] canonicalize (false)
226225 def initialize ( *args , validate : false , canonicalize : false , **options )
226+ @value = @object = @hash = nil
227227 uri = args . first
228228 if uri
229229 @value = uri . to_s
@@ -344,7 +344,7 @@ def length
344344 # @return [Boolean] `true` or `false`
345345 # @since 0.3.9
346346 def valid?
347- to_s . match ( RDF ::URI ::IRI ) || false
347+ RDF ::URI ::IRI . match ( to_s ) || false
348348 end
349349
350350 ##
@@ -796,7 +796,8 @@ def inspect
796796 # lexical representation of URI, either absolute or relative
797797 # @return [String]
798798 def value
799- @value ||= [
799+ return @value if @value
800+ @value = [
800801 ( "#{ scheme } :" if absolute? ) ,
801802 ( "//#{ authority } " if authority ) ,
802803 path ,
@@ -810,15 +811,15 @@ def value
810811 #
811812 # @return [Integer]
812813 def hash
813- @hash ||= ( value . hash * -1 )
814+ @hash || @hash = ( value . hash * -1 )
814815 end
815816
816817 ##
817818 # Returns object representation of this URI, broken into components
818819 #
819820 # @return [Hash{Symbol => String}]
820821 def object
821- @object ||= parse ( @value )
822+ @object || @object = parse ( @value )
822823 end
823824 alias_method :to_h , :object
824825
@@ -830,8 +831,8 @@ def object
830831 def parse ( value )
831832 value = value . to_s . dup . force_encoding ( Encoding ::ASCII_8BIT )
832833 parts = { }
833- if matchdata = value . to_s . match ( IRI_PARTS )
834- scheme , authority , path , query , fragment = matchdata . to_a [ 1 ..-1 ]
834+ if matchdata = IRI_PARTS . match ( value )
835+ scheme , authority , path , query , fragment = matchdata [ 1 ..-1 ]
835836 userinfo , hostport = authority . to_s . split ( '@' , 2 )
836837 hostport , userinfo = userinfo , nil unless hostport
837838 user , password = userinfo . to_s . split ( ':' , 2 )
@@ -928,11 +929,13 @@ def normalized_password
928929 ::URI . encode ( ::URI . decode ( password ) , /[^#{ IUNRESERVED } |#{ SUB_DELIMS } ]/ ) if password
929930 end
930931
932+ HOST_FROM_AUTHORITY_RE = /(?:[^@]+@)?([^:]+)(?::.*)?$/ . freeze
933+
931934 ##
932935 # @return [String]
933936 def host
934937 object . fetch ( :host ) do
935- @object [ :host ] = ( $1 if @object [ :authority ] . to_s . match ( /(?:[^@]+@)?([^:]+)(?::.*)?$/ ) )
938+ @object [ :host ] = ( $1 if HOST_FROM_AUTHORITY_RE . match ( @object [ :authority ] ) )
936939 end
937940 end
938941
@@ -954,11 +957,13 @@ def normalized_host
954957 normalize_segment ( host , IHOST , true ) . chomp ( '.' ) if host
955958 end
956959
960+ PORT_FROM_AUTHORITY_RE = /:(\d +)$/ . freeze
961+
957962 ##
958963 # @return [String]
959964 def port
960965 object . fetch ( :port ) do
961- @object [ :port ] = ( $1 if @object [ :authority ] . to_s . match ( /:( \d +)$/ ) )
966+ @object [ :port ] = ( $1 if PORT_FROM_AUTHORITY_RE . match ( @object [ :authority ] ) )
962967 end
963968 end
964969
0 commit comments