Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Field cleanup and performance improvements #488

Merged
merged 4 commits into from

2 participants

@bpot

No description provided.

@jeremy jeremy commented on the diff
lib/mail/field.rb
@@ -195,16 +199,11 @@ def create_field(name, value, charset)
def new_field(name, value, charset)
lower_case_name = name.to_s.downcase
- header_name = nil
- FIELDS_MAP.each do |field_name, _|
- header_name = field_name if lower_case_name == field_name
- end
- if header_name
- FIELDS_MAP[header_name].new(value, charset)
+ if field_klass = FIELDS_MAP[lower_case_name]
@jeremy Collaborator
jeremy added a note

Looks like this should be using FIELD_ORDER_LOOKUP - ?

@jeremy Collaborator
jeremy added a note

Nevermind, wrong mapping!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jeremy
Collaborator

These all look good. Only change is if anyone tweaks FIELD_ORDER, existing memoized field order won't change. Seems reasonable & non-breaking.

@jeremy jeremy merged commit 2433698 into from
@jeremy jeremy referenced this pull request from a commit
@jeremy jeremy Update CHANGELOG for #488 4836faf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 12 deletions.
  1. +10 −11 lib/mail/field.rb
  2. +1 −0  lib/mail/patterns.rb
  3. +1 −1  lib/mail/utilities.rb
View
21 lib/mail/field.rb
@@ -156,9 +156,11 @@ def same( other )
alias_method :==, :same
def <=>( other )
- self_order = FIELD_ORDER.rindex(self.name.to_s.downcase) || 100
- other_order = FIELD_ORDER.rindex(other.name.to_s.downcase) || 100
- self_order <=> other_order
+ self.field_order_id <=> other.field_order_id
+ end
+
+ def field_order_id
+ @field_order_id ||= (FIELD_ORDER_LOOKUP[self.name.to_s.downcase] || 100)
end
def method_missing(name, *args, &block)
@@ -174,10 +176,12 @@ def method_missing(name, *args, &block)
mime-version content-type content-transfer-encoding
content-location content-disposition content-description ]
+ FIELD_ORDER_LOOKUP = Hash[FIELD_ORDER.each_with_index.to_a]
+
private
def split(raw_field)
- match_data = raw_field.mb_chars.match(/^(#{FIELD_NAME})\s*:\s*(#{FIELD_BODY})?$/)
+ match_data = raw_field.mb_chars.match(FIELD_SPLIT)
[match_data[1].to_s.mb_chars.strip, match_data[2].to_s.mb_chars.strip]
rescue
STDERR.puts "WARNING: Could not parse (and so ignoring) '#{raw_field}'"
@@ -195,16 +199,11 @@ def create_field(name, value, charset)
def new_field(name, value, charset)
lower_case_name = name.to_s.downcase
- header_name = nil
- FIELDS_MAP.each do |field_name, _|
- header_name = field_name if lower_case_name == field_name
- end
- if header_name
- FIELDS_MAP[header_name].new(value, charset)
+ if field_klass = FIELDS_MAP[lower_case_name]
@jeremy Collaborator
jeremy added a note

Looks like this should be using FIELD_ORDER_LOOKUP - ?

@jeremy Collaborator
jeremy added a note

Nevermind, wrong mapping!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ field_klass.new(value, charset)
else
OptionalField.new(name, value, charset)
end
-
end
end
View
1  lib/mail/patterns.rb
@@ -22,6 +22,7 @@ module Patterns
FIELD_NAME = /[#{field_name}]+/
FIELD_BODY = /.+/
FIELD_LINE = /^[#{field_name}]+:\s*.+$/
+ FIELD_SPLIT = /^(#{FIELD_NAME})\s*:\s*(#{FIELD_BODY})?$/
HEADER_LINE = /^([#{field_name}]+:\s*.+)$/
QP_UNSAFE = /[^#{qp_safe}]/
View
2  lib/mail/utilities.rb
@@ -135,7 +135,7 @@ def uri_parser
# obj1 = :this_IS_an_object
# match_to_s( obj1, obj2 ) #=> true
def match_to_s( obj1, obj2 )
- obj1.to_s.downcase == obj2.to_s.downcase
+ obj1.to_s.casecmp(obj2.to_s) == 0
end
# Capitalizes a string that is joined by hyphens correctly.
Something went wrong with that request. Please try again.