diff --git a/src/mailbox.cpp b/src/mailbox.cpp index 2f625216..f6df5165 100644 --- a/src/mailbox.cpp +++ b/src/mailbox.cpp @@ -415,7 +415,7 @@ void mailbox::generate(utility::outputStream& os, const string::size_type maxLin bool newLine = true; m_name.encodeAndFold(os, maxLineLength, pos, &pos, - forceEncode ? text::FORCE_ENCODING : 0); + text::QUOTE_IF_POSSIBLE | (forceEncode ? text::FORCE_ENCODING : 0)); if (pos + m_email.length() + 3 > maxLineLength) { diff --git a/src/word.cpp b/src/word.cpp index ce5ddfcc..ea8244a5 100644 --- a/src/word.cpp +++ b/src/word.cpp @@ -352,10 +352,22 @@ void word::generate(utility::outputStream& os, const string::size_type maxLineLe noEncoding = false; } - if (noEncoding) + // If possible and requested (with flag), quote the buffer (no folding is performed). + // Quoting is possible if and only if: + // - the whole buffer is ASCII-only + // - the buffer does not contain quoting character (") + // - there is enough remaining space on the current line to hold the whole buffer + if ((flags & text::QUOTE_IF_POSSIBLE) && + asciiCount == m_buffer.length() && + m_buffer.find('"') == string::npos && + (curLineLength + 2 /* 2 x " */ + m_buffer.length()) < maxLineLength) + { + os << '"' << m_buffer << '"'; + curLineLength += 2 + m_buffer.length(); + } + // We will fold lines without encoding them. + else if (noEncoding) { - // We will fold lines without encoding them. - string::const_iterator lastWSpos = m_buffer.end(); // last white-space position string::const_iterator curLineStart = m_buffer.begin(); // current line start diff --git a/vmime/text.hpp b/vmime/text.hpp index dd4534d8..9760c48a 100644 --- a/vmime/text.hpp +++ b/vmime/text.hpp @@ -197,7 +197,8 @@ public: FORCE_NO_ENCODING = (1 << 0), /**< Just fold lines, don't encode them. */ FORCE_ENCODING = (1 << 1), /**< Encode lines even if they are plain ASCII text. */ - NO_NEW_LINE_SEQUENCE = (1 << 2) /**< Use CRLF instead of new-line sequence (CRLF + TAB). */ + NO_NEW_LINE_SEQUENCE = (1 << 2), /**< Use CRLF instead of new-line sequence (CRLF + TAB). */ + QUOTE_IF_POSSIBLE = (1 << 3) /**< Use quoting instead of encoding when possible (even if FORCE_ENCODING is specified). */ }; /** Encode and fold text in respect to RFC-2047.