DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
InternalStringConvert.cpp
Go to the documentation of this file.
1#include "pch.h"
3
4#if defined(_WIN32)
5#include <windows.h>
6#endif
7
8namespace DotNetDupe {
9 namespace System {
10 namespace Internal {
11
12 std::string StringConvertInternal::WCharToUtf8(const wchar_t* pWStr) {
14 if (!pWStr || pWStr[0] == 0) return std::string("");
15
17#if defined(_WIN32)
18 int iSizeNeeded = ::WideCharToMultiByte(CP_UTF8, 0, pWStr, -1, NULL, 0, NULL, NULL);
19 if (iSizeNeeded <= 0) return std::string("");
20 std::string sResult(iSizeNeeded, 0);
21 ::WideCharToMultiByte(CP_UTF8, 0, pWStr, -1, &sResult[0], iSizeNeeded, NULL, NULL);
22 if (!sResult.empty() && sResult.back() == '\0') sResult.pop_back();
23 return sResult;
24#else
25 std::string sResult;
26 for (; *pWStr; ++pWStr) {
27 sResult.push_back(static_cast<char>(*pWStr));
28 }
29 return sResult;
30#endif
31 }
32
33 std::wstring StringConvertInternal::Utf8ToWChar(const char* pUtf8Str) {
35 if (!pUtf8Str || pUtf8Str[0] == 0) return std::wstring(L"");
36
38#if defined(_WIN32)
39 int iSizeNeeded = ::MultiByteToWideChar(CP_UTF8, 0, pUtf8Str, -1, NULL, 0);
40 if (iSizeNeeded <= 0) return std::wstring(L"");
41 std::wstring sResult(iSizeNeeded, 0);
42 ::MultiByteToWideChar(CP_UTF8, 0, pUtf8Str, -1, &sResult[0], iSizeNeeded);
43 if (!sResult.empty() && sResult.back() == L'\0') sResult.pop_back();
44 return sResult;
45#else
46 std::wstring sResult;
47 for (; *pUtf8Str; ++pUtf8Str) {
48 sResult.push_back(static_cast<wchar_t>(*pUtf8Str));
49 }
50 return sResult;
51#endif
52 }
53
54 }
55 }
56}
static std::wstring Utf8ToWChar(const char *pUtf8Str)
static std::string WCharToUtf8(const wchar_t *pWStr)