diff options
author | Vincent Richard <[email protected]> | 2010-07-17 07:00:49 +0000 |
---|---|---|
committer | Vincent Richard <[email protected]> | 2010-07-17 07:00:49 +0000 |
commit | f63c64c0e19c8088804a4b894a39163b94302e9d (patch) | |
tree | 58c1280242512f464fa8270c0664dd114ec8c020 | |
parent | Do not generate 7-bit value for parameter if RFC-2231 extended value is gener... (diff) | |
download | vmime-f63c64c0e19c8088804a4b894a39163b94302e9d.tar.gz vmime-f63c64c0e19c8088804a4b894a39163b94302e9d.zip |
Take account of charset recommended encoding (thanks to John van der Kamp, Zarafa).
-rw-r--r-- | src/text.cpp | 27 |
1 files changed, 19 insertions, 8 deletions
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> 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 <word>(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])) |