Implemented string conversion from Latin-1 to Unicode

This commit is contained in:
Daniel Wolf 2016-06-02 22:21:37 +02:00
parent 4d45bf7c89
commit 8be6485685
3 changed files with 20 additions and 0 deletions

View File

@ -2,6 +2,7 @@
#include <boost/algorithm/string/trim.hpp> #include <boost/algorithm/string/trim.hpp>
using std::string; using std::string;
using std::wstring;
using std::u32string; using std::u32string;
using std::vector; using std::vector;
using boost::optional; using boost::optional;
@ -81,6 +82,14 @@ vector<string> wrapString(const string& s, int lineLength, int hangingIndent) {
return lines; return lines;
} }
wstring latin1ToWide(const string& s) {
wstring result;
for (unsigned char c : s) {
result.append(1, c);
}
return result;
}
optional<char> toASCII(char32_t ch) { optional<char> toASCII(char32_t ch) {
switch (ch) { switch (ch) {
#include "asciiCases.cpp" #include "asciiCases.cpp"

View File

@ -10,6 +10,8 @@ std::vector<std::string> wrapSingleLineString(const std::string& s, int lineLeng
std::vector<std::string> wrapString(const std::string& s, int lineLength, int hangingIndent = 0); std::vector<std::string> wrapString(const std::string& s, int lineLength, int hangingIndent = 0);
std::wstring latin1ToWide(const std::string& s);
boost::optional<char> toASCII(char32_t ch); boost::optional<char> toASCII(char32_t ch);
std::string toASCII(const std::u32string& s); std::string toASCII(const std::u32string& s);

View File

@ -3,6 +3,7 @@
using namespace testing; using namespace testing;
using std::string; using std::string;
using std::wstring;
// splitIntoLines // 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", "")); 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 // toASCII
TEST(toASCII, string) { TEST(toASCII, string) {