aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVincent Richard <[email protected]>2008-04-28 19:49:48 +0000
committerVincent Richard <[email protected]>2008-04-28 19:49:48 +0000
commit439b2b3e90cb78a81e8d42ffdcd8543c64b1d1de (patch)
treea51cb6e1f77bb6f2f6a7c1d4c14df290bf7370b7 /src
parentRemoved old GNU TLS error. (diff)
downloadvmime-439b2b3e90cb78a81e8d42ffdcd8543c64b1d1de.tar.gz
vmime-439b2b3e90cb78a81e8d42ffdcd8543c64b1d1de.zip
Fixed extra space in subject (see https://sourceforge.net/forum/message.php?msg_id=4894970).
Diffstat (limited to 'src')
-rw-r--r--src/text.cpp101
-rw-r--r--src/word.cpp42
2 files changed, 98 insertions, 45 deletions
diff --git a/src/text.cpp b/src/text.cpp
index 67aed20f..13a682ef 100644
--- a/src/text.cpp
+++ b/src/text.cpp
@@ -269,63 +269,78 @@ void text::createFromString(const string& in, const charset& ch)
removeAllWords();
- for (string::size_type end = in.size(), pos = 0, start = 0 ; ; )
+ const string::size_type asciiCount =
+ utility::stringUtils::countASCIIchars(in.begin(), in.end());
+
+ const string::size_type 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
+ {
+ appendWord(vmime::create <word>(in, ch));
+ }
+ // Else, only encode words which need it
+ else
{
- if (pos == end || parserHelpers::isSpace(in[pos]))
+ for (string::size_type end = in.size(), pos = 0, start = 0 ; ; )
{
- if (pos != end)
- ++pos;
+ if (pos == end || parserHelpers::isSpace(in[pos]))
+ {
+ const string chunk(in.begin() + start, in.begin() + pos);
- const string chunk(in.begin() + start, in.begin() + pos);
+ if (pos != end)
+ ++pos;
- if (is8bit)
- {
- if (count && prevIs8bit)
+ if (is8bit)
{
- // No need to create a new encoded word, just append
- // the current word to the previous one.
- ref <word> w = getWordAt(getWordCount() - 1);
- w->getBuffer() += chunk;
+ if (count && prevIs8bit)
+ {
+ // No need to create a new encoded word, just append
+ // the current word to the previous one.
+ ref <word> w = getWordAt(getWordCount() - 1);
+ w->getBuffer() += " " + chunk;
+ }
+ else
+ {
+ appendWord(vmime::create <word>(chunk, ch));
+
+ prevIs8bit = true;
+ ++count;
+ }
}
else
{
- appendWord(vmime::create <word>(chunk, ch));
+ if (count && !prevIs8bit)
+ {
+ ref <word> w = getWordAt(getWordCount() - 1);
+ w->getBuffer() += " " + chunk;
+ }
+ else
+ {
+ appendWord(vmime::create <word>
+ (chunk, charset(charsets::US_ASCII)));
- prevIs8bit = true;
- ++count;
+ prevIs8bit = false;
+ ++count;
+ }
}
+
+ if (pos == end)
+ break;
+
+ is8bit = false;
+ start = pos;
+ }
+ else if (!parserHelpers::isAscii(in[pos]))
+ {
+ is8bit = true;
+ ++pos;
}
else
{
- if (count && !prevIs8bit)
- {
- ref <word> w = getWordAt(getWordCount() - 1);
- w->getBuffer() += chunk;
- }
- else
- {
- appendWord(vmime::create <word>
- (chunk, charset(charsets::US_ASCII)));
-
- prevIs8bit = false;
- ++count;
- }
+ ++pos;
}
-
- if (pos == end)
- break;
-
- is8bit = false;
- start = pos;
- }
- else if (!parserHelpers::isAscii(in[pos]))
- {
- is8bit = true;
- ++pos;
- }
- else
- {
- ++pos;
}
}
}
diff --git a/src/word.cpp b/src/word.cpp
index 98ad208a..3a0605ff 100644
--- a/src/word.cpp
+++ b/src/word.cpp
@@ -73,8 +73,13 @@ ref <word> word::parseNext(const string& buffer, const string::size_type positio
// - before the first word
// - between two encoded words
// - after the last word
+ string whiteSpaces;
+
while (pos < end && parserHelpers::isSpace(buffer[pos]))
+ {
+ whiteSpaces += buffer[pos];
++pos;
+ }
string::size_type startPos = pos;
string unencoded;
@@ -88,7 +93,10 @@ ref <word> word::parseNext(const string& buffer, const string::size_type positio
string::size_type endPos = pos;
if (pos > position && buffer[pos - 1] == '\r')
+ {
+ ++pos;
--endPos;
+ }
while (pos != end && parserHelpers::isSpace(buffer[pos]))
++pos;
@@ -97,6 +105,7 @@ ref <word> word::parseNext(const string& buffer, const string::size_type positio
unencoded += ' ';
startPos = pos;
+ continue;
}
// Start of an encoded word
else if (pos + 8 < end && // 8 = "=?(.+)?(.+)?(.*)?="
@@ -107,6 +116,9 @@ ref <word> word::parseNext(const string& buffer, const string::size_type positio
if (!unencoded.empty())
{
+ if (prevIsEncoded)
+ unencoded = whiteSpaces + unencoded;
+
ref <word> w = vmime::create <word>(unencoded, charset(charsets::US_ASCII));
w->setParsedBounds(position, pos);
@@ -183,7 +195,7 @@ ref <word> word::parseNext(const string& buffer, const string::size_type positio
if (end != startPos)
{
if (startPos != pos && !isFirst && prevIsEncoded)
- unencoded += ' ';
+ unencoded += whiteSpaces;
unencoded += buffer.substr(startPos, end - startPos);
@@ -388,11 +400,15 @@ void word::generate(utility::outputStream& os, const string::size_type maxLineLe
{
os << CRLF;
curLineLength = 0;
+
+ state->lastCharIsSpace = true;
}
else
{
os << NEW_LINE_SEQUENCE;
curLineLength = NEW_LINE_SEQUENCE_LENGTH;
+
+ state->lastCharIsSpace = true;
}
p = curLineStart;
@@ -401,8 +417,16 @@ void word::generate(utility::outputStream& os, const string::size_type maxLineLe
}
else
{
+ if (!state->isFirstWord && state->prevWordIsEncoded && !state->lastCharIsSpace && !parserHelpers::isSpace(*curLineStart))
+ os << " "; // Separate from previous word
+
os << string(curLineStart, p);
+ if (parserHelpers::isSpace(*(p - 1)))
+ state->lastCharIsSpace = true;
+ else
+ state->lastCharIsSpace = false;
+
if (p == end)
{
finished = true;
@@ -439,15 +463,24 @@ void word::generate(utility::outputStream& os, const string::size_type maxLineLe
os << string(curLineStart, lastWSpos);
+ if (lastWSpos > curLineStart && std::isspace(*(lastWSpos - 1)))
+ state->lastCharIsSpace = true;
+ else
+ state->lastCharIsSpace = false;
+
if (flags & text::NO_NEW_LINE_SEQUENCE)
{
os << CRLF;
curLineLength = 0;
+
+ state->lastCharIsSpace = true;
}
else
{
os << NEW_LINE_SEQUENCE;
curLineLength = NEW_LINE_SEQUENCE_LENGTH;
+
+ state->lastCharIsSpace = true;
}
curLineStart = lastWSpos + 1;
@@ -523,13 +556,17 @@ void word::generate(utility::outputStream& os, const string::size_type maxLineLe
{
os << NEW_LINE_SEQUENCE;
curLineLength = NEW_LINE_SEQUENCE_LENGTH;
+
+ state->lastCharIsSpace = true;
}
// Encode and fold input buffer
- if (!startNewLine && !state->isFirstWord && state->prevWordIsEncoded)
+ if (!startNewLine && !state->isFirstWord && !state->lastCharIsSpace)
{
os << " "; // Separate from previous word
++curLineLength;
+
+ state->lastCharIsSpace = true;
}
for (unsigned int i = 0 ; ; ++i)
@@ -561,6 +598,7 @@ void word::generate(utility::outputStream& os, const string::size_type maxLineLe
os << wordEnd;
state->prevWordIsEncoded = true;
+ state->lastCharIsSpace = false;
}
}