From 8be6485685fea54f2a0a4ea55d2ef38d4518c6a2 Mon Sep 17 00:00:00 2001 From: Daniel Wolf Date: Thu, 2 Jun 2016 22:21:37 +0200 Subject: [PATCH] Implemented string conversion from Latin-1 to Unicode --- src/stringTools.cpp | 9 +++++++++ src/stringTools.h | 2 ++ tests/stringToolsTests.cpp | 9 +++++++++ 3 files changed, 20 insertions(+) diff --git a/src/stringTools.cpp b/src/stringTools.cpp index 9da85c1..c64d63c 100644 --- a/src/stringTools.cpp +++ b/src/stringTools.cpp @@ -2,6 +2,7 @@ #include using std::string; +using std::wstring; using std::u32string; using std::vector; using boost::optional; @@ -81,6 +82,14 @@ vector wrapString(const string& s, int lineLength, int hangingIndent) { return lines; } +wstring latin1ToWide(const string& s) { + wstring result; + for (unsigned char c : s) { + result.append(1, c); + } + return result; +} + optional toASCII(char32_t ch) { switch (ch) { #include "asciiCases.cpp" diff --git a/src/stringTools.h b/src/stringTools.h index 9257e68..4ba7aab 100644 --- a/src/stringTools.h +++ b/src/stringTools.h @@ -10,6 +10,8 @@ std::vector wrapSingleLineString(const std::string& s, int lineLeng std::vector wrapString(const std::string& s, int lineLength, int hangingIndent = 0); +std::wstring latin1ToWide(const std::string& s); + boost::optional toASCII(char32_t ch); std::string toASCII(const std::u32string& s); \ No newline at end of file diff --git a/tests/stringToolsTests.cpp b/tests/stringToolsTests.cpp index f135c9a..f5ab84b 100644 --- a/tests/stringToolsTests.cpp +++ b/tests/stringToolsTests.cpp @@ -3,6 +3,7 @@ using namespace testing; using std::string; +using std::wstring; // splitIntoLines @@ -72,6 +73,14 @@ TEST(wrapString, basic) { EXPECT_THAT(wrapString("\n\nLine no 3\n\nLine no 4\n", 8), ElementsAre("", "", "Line no", "3", "", "Line no", "4", "")); } +// latin1ToWide + +TEST(latin1ToWide, basic) { + string pangramLatin1 = "D\350s No\353l o\371 un z\351phyr ha\357 me v\352t de gla\347ons w\374rmiens, je d\356ne d'exquis r\364tis de boeuf au kir \340 l'a\377 d'\342ge m\373r & c\346tera!"; + wstring pangramWide = L"Dès Noël où un zéphyr haï me vêt de glaçons würmiens, je dîne d'exquis rôtis de boeuf au kir à l'aÿ d'âge mûr & cætera!"; + EXPECT_EQ(pangramWide, latin1ToWide(pangramLatin1)); +} + // toASCII TEST(toASCII, string) {