-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringutils.cpp
More file actions
74 lines (56 loc) · 1.92 KB
/
stringutils.cpp
File metadata and controls
74 lines (56 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "utils.h"
#include <iostream>
namespace StringUtils
{
std::wstring AnsiToW(LPCSTR s)
{
if (!s)
return std::wstring();
int needed = MultiByteToWideChar(CP_ACP, 0, s, -1, nullptr, 0);
if (needed <= 0)
return std::wstring();
std::vector<wchar_t> buf(needed);
MultiByteToWideChar(CP_ACP, 0, s, -1, buf.data(), needed);
return std::wstring(buf.data());
}
std::wstring AnsiToW(const std::string& s)
{
return AnsiToW(s.c_str());
}
std::string BytesToStringA(const BYTE* bytes, int byteCount)
{
if (byteCount == -1)
return std::string(reinterpret_cast<const char*>(bytes));
std::string str(reinterpret_cast<const char*>(bytes),
reinterpret_cast<const char*>(bytes + byteCount));
if (!str.empty() && str.back() == '\0')
str.pop_back();
return str;
}
std::wstring BytesToStringW(const BYTE* bytes, int byteCount)
{
if (byteCount == -1)
return std::wstring(reinterpret_cast<const wchar_t*>(bytes));
std::wstring str(reinterpret_cast<const wchar_t*>(bytes),
reinterpret_cast<const wchar_t*>(bytes + byteCount));
if (!str.empty() && str.back() == L'\0')
str.pop_back();
return str;
}
std::wstring TryAnsiToW(LPCSTR s)
{
return s ? AnsiToW(s) : std::wstring();
}
std::string TryBytesToStringA(const BYTE* bytes, int byteCount)
{
return byteCount <= 0 || bytes == nullptr ? "" : BytesToStringA(bytes, byteCount);
}
std::wstring TryBytesToStringW(const BYTE* bytes, int byteCount)
{
return byteCount <= 0 || bytes == nullptr ? L"" : BytesToStringW(bytes, byteCount);
}
std::wstring TryRawToW(LPCWSTR s)
{
return s ? std::wstring(s) : std::wstring();
}
}