Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use match? if available #1231

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/mail/encodings.rb
Expand Up @@ -73,7 +73,7 @@ def Encodings.transcode_charset(str, from_charset, to_charset = 'UTF-8')
# Mail::Encodings.param_encode("This is fun") #=> "us-ascii'en'This%20is%20fun"
def Encodings.param_encode(str)
case
when str.ascii_only? && str =~ TOKEN_UNSAFE
when str.ascii_only? && ::Mail::Utilities.match?(str, TOKEN_UNSAFE)
%Q{"#{str}"}
when str.ascii_only?
str
Expand Down Expand Up @@ -122,7 +122,7 @@ def Encodings.decode_encode(str, output_type)
# String has to be of the format =?<encoding>?[QB]?<string>?=
def Encodings.value_decode(str)
# Optimization: If there's no encoded-words in the string, just return it
return str unless str =~ ENCODED_VALUE
return str unless ::Mail::Utilities.match?(str, ENCODED_VALUE)

lines = collapse_adjacent_encodings(str)

Expand Down
2 changes: 1 addition & 1 deletion lib/mail/field.rb
Expand Up @@ -130,7 +130,7 @@ def split(raw_field) #:nodoc:
if raw_field.index(Constants::COLON)
name, value = raw_field.split(Constants::COLON, 2)
name.rstrip!
if name =~ /\A#{Constants::FIELD_NAME}\z/
if ::Mail::Utilities.match?(name, /\A#{Constants::FIELD_NAME}\z/)
[ name.rstrip, value.strip ]
else
Kernel.warn "WARNING: Ignoring unparsable header #{raw_field.inspect}: invalid header name syntax: #{name.inspect}"
Expand Down
2 changes: 1 addition & 1 deletion lib/mail/fields/common_address_field.rb
Expand Up @@ -121,7 +121,7 @@ def encode_if_needed(val, val_charset = charset) #:nodoc:
if 'string'.respond_to?(:encoding)
# Pass through UTF-8 addresses
def utf8_if_needed(val, val_charset)
if val_charset =~ /\AUTF-?8\z/i
if ::Mail::Utilities.match?(val_charset, /\AUTF-?8\z/i)
val
elsif val.encoding == Encoding::UTF_8
val
Expand Down
4 changes: 2 additions & 2 deletions lib/mail/fields/content_type_field.rb
Expand Up @@ -134,7 +134,7 @@ def sanitize(val)
# Microsoft helper:
# Handles 'type/subtype;ISO-8559-1'
"#{$1}/#{$2}; charset=#{Utilities.quote_atom($3)}"
when val.chomp =~ /^text;?$/i
when ::Mail::Utilities.match?(val.chomp, /^text;?$/i)
# Handles 'text;' and 'text'
"text/plain;"
when val.chomp =~ /^(\w+);\s(.*)$/i
Expand All @@ -153,7 +153,7 @@ def sanitize(val)
params = params.map { |i| i.split(/\s*\=\s*/, 2) }
params = params.map { |i| "#{i[0]}=#{Utilities.dquote(i[1].to_s.gsub(/;$/,""))}" }.join('; ')
"#{type}; #{params}"
when val =~ /^\s*$/
when ::Mail::Utilities.match?(val, /^\s*$/)
'text/plain'
else
val
Expand Down
19 changes: 10 additions & 9 deletions lib/mail/message.rb
Expand Up @@ -294,7 +294,7 @@ def reply(*args, &block)
reply.references ||= bracketed_message_id
end
if subject
reply.subject = subject =~ /^Re:/i ? subject : "Re: #{subject}"
reply.subject = ::Mail::Utilities.match?(subject, /^Re:/i) ? subject : "Re: #{subject}"
end
if reply_to || from
reply.to = self[reply_to ? :reply_to : :from].to_s
Expand Down Expand Up @@ -1314,11 +1314,12 @@ def bcc_addrs
# mail['foo'] = '1234'
# mail['foo'].to_s #=> '1234'
def []=(name, value)
if name.to_s == 'body'
str_name = name.to_s
if str_name == 'body'
self.body = value
elsif name.to_s =~ /content[-_]type/i
elsif ::Mail::Utilities.match?(str_name, /content[-_]type/i)
header[name] = value
elsif name.to_s == 'charset'
elsif str_name == 'charset'
self.charset = value
else
header[name] = value
Expand Down Expand Up @@ -1524,17 +1525,17 @@ def content_type_parameters

# Returns true if the message is multipart
def multipart?
has_content_type? ? !!(main_type =~ /^multipart$/i) : false
has_content_type? ? ::Mail::Utilities.match?(main_type, /^multipart$/i) : false
end

# Returns true if the message is a multipart/report
def multipart_report?
multipart? && sub_type =~ /^report$/i
multipart? && ::Mail::Utilities.match?(sub_type, /^report$/i)
end

# Returns true if the message is a multipart/report; report-type=delivery-status;
def delivery_status_report?
multipart_report? && content_type_parameters['report-type'] =~ /^delivery-status$/i
multipart_report? && ::Mail::Utilities.match?(content_type_parameters['report-type'], /^delivery-status$/i)
end

# returns the part in a multipart/report email that has the content-type delivery-status
Expand Down Expand Up @@ -1853,7 +1854,7 @@ def self.from_yaml(str)
m.transport_encoding(v)
when k == 'multipart_body'
v.map {|part| m.add_part Mail::Part.from_yaml(part) }
when k =~ /^@/
when ::Mail::Utilities.match?(k, /^@/)
m.instance_variable_set(k.to_sym, v)
end
end
Expand Down Expand Up @@ -1960,7 +1961,7 @@ def is_marked_for_delete?
end

def text?
has_content_type? ? !!(main_type =~ /^text$/i) : false
has_content_type? ? ::Mail::Utilities.match?(main_type, /^text$/i) : false
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/mail/multibyte/chars.rb
Expand Up @@ -52,7 +52,7 @@ def initialize(string) #:nodoc:

# Forward all undefined methods to the wrapped string.
def method_missing(method, *args, &block)
if method.to_s =~ /!$/
if ::Mail::Utilities.match?(method.to_s, /!$/)
@wrapped_string.__send__(method, *args, &block)
self
else
Expand Down
2 changes: 1 addition & 1 deletion lib/mail/smtp_envelope.rb
Expand Up @@ -47,7 +47,7 @@ def validate_addr(addr_name, addr)
raise ArgumentError, "SMTP #{addr_name} address may not exceed #{MAX_ADDRESS_BYTESIZE} bytes: #{addr.inspect}"
end

if /[\r\n]/ =~ addr
if ::Mail::Utilities.match?(addr, /[\r\n]/)
raise ArgumentError, "SMTP #{addr_name} address may not contain CR or LF line breaks: #{addr.inspect}"
end

Expand Down
11 changes: 11 additions & 0 deletions lib/mail/utilities.rb
Expand Up @@ -162,6 +162,17 @@ def uri_parser
@uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI
end

# use optimized match? if available
if ''.respond_to?(:match?)
def match?( str, pattern )
str ? str.match?(pattern) : false
end
else
def match?( str, pattern )
!!(str =~ pattern)
end
end

# Matches two objects with their to_s values case insensitively
#
# Example:
Expand Down