From 097bde861d8a217a5e5ab46253d242d3a52a8436 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Tue, 12 Oct 2010 20:01:34 +0000 Subject: [PATCH] Fixed missing whitespace in text parsing. --- src/text.cpp | 12 +++++++++ tests/parser/textTest.cpp | 54 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/text.cpp b/src/text.cpp index a2fe0601..24544567 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -299,6 +299,12 @@ void text::createFromString(const string& in, const charset& ch) } else { + if (count) + { + ref w = getWordAt(getWordCount() - 1); + w->getBuffer() += ' '; + } + appendWord(vmime::create (chunk, ch)); prevIs8bit = true; @@ -314,6 +320,12 @@ void text::createFromString(const string& in, const charset& ch) } else { + if (count) + { + ref w = getWordAt(getWordCount() - 1); + w->getBuffer() += ' '; + } + appendWord(vmime::create (chunk, charset(charsets::US_ASCII))); diff --git a/tests/parser/textTest.cpp b/tests/parser/textTest.cpp index b455d919..c60da5aa 100644 --- a/tests/parser/textTest.cpp +++ b/tests/parser/textTest.cpp @@ -47,6 +47,9 @@ VMIME_TEST_SUITE_BEGIN VMIME_TEST(testWordGenerateQuote) VMIME_TEST(testWordGenerateSpecialCharsets) VMIME_TEST(testWordGenerateSpecials) + + VMIME_TEST(testWhitespace) + VMIME_TEST(testWhitespaceMBox) VMIME_TEST_LIST_END @@ -141,9 +144,9 @@ VMIME_TEST_SUITE_BEGIN t2.createFromString(s2, c2); VASSERT_EQ("2.1", 3, t2.getWordCount()); - VASSERT_EQ("2.2", "some ASCII characters and special chars:", t2.getWordAt(0)->getBuffer()); + VASSERT_EQ("2.2", "some ASCII characters and special chars: ", t2.getWordAt(0)->getBuffer()); VASSERT_EQ("2.3", vmime::charset(vmime::charsets::US_ASCII), t2.getWordAt(0)->getCharset()); - VASSERT_EQ("2.4", "\xf1\xf2\xf3\xf4", t2.getWordAt(1)->getBuffer()); + VASSERT_EQ("2.4", "\xf1\xf2\xf3\xf4 ", t2.getWordAt(1)->getBuffer()); VASSERT_EQ("2.5", c2, t2.getWordAt(1)->getCharset()); VASSERT_EQ("2.6", "and then more ASCII chars.", t2.getWordAt(2)->getBuffer()); VASSERT_EQ("2.7", vmime::charset(vmime::charsets::US_ASCII), t2.getWordAt(2)->getCharset()); @@ -378,5 +381,52 @@ VMIME_TEST_SUITE_BEGIN vmime::word("\x22\xC3\x9Cml\xC3\xA4ute\x22", vmime::charset("UTF-8")).generate()); } + void testWhitespace() + { + // Create + vmime::text text; + text.createFromString("Achim Br\xc3\xa4ndt", vmime::charsets::UTF_8); + + VASSERT_EQ("1", 2, text.getWordCount()); + VASSERT_EQ("2", "Achim ", text.getWordAt(0)->getBuffer()); + VASSERT_EQ("3", "us-ascii", text.getWordAt(0)->getCharset()); + VASSERT_EQ("4", "Br\xc3\xa4ndt", text.getWordAt(1)->getBuffer()); + VASSERT_EQ("5", "utf-8", text.getWordAt(1)->getCharset()); + + // Generate + VASSERT_EQ("6", "Achim =?utf-8?Q?Br=C3=A4ndt?=", text.generate()); + + // Parse + text.parse("=?us-ascii?Q?Achim_?= =?utf-8?Q?Br=C3=A4ndt?="); + + VASSERT_EQ("7", 2, text.getWordCount()); + VASSERT_EQ("8", "Achim ", text.getWordAt(0)->getBuffer()); + VASSERT_EQ("9", "us-ascii", text.getWordAt(0)->getCharset()); + VASSERT_EQ("10", "Br\xc3\xa4ndt", text.getWordAt(1)->getBuffer()); + VASSERT_EQ("11", "utf-8", text.getWordAt(1)->getCharset()); + } + + void testWhitespaceMBox() + { + // Space MUST be encoded inside a word + vmime::mailbox mbox(vmime::text("Achim Br\xc3\xa4ndt", vmime::charsets::UTF_8), "me@vmime.org"); + VASSERT_EQ("generate1", "=?us-ascii?Q?Achim_?= =?utf-8?Q?Br=C3=A4ndt?= ", mbox.generate()); + + vmime::text txt; + txt.appendWord(vmime::create ("Achim ", "us-ascii")); + txt.appendWord(vmime::create ("Br\xc3\xa4ndt", "utf-8")); + mbox = vmime::mailbox(txt, "me@vmime.org"); + VASSERT_EQ("generate2", "=?us-ascii?Q?Achim_?= =?utf-8?Q?Br=C3=A4ndt?= ", mbox.generate()); + + mbox.parse("=?us-ascii?Q?Achim?= =?utf-8?Q?Br=C3=A4ndt?= "); + VASSERT_EQ("parse.name.count", 2, mbox.getName().getWordCount()); + VASSERT_EQ("parse.name.word1.buffer", "Achim", mbox.getName().getWordAt(0)->getBuffer()); + VASSERT_EQ("parse.name.word1.charset", "us-ascii", mbox.getName().getWordAt(0)->getCharset()); + VASSERT_EQ("parse.name.word2.buffer", "Br\xc3\xa4ndt", mbox.getName().getWordAt(1)->getBuffer()); + VASSERT_EQ("parse.name.word2.charset", "utf-8", mbox.getName().getWordAt(1)->getCharset()); + + VASSERT_EQ("parse.email", "me@vmime.org", mbox.getEmail()); + } + VMIME_TEST_SUITE_END