DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
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
13
namespace
DotNetDupe
{
14
namespace
System
{
15
class
String
;
16
17
namespace
Utils
{
18
21
class
StringConvert
{
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
}
Common.h
Defines common cross-platform macros, export decorators, and fundamental types.
DotNetDupe::System::String
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition
String.h:74
DotNetDupe::System::Utils::StringConvert
Cross-platform string encoding conversion utility between UTF-16 (wchar_t) and UTF-8 (char).
Definition
StringConvert.h:21
DotNetDupe::System::Utils::StringConvert::WCharToUtf8
static std::string WCharToUtf8(const wchar_t *pWStr)
Converts a null-terminated UTF-16 wchar_t string into a UTF-8 std::string.
Definition
StringConvert.h:26
DotNetDupe::System::Utils::StringConvert::Utf8ToWChar
static std::wstring Utf8ToWChar(const char *pUtf8Str)
Converts a null-terminated UTF-8 char string into a UTF-16 std::wstring.
Definition
StringConvert.h:47
DotNetDupe::System::Utils
Definition
StringConvert.h:17
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
Include
System
Utils
StringConvert.h
Generated by
1.18.0