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/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]))