-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathstring.cpp
77 lines (67 loc) · 1.84 KB
/
string.cpp
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
75
76
77
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include "string.h"
#ifdef _WIN32
#include <Windows.h>
#define tmp_buffer_size 512
#else
#include "miniutf.hpp"
#endif
namespace trace
{
std::string ToString(const std::string& str)
{
return str;
}
std::string ToString(const char* str)
{
return std::string(str);
}
std::string ToString(const uint64_t i)
{
return std::to_string(i);
}
std::string ToString(const WSTRING& wstr)
{
#ifdef _WIN32
if (wstr.empty())
return std::string();
std::string tmpStr(tmp_buffer_size, 0);
int size_needed =
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &tmpStr[0], tmp_buffer_size, NULL, NULL);
if (size_needed < tmp_buffer_size)
{
return tmpStr.substr(0, size_needed);
}
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
#else
std::u16string ustr(reinterpret_cast<const char16_t*>(wstr.c_str()));
return miniutf::to_utf8(ustr);
#endif
}
WSTRING ToWSTRING(const std::string& str)
{
#ifdef _WIN32
if (str.empty())
return std::wstring();
std::wstring tmpStr(tmp_buffer_size, 0);
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &tmpStr[0], tmp_buffer_size);
if (size_needed < tmp_buffer_size)
{
return tmpStr.substr(0, size_needed);
}
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
#else
auto ustr = miniutf::to_utf16(str);
return WSTRING(reinterpret_cast<const WCHAR*>(ustr.c_str()));
#endif
}
WSTRING ToWSTRING(const uint64_t i)
{
return WSTRING(reinterpret_cast<const WCHAR*>(std::to_wstring(i).c_str()));
}
} // namespace trace