From d1917ed9a672894f8dbd26ab61151792542eb73e Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Mon, 21 Jan 2019 14:22:47 -0500 Subject: [PATCH] replace \ properly --- extensions/bingtranslate.cpp | 12 +++++++++++- extensions/googletranslate.cpp | 11 ++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/extensions/bingtranslate.cpp b/extensions/bingtranslate.cpp index 7b411b4..ed3318a 100644 --- a/extensions/bingtranslate.cpp +++ b/extensions/bingtranslate.cpp @@ -148,7 +148,17 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) std::wstring translation, translateFrom; Translate(sentence, translateFrom, translateTo); translation = Translate(sentence, translateFrom, translateTo); - for (auto& c : translation) if (c == L'\\') c = 0x200b; + + for (int i = 0; i < translation.size(); ++i) + { + if (translation[i] == L'\\') + { + translation[i] = 0x200b; + if (translation[i + 1] == L'r') translation[i + 1] = L'\r'; + if (translation[i + 1] == L'n') translation[i + 1] = L'\n'; + } + } + if (translation.empty()) translation = TRANSLATION_ERROR; sentence += L"\n" + translation; return true; diff --git a/extensions/googletranslate.cpp b/extensions/googletranslate.cpp index 778d323..0fc1030 100644 --- a/extensions/googletranslate.cpp +++ b/extensions/googletranslate.cpp @@ -194,7 +194,16 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) std::wstring response(wbuffer); for (std::wsmatch results; std::regex_search(response, results, std::wregex(L"\\[\"(.*?)\",[n\"]")); response = results.suffix()) translation += std::wstring(results[1]) + L" "; - for (auto& c : translation) if (c == L'\\') c = 0x200b; + + for (int i = 0; i < translation.size(); ++i) + { + if (translation[i] == L'\\') + { + translation[i] = 0x200b; + if (translation[i + 1] == L'r') translation[i + 1] = L'\r'; + if (translation[i + 1] == L'n') translation[i + 1] = L'\n'; + } + } } else {