Skip to content

Commit

Permalink
Merge pull request #974 from twalpole/quote_phrase
Browse files Browse the repository at this point in the history
Don't mutate string in quote_phrase
  • Loading branch information
jeremy committed Mar 8, 2016
2 parents 6343e70 + bd0fb6a commit df48a05
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
10 changes: 4 additions & 6 deletions lib/mail/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ def quote_atom( str )
def quote_phrase( str )
if RUBY_VERSION >= '1.9'
original_encoding = str.encoding
str.force_encoding('ASCII-8BIT')
if (PHRASE_UNSAFE === str)
quoted_str = dquote(str).force_encoding(original_encoding)
str.force_encoding(original_encoding)
quoted_str
ascii_str = str.dup.force_encoding('ASCII-8BIT')
if (PHRASE_UNSAFE === ascii_str)
dquote(ascii_str).force_encoding(original_encoding)
else
str.force_encoding(original_encoding)
str
end
else
(PHRASE_UNSAFE === str) ? dquote(str) : str
Expand Down
21 changes: 13 additions & 8 deletions spec/mail/utilities_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,32 @@

end

if RUBY_VERSION >= '1.9'
describe "quoting phrases" do
describe "quoting phrases" do
it "doesn't mutate original string" do
input_str = "blargh".freeze
expect { quote_phrase(input_str) }.not_to raise_error
end

if RUBY_VERSION >= '1.9'
describe "given a non-unsafe string" do
it "should not change the encoding" do
input_str = String.new("blargh")
input_str = "blargh"
input_str_encoding = input_str.encoding

quote_phrase(input_str)
result = quote_phrase(input_str)

expect(input_str.encoding).to eq input_str_encoding
expect(result.encoding).to eq input_str_encoding
end
end

describe "given an unsafe string" do
it "should not change the encoding" do
input_str = String.new("Bjørn")
input_str = "Bjørn"
input_str_encoding = input_str.encoding

quote_phrase(input_str)
result = quote_phrase(input_str)

expect(input_str.encoding).to eq input_str_encoding
expect(result.encoding).to eq input_str_encoding
end
end
end
Expand Down

0 comments on commit df48a05

Please sign in to comment.