diff --git a/src/text.cpp b/src/text.cpp index 89a541a0..a2fe0601 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -24,6 +24,7 @@ #include "vmime/text.hpp" #include "vmime/parserHelpers.hpp" +#include "vmime/encoding.hpp" namespace vmime @@ -248,26 +249,36 @@ ref text::newFromString(const string& in, const charset& ch) void text::createFromString(const string& in, const charset& ch) { - bool is8bit = false; // is the current word 8-bit? - bool prevIs8bit = false; // is previous word 8-bit? - unsigned int count = 0; // total number of words + string::size_type asciiCount = 0; + string::size_type asciiPercent = 0; removeAllWords(); - const string::size_type asciiCount = - utility::stringUtils::countASCIIchars(in.begin(), in.end()); + // Check whether there is a recommended encoding for this charset. + // If so, the whole buffer will be encoded. Else, the number of + // 7-bit (ASCII) bytes in the input will be used to determine if + // we need to encode the whole buffer. + encoding recommendedEnc; + const bool alwaysEncode = ch.getRecommendedEncoding(recommendedEnc); - const string::size_type asciiPercent = - (in.length() == 0 ? 100 : (100 * asciiCount) / in.length()); + if (!alwaysEncode) + { + asciiCount = utility::stringUtils::countASCIIchars(in.begin(), in.end()); + asciiPercent = (in.length() == 0 ? 100 : (100 * asciiCount) / in.length()); + } // If there are "too much" non-ASCII chars, encode everything - if (asciiPercent < 60) // less than 60% ASCII chars + if (alwaysEncode || asciiPercent < 60) // less than 60% ASCII chars { appendWord(vmime::create (in, ch)); } // Else, only encode words which need it else { + bool is8bit = false; // is the current word 8-bit? + bool prevIs8bit = false; // is previous word 8-bit? + unsigned int count = 0; // total number of words + for (string::size_type end = in.size(), pos = 0, start = 0 ; ; ) { if (pos == end || parserHelpers::isSpace(in[pos]))