Skip to content

Commit

Permalink
Removed duplication from Regex's containing [\w\d]+ as \w contains \d
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikel Lindsaar committed Dec 23, 2010
1 parent fa452ee commit 09e02bb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/mail/fields/common/parameter_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def [](key_name)
super(exact || key_name)
else # Dealing with a multiple value pair or a single encoded value pair
string = pairs.sort { |a,b| a.first <=> b.first }.map { |v| v.last }.join('')
if mt = string.match(/([\w\d\-]+)'(\w\w)'(.*)/)
if mt = string.match(/([\w\-]+)'(\w\w)'(.*)/)
string = mt[3]
encoding = mt[1]
else
Expand Down
10 changes: 5 additions & 5 deletions lib/mail/fields/content_type_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ def sanatize( val )
end

case
when val.chomp =~ /^\s*([\w\d\-_]+)\/([\w\d\-_]+)\s*;;+(.*)$/i
when val.chomp =~ /^\s*([\w\-_]+)\/([\w\-_]+)\s*;;+(.*)$/i
# Handles 'text/plain;; format="flowed"' (double semi colon)
"#{$1}/#{$2}; #{$3}"
when val.chomp =~ /^\s*([\w\d\-_]+)\/([\w\d\-_]+)\s*;\s?(ISO[\w\d\-_]+)$/i
when val.chomp =~ /^\s*([\w\-_]+)\/([\w\-_]+)\s*;\s?(ISO[\w\-_]+)$/i
# Microsoft helper:
# Handles 'type/subtype;ISO-8559-1'
"#{$1}/#{$2}; charset=#{quote_atom($3)}"
Expand All @@ -166,10 +166,10 @@ def sanatize( val )
when val.chomp =~ /^(\w+);\s(.*)$/i
# Handles 'text; <parameters>'
"text/plain; #{$2}"
when val =~ /([\w\d\-_]+\/[\w\d\-_]+);\scharset="charset="(\w+)""/i
when val =~ /([\w\-_]+\/[\w\-_]+);\scharset="charset="(\w+)""/i
# Handles text/html; charset="charset="GB2312""
"#{$1}; charset=#{quote_atom($2)}"
when val =~ /([\w\d\-_]+\/[\w\d\-_]+);\s+(.*)/i
when val =~ /([\w\-_]+\/[\w\-_]+);\s+(.*)/i
type = $1
# Handles misquoted param values
# e.g: application/octet-stream; name=archiveshelp1[1].htm
Expand All @@ -188,7 +188,7 @@ def sanatize( val )

def get_mime_type( val )
case
when val =~ /^([\w\d\-_]+)\/([\w\d\-_]+);.+$/i
when val =~ /^([\w\-_]+)\/([\w\-_]+);.+$/i
"#{$1}/#{$2}"
else
'text/plain'
Expand Down
4 changes: 2 additions & 2 deletions spec/mail/fields/content_type_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@
describe "class methods" do
it "should give back an initialized instance with a unique boundary" do
boundary = Mail::ContentTypeField.with_boundary('multipart/mixed')
boundary.encoded.should =~ %r{Content-Type: multipart/mixed;\r\n\sboundary="--==_mimepart_[\w\d]+_[\w\d]+"\r\n}
boundary.encoded.should =~ %r{Content-Type: multipart/mixed;\r\n\sboundary="--==_mimepart_[\w]+_[\w]+"\r\n}
end

it "should give back an initialized instance with different type with a unique boundary" do
boundary = Mail::ContentTypeField.with_boundary('multipart/alternative')
boundary.encoded.should =~ %r{Content-Type: multipart/alternative;\r\n\sboundary="--==_mimepart_[\w\d]+_[\w\d]+"\r\n}
boundary.encoded.should =~ %r{Content-Type: multipart/alternative;\r\n\sboundary="--==_mimepart_[\w]+_[\w]+"\r\n}
end

it "should give unique boundaries" do
Expand Down

0 comments on commit 09e02bb

Please sign in to comment.