Take account of charset recommended encoding (thanks to John van der Kamp, Zarafa).

This commit is contained in:
Vincent Richard 2010-07-17 07:00:49 +00:00
parent 1930316f36
commit f63c64c0e1

View File

@ -24,6 +24,7 @@
#include "vmime/text.hpp" #include "vmime/text.hpp"
#include "vmime/parserHelpers.hpp" #include "vmime/parserHelpers.hpp"
#include "vmime/encoding.hpp"
namespace vmime 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) void text::createFromString(const string& in, const charset& ch)
{ {
bool is8bit = false; // is the current word 8-bit? string::size_type asciiCount = 0;
bool prevIs8bit = false; // is previous word 8-bit? string::size_type asciiPercent = 0;
unsigned int count = 0; // total number of words
removeAllWords(); removeAllWords();
const string::size_type asciiCount = // Check whether there is a recommended encoding for this charset.
utility::stringUtils::countASCIIchars(in.begin(), in.end()); // 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 = if (!alwaysEncode)
(in.length() == 0 ? 100 : (100 * asciiCount) / in.length()); {
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 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)); appendWord(vmime::create <word>(in, ch));
} }
// Else, only encode words which need it // Else, only encode words which need it
else 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 ; ; ) for (string::size_type end = in.size(), pos = 0, start = 0 ; ; )
{ {
if (pos == end || parserHelpers::isSpace(in[pos])) if (pos == end || parserHelpers::isSpace(in[pos]))