Skip to content

Commit 9e44c5e

Browse files
jpervillegkellogg
authored andcommitted
Refactor and reverse some regexp matches
1 parent c5e0f7f commit 9e44c5e

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

lib/rdf/model/uri.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def length
343343
# @return [Boolean] `true` or `false`
344344
# @since 0.3.9
345345
def valid?
346-
to_s.match(RDF::URI::IRI) || false
346+
RDF::URI::IRI.match(to_s) || false
347347
end
348348

349349
##
@@ -829,7 +829,7 @@ def object
829829
def parse(value)
830830
value = value.to_s.dup.force_encoding(Encoding::ASCII_8BIT)
831831
parts = {}
832-
if matchdata = value.to_s.match(IRI_PARTS)
832+
if matchdata = IRI_PARTS.match(value)
833833
scheme, authority, path, query, fragment = matchdata[1..-1]
834834
userinfo, hostport = authority.to_s.split('@', 2)
835835
hostport, userinfo = userinfo, nil unless hostport
@@ -927,11 +927,13 @@ def normalized_password
927927
::URI.encode(::URI.decode(password), /[^#{IUNRESERVED}|#{SUB_DELIMS}]/) if password
928928
end
929929

930+
HOST_FROM_AUTHORITY_RE = /(?:[^@]+@)?([^:]+)(?::.*)?$/.freeze
931+
930932
##
931933
# @return [String]
932934
def host
933935
object.fetch(:host) do
934-
@object[:host] = ($1 if @object[:authority].to_s.match(/(?:[^@]+@)?([^:]+)(?::.*)?$/))
936+
@object[:host] = ($1 if HOST_FROM_AUTHORITY_RE.match(@object[:authority]))
935937
end
936938
end
937939

@@ -953,11 +955,13 @@ def normalized_host
953955
normalize_segment(host, IHOST, true).chomp('.') if host
954956
end
955957

958+
PORT_FROM_AUTHORITY_RE = /:(\d+)$/.freeze
959+
956960
##
957961
# @return [String]
958962
def port
959963
object.fetch(:port) do
960-
@object[:port] = ($1 if @object[:authority].to_s.match(/:(\d+)$/))
964+
@object[:port] = ($1 if PORT_FROM_AUTHORITY_RE.match(@object[:authority]))
961965
end
962966
end
963967

lib/rdf/vocabulary.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ def dup
623623
# @since 0.3.9
624624
def valid?
625625
# Validate relative to RFC3987
626-
to_s.match(RDF::URI::IRI) || false
626+
RDF::URI::IRI.match(to_s) || false
627627
end
628628

629629
##

0 commit comments

Comments
 (0)