initialize_pattern(opts = {})
private
Constructs the default Hash of patterns
Show source
def initialize_pattern(opts = {})
ret = {}
ret[:ESCAPED] = escaped = (opts.delete(:ESCAPED) || PATTERN::ESCAPED)
ret[:UNRESERVED] = unreserved = opts.delete(:UNRESERVED) || PATTERN::UNRESERVED
ret[:RESERVED] = reserved = opts.delete(:RESERVED) || PATTERN::RESERVED
ret[:DOMLABEL] = opts.delete(:DOMLABEL) || PATTERN::DOMLABEL
ret[:TOPLABEL] = opts.delete(:TOPLABEL) || PATTERN::TOPLABEL
ret[:HOSTNAME] = hostname = opts.delete(:HOSTNAME)
ret[:URIC] = uric = "(?:[#{unreserved}#{reserved}]|#{escaped})"
ret[:URIC_NO_SLASH] = uric_no_slash = "(?:[#{unreserved};?:@&=+$,]|#{escaped})"
ret[:QUERY] = query = "#{uric}*"
ret[:FRAGMENT] = fragment = "#{uric}*"
unless hostname
ret[:HOSTNAME] = hostname = "(?:[a-zA-Z0-9\\-.]|%\\h\\h)+"
end
ret[:IPV4ADDR] = ipv4addr = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"
hex4 = "[#{PATTERN::HEX}]{1,4}"
lastpart = "(?:#{hex4}|#{ipv4addr})"
hexseq1 = "(?:#{hex4}:)*#{hex4}"
hexseq2 = "(?:#{hex4}:)*#{lastpart}"
ret[:IPV6ADDR] = ipv6addr = "(?:#{hexseq2}|(?:#{hexseq1})?::(?:#{hexseq2})?)"
ret[:IPV6REF] = ipv6ref = "\\[#{ipv6addr}\\]"
ret[:HOST] = host = "(?:#{hostname}|#{ipv4addr}|#{ipv6ref})"
ret[:PORT] = port = '\d*'
ret[:HOSTPORT] = hostport = "#{host}(?::#{port})?"
ret[:USERINFO] = userinfo = "(?:[#{unreserved};:&=+$,]|#{escaped})*"
pchar = "(?:[#{unreserved}:@&=+$,]|#{escaped})"
param = "#{pchar}*"
segment = "#{pchar}*(?:;#{param})*"
ret[:PATH_SEGMENTS] = path_segments = "#{segment}(?:/#{segment})*"
server = "(?:#{userinfo}@)?#{hostport}"
ret[:REG_NAME] = reg_name = "(?:[#{unreserved}$,;:@&=+]|#{escaped})+"
authority = "(?:#{server}|#{reg_name})"
ret[:REL_SEGMENT] = rel_segment = "(?:[#{unreserved};@&=+$,]|#{escaped})+"
ret[:SCHEME] = scheme = "[#{PATTERN::ALPHA}][\\-+.#{PATTERN::ALPHA}\\d]*"
ret[:ABS_PATH] = abs_path = "/#{path_segments}"
ret[:REL_PATH] = rel_path = "#{rel_segment}(?:#{abs_path})?"
ret[:NET_PATH] = net_path = "//#{authority}(?:#{abs_path})?"
ret[:HIER_PART] = hier_part = "(?:#{net_path}|#{abs_path})(?:\\?(?:#{query}))?"
ret[:OPAQUE_PART] = opaque_part = "#{uric_no_slash}#{uric}*"
ret[:ABS_URI] = abs_uri = "#{scheme}:(?:#{hier_part}|#{opaque_part})"
ret[:REL_URI] = rel_uri = "(?:#{net_path}|#{abs_path}|#{rel_path})(?:\\?#{query})?"
ret[:URI_REF] = "(?:#{abs_uri}|#{rel_uri})?(?:##{fragment})?"
ret[:X_ABS_URI] = "
(#{scheme}): (?# 1: scheme)
(?:
(#{opaque_part}) (?# 2: opaque)
|
(?:(?:
//(?:
(?:(?:(#{userinfo})@)? (?# 3: userinfo)
(?:(#{host})(?::(\\d*))?))? (?# 4: host, 5: port)
|
(#{reg_name}) (?# 6: registry)
)
|
(?!//)) (?# XXX: '//' is the mark for hostport)
(#{abs_path})? (?# 7: path)
)(?:\\?(#{query}))? (?# 8: query)
)
(?:\\#(#{fragment}))? (?# 9: fragment)
"
ret[:X_REL_URI] = "
(?:
(?:
//
(?:
(?:(#{userinfo})@)? (?# 1: userinfo)
(#{host})?(?::(\\d*))? (?# 2: host, 3: port)
|
(#{reg_name}) (?# 4: registry)
)
)
|
(#{rel_segment}) (?# 5: rel_segment)
)?
(#{abs_path})? (?# 6: abs_path)
(?:\\?(#{query}))? (?# 7: query)
(?:\\#(#{fragment}))? (?# 8: fragment)
"
ret
end