Skip to content

Commit

Permalink
Update encodings code
Browse files Browse the repository at this point in the history
  • Loading branch information
pzb committed Mar 11, 2010
1 parent b28640d commit 0adeb30
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
8 changes: 4 additions & 4 deletions lib/mail/encodings/7bit.rb
Expand Up @@ -6,21 +6,21 @@ class SevenBit < EightBit

PRIORITY = 1

# 7bit is an identiy encoding, meaning nothing to do
# 7bit and 8bit operate the same

# Decode the string
def self.decode(str)
str.to_crlf
super
end

# Encode the string
def self.encode(str)
str.to_crlf
super
end

# Idenity encodings have a fixed cost, 1 byte out per 1 byte in
def self.cost(str)
1.0
super
end

Encodings.register(NAME, self)
Expand Down
2 changes: 1 addition & 1 deletion lib/mail/encodings/8bit.rb
Expand Up @@ -10,7 +10,7 @@ class EightBit < Binary

# Decode the string
def self.decode(str)
str.to_crlf
str.to_lf
end

# Encode the string
Expand Down
5 changes: 1 addition & 4 deletions lib/mail/encodings/encodings.rb
Expand Up @@ -41,13 +41,10 @@ def Encodings.get_encoding( str )
end

def Encodings.get_all
@transfer_encodings.keys
@transfer_encodings.values
end

def Encodings.get_name(enc)
if enc.is_a? Class then
enc = enc::Name
end
enc = enc.to_s.gsub("-", "_").downcase
end

Expand Down
31 changes: 31 additions & 0 deletions lib/mail/encodings/transfer_encoding.rb
Expand Up @@ -22,6 +22,37 @@ def self.can_encode?(enc)
def self.cost(str)
raise "Unimplemented"
end

def self.to_s
self::NAME
end

def self.get_best_compatible(source_encoding, str)
if self.can_transport? source_encoding then
source_encoding
else
choices = []
Encodings.get_all.each do |enc|
choices << enc if self.can_transport? enc and enc.can_encode? source_encoding
end
best = nil
best_cost = 100
choices.each do |enc|
this_cost = enc.cost str
if this_cost < best_cost then
best_cost = this_cost
best = enc
elsif this_cost == best_cost then
best = enc if enc::PRIORITY < best::PRIORITY
end
end
best
end
end

def to_s
self.class.to_s
end
end
end
end

0 comments on commit 0adeb30

Please sign in to comment.