DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
StringConvert.h
Go to the documentation of this file.
1
3
4#pragma once
5
6#include "Common.h"
7#include <string>
8
9#if defined(_WIN32)
10#include <windows.h>
11#endif
12
13namespace DotNetDupe {
14 namespace System {
15 class String;
16
17 namespace Utils {
18
22 public:
26 static inline std::string WCharToUtf8(const wchar_t* pWStr) {
27 if (!pWStr || pWStr[0] == 0) return std::string("");
28#if defined(_WIN32)
29 int iSizeNeeded = ::WideCharToMultiByte(CP_UTF8, 0, pWStr, -1, NULL, 0, NULL, NULL);
30 if (iSizeNeeded <= 0) return std::string("");
31 std::string sResult(iSizeNeeded, 0);
32 ::WideCharToMultiByte(CP_UTF8, 0, pWStr, -1, &sResult[0], iSizeNeeded, NULL, NULL);
33 if (!sResult.empty() && sResult.back() == '\0') sResult.pop_back();
34 return sResult;
35#else
36 std::string sResult;
37 for (; *pWStr; ++pWStr) {
38 sResult.push_back(static_cast<char>(*pWStr));
39 }
40 return sResult;
41#endif
42 }
43
47 static inline std::wstring Utf8ToWChar(const char* pUtf8Str) {
48 if (!pUtf8Str || pUtf8Str[0] == 0) return std::wstring(L"");
49#if defined(_WIN32)
50 int iSizeNeeded = ::MultiByteToWideChar(CP_UTF8, 0, pUtf8Str, -1, NULL, 0);
51 if (iSizeNeeded <= 0) return std::wstring(L"");
52 std::wstring sResult(iSizeNeeded, 0);
53 ::MultiByteToWideChar(CP_UTF8, 0, pUtf8Str, -1, &sResult[0], iSizeNeeded);
54 if (!sResult.empty() && sResult.back() == L'\0') sResult.pop_back();
55 return sResult;
56#else
57 std::wstring sResult;
58 for (; *pUtf8Str; ++pUtf8Str) {
59 sResult.push_back(static_cast<wchar_t>(*pUtf8Str));
60 }
61 return sResult;
62#endif
63 }
64 };
65
66 }
67 }
68}
Defines common cross-platform macros, export decorators, and fundamental types.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
Cross-platform string encoding conversion utility between UTF-16 (wchar_t) and UTF-8 (char).
static std::string WCharToUtf8(const wchar_t *pWStr)
Converts a null-terminated UTF-16 wchar_t string into a UTF-8 std::string.
static std::wstring Utf8ToWChar(const char *pUtf8Str)
Converts a null-terminated UTF-8 char string into a UTF-16 std::wstring.