Remove usages of CWStringView, except in Strings::to_utf8()

This commit is contained in:
Alexander Karatarakis 2017-10-16 13:50:28 -07:00
parent 7214c3583b
commit ced047ad78
8 changed files with 62 additions and 91 deletions

View File

@ -5,94 +5,65 @@
namespace vcpkg
{
template<class CharType>
struct BasicCStringView
struct CStringView
{
constexpr BasicCStringView() : cstr(nullptr) {}
constexpr BasicCStringView(const CharType* cstr) : cstr(cstr) {}
constexpr BasicCStringView(const BasicCStringView&) = default;
BasicCStringView(const std::basic_string<CharType>& str) : cstr(str.c_str()) {}
constexpr CStringView() : cstr(nullptr) {}
constexpr CStringView(const char* cstr) : cstr(cstr) {}
constexpr CStringView(const CStringView&) = default;
CStringView(const std::string& str) : cstr(str.c_str()) {}
constexpr const CharType* c_str() const { return cstr; }
constexpr const char* c_str() const { return cstr; }
private:
const CharType* cstr;
const char* cstr;
};
struct CWStringView
{
constexpr CWStringView() : cstr(nullptr) {}
constexpr CWStringView(const wchar_t* cstr) : cstr(cstr) {}
constexpr CWStringView(const CWStringView&) = default;
CWStringView(const std::wstring& str) : cstr(str.c_str()) {}
constexpr const wchar_t* c_str() const { return cstr; }
private:
const wchar_t* cstr;
};
namespace details
{
inline bool vcpkg_strcmp(const char* l, const char* r) { return strcmp(l, r) == 0; }
inline bool vcpkg_strcmp(const wchar_t* l, const wchar_t* r) { return wcscmp(l, r) == 0; }
}
template<class CharType>
bool operator==(const BasicCStringView<CharType>& l, const BasicCStringView<CharType>& r)
inline bool operator==(const CStringView& l, const CStringView& r)
{
return details::vcpkg_strcmp(l.c_str(), r.c_str());
}
template<class CharType>
bool operator==(const CharType* l, const BasicCStringView<CharType>& r)
{
return details::vcpkg_strcmp(l, r.c_str());
}
inline bool operator==(const char* l, const CStringView& r) { return details::vcpkg_strcmp(l, r.c_str()); }
template<class CharType>
bool operator==(const BasicCStringView<CharType>& r, const CharType* l)
{
return details::vcpkg_strcmp(l, r.c_str());
}
inline bool operator==(const CStringView& r, const char* l) { return details::vcpkg_strcmp(l, r.c_str()); }
template<class CharType>
bool operator==(const std::basic_string<CharType>& l, const BasicCStringView<CharType>& r)
{
return l == r.c_str();
}
inline bool operator==(const std::string& l, const CStringView& r) { return l == r.c_str(); }
template<class CharType>
bool operator==(const BasicCStringView<CharType>& r, const std::basic_string<CharType>& l)
{
return l == r.c_str();
}
inline bool operator==(const CStringView& r, const std::string& l) { return l == r.c_str(); }
// notequals
template<class CharType>
bool operator!=(const BasicCStringView<CharType>& l, const BasicCStringView<CharType>& r)
inline bool operator!=(const CStringView& l, const CStringView& r)
{
return !details::vcpkg_strcmp(l.c_str(), r.c_str());
}
template<class CharType>
bool operator!=(const CharType* l, const BasicCStringView<CharType>& r)
{
return !details::vcpkg_strcmp(l, r.c_str());
}
inline bool operator!=(const char* l, const CStringView& r) { return !details::vcpkg_strcmp(l, r.c_str()); }
template<class CharType>
bool operator!=(const BasicCStringView<CharType>& r, const CharType* l)
{
return !details::vcpkg_strcmp(l, r.c_str());
}
inline bool operator!=(const CStringView& r, const char* l) { return !details::vcpkg_strcmp(l, r.c_str()); }
template<class CharType>
bool operator!=(const BasicCStringView<CharType>& r, const std::basic_string<CharType>& l)
{
return l != r.c_str();
}
inline bool operator!=(const CStringView& r, const std::string& l) { return l != r.c_str(); }
template<class CharType>
bool operator!=(const std::basic_string<CharType>& l, const BasicCStringView<CharType>& r)
{
return l != r.c_str();
}
using CStringView = BasicCStringView<char>;
using CWStringView = BasicCStringView<wchar_t>;
inline bool operator!=(const std::string& l, const CStringView& r) { return l != r.c_str(); }
inline const char* to_printf_arg(const CStringView string_view) { return string_view.c_str(); }
inline const wchar_t* to_wprintf_arg(const CWStringView string_view) { return string_view.c_str(); }
static_assert(sizeof(CStringView) == sizeof(void*), "CStringView must be a simple wrapper around char*");
static_assert(sizeof(CWStringView) == sizeof(void*), "CWStringView must be a simple wrapper around wchar_t*");
}

View File

@ -47,9 +47,9 @@ namespace vcpkg::Strings
return details::wformat_internal(fmtstr, to_wprintf_arg(to_wprintf_arg(args))...);
}
std::wstring to_utf16(const CStringView s);
std::wstring to_utf16(const CStringView& s);
std::string to_utf8(const CWStringView w);
std::string to_utf8(const CWStringView& w);
std::string::const_iterator case_insensitive_ascii_find(const std::string& s, const std::string& pattern);

View File

@ -4,7 +4,6 @@
#include <vcpkg/base/optional.h>
#include <vcpkg/base/strings.h>
namespace vcpkg::System
{
tm get_current_date_time();
@ -64,9 +63,7 @@ namespace vcpkg::System
Optional<std::string> get_environment_variable(const CStringView varname) noexcept;
Optional<std::wstring> get_registry_string(void* base_hkey,
const CWStringView subkey,
const CWStringView valuename);
Optional<std::string> get_registry_string(void* base_hkey, const CStringView subkey, const CStringView valuename);
enum class CPUArchitecture
{
@ -76,7 +73,7 @@ namespace vcpkg::System
ARM64,
};
Optional<CPUArchitecture> to_cpu_architecture(CStringView arch);
Optional<CPUArchitecture> to_cpu_architecture(const CStringView& arch);
CPUArchitecture get_host_processor();

View File

@ -23,6 +23,6 @@ namespace vcpkg::Metrics
extern Util::LockGuarded<Metrics> g_metrics;
std::wstring get_SQM_user();
std::string get_SQM_user();
bool get_compiled_metrics_enabled();
}

View File

@ -72,7 +72,7 @@ namespace vcpkg::Strings::details
namespace vcpkg::Strings
{
std::wstring to_utf16(const CStringView s)
std::wstring to_utf16(const CStringView& s)
{
#if defined(_WIN32)
const int size = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, nullptr, 0);
@ -85,7 +85,7 @@ namespace vcpkg::Strings
#endif
}
std::string to_utf8(const CWStringView w)
std::string to_utf8(const CWStringView& w)
{
#if defined(_WIN32)
const int size = WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, nullptr, 0, nullptr, nullptr);

View File

@ -38,7 +38,7 @@ namespace vcpkg::System
#endif
}
Optional<CPUArchitecture> to_cpu_architecture(CStringView arch)
Optional<CPUArchitecture> to_cpu_architecture(const CStringView& arch)
{
if (Strings::case_insensitive_ascii_equals(arch, "x86")) return CPUArchitecture::X86;
if (Strings::case_insensitive_ascii_equals(arch, "x64")) return CPUArchitecture::X64;
@ -153,7 +153,7 @@ namespace vcpkg::System
memset(&process_info, 0, sizeof(PROCESS_INFORMATION));
// Basically we are wrapping it in quotes
std::string actual_cmd_line = Strings::format(R"###(cmd.exe /c "%s")###", cmd_line);
const std::string actual_cmd_line = Strings::format(R"###(cmd.exe /c "%s")###", cmd_line);
Debug::println("CreateProcessW(%s)", actual_cmd_line);
bool succeeded = TRUE == CreateProcessW(nullptr,
Strings::to_utf16(actual_cmd_line).data(),
@ -331,35 +331,39 @@ namespace vcpkg::System
}
#if defined(_WIN32)
static bool is_string_keytype(DWORD hkey_type)
static bool is_string_keytype(const DWORD hkey_type)
{
return hkey_type == REG_SZ || hkey_type == REG_MULTI_SZ || hkey_type == REG_EXPAND_SZ;
}
Optional<std::wstring> get_registry_string(void* base_hkey,
const CWStringView sub_key,
const CWStringView valuename)
Optional<std::string> get_registry_string(void* base_hkey, const CStringView sub_key, const CStringView valuename)
{
HKEY k = nullptr;
const LSTATUS ec = RegOpenKeyExW(reinterpret_cast<HKEY>(base_hkey), sub_key.c_str(), NULL, KEY_READ, &k);
const LSTATUS ec =
RegOpenKeyExW(reinterpret_cast<HKEY>(base_hkey), Strings::to_utf16(sub_key).c_str(), NULL, KEY_READ, &k);
if (ec != ERROR_SUCCESS) return nullopt;
DWORD dw_buffer_size = 0;
DWORD dw_type = 0;
auto rc = RegQueryValueExW(k, valuename.c_str(), nullptr, &dw_type, nullptr, &dw_buffer_size);
auto rc =
RegQueryValueExW(k, Strings::to_utf16(valuename).c_str(), nullptr, &dw_type, nullptr, &dw_buffer_size);
if (rc != ERROR_SUCCESS || !is_string_keytype(dw_type) || dw_buffer_size == 0 ||
dw_buffer_size % sizeof(wchar_t) != 0)
return nullopt;
std::wstring ret;
ret.resize(dw_buffer_size / sizeof(wchar_t));
rc = RegQueryValueExW(
k, valuename.c_str(), nullptr, &dw_type, reinterpret_cast<LPBYTE>(ret.data()), &dw_buffer_size);
rc = RegQueryValueExW(k,
Strings::to_utf16(valuename).c_str(),
nullptr,
&dw_type,
reinterpret_cast<LPBYTE>(ret.data()),
&dw_buffer_size);
if (rc != ERROR_SUCCESS || !is_string_keytype(dw_type) || dw_buffer_size != sizeof(wchar_t) * ret.size())
return nullopt;
ret.pop_back(); // remove extra trailing null byte
return ret;
return Strings::to_utf8(ret);
}
#else
Optional<std::wstring> get_registry_string(void* base_hkey,

View File

@ -3,25 +3,24 @@
#include <vcpkg/base/system.h>
#include <vcpkg/commands.h>
#include <vcpkg/help.h>
#include <vcpkg/input.h>
#include <vcpkg/paragraphs.h>
namespace vcpkg::Commands::Edit
{
static std::vector<fs::path> find_from_registry()
{
static const std::array<const wchar_t*, 3> REGKEYS = {
LR"(SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{C26E74D1-022E-4238-8B9D-1E7564A36CC9}_is1)",
LR"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1287CAD5-7C8D-410D-88B9-0D1EE4A83FF2}_is1)",
LR"(SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{F8A2A208-72B3-4D61-95FC-8A65D340689B}_is1)",
static const std::array<const char*, 3> REGKEYS = {
R"(SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{C26E74D1-022E-4238-8B9D-1E7564A36CC9}_is1)",
R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1287CAD5-7C8D-410D-88B9-0D1EE4A83FF2}_is1)",
R"(SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{F8A2A208-72B3-4D61-95FC-8A65D340689B}_is1)",
};
std::vector<fs::path> output;
#if defined(_WIN32)
for (auto&& keypath : REGKEYS)
{
const Optional<std::wstring> code_installpath =
System::get_registry_string(HKEY_LOCAL_MACHINE, keypath, L"InstallLocation");
const Optional<std::string> code_installpath =
System::get_registry_string(HKEY_LOCAL_MACHINE, keypath, "InstallLocation");
if (const auto c = code_installpath.get())
{
const fs::path install_path = fs::path(*c);

View File

@ -233,14 +233,14 @@ namespace vcpkg::Metrics
bool get_compiled_metrics_enabled() { return DISABLE_METRICS == 0; }
std::wstring get_SQM_user()
std::string get_SQM_user()
{
#if defined(_WIN32)
auto hkcu_sqmclient =
System::get_registry_string(HKEY_CURRENT_USER, LR"(Software\Microsoft\SQMClient)", L"UserId");
return hkcu_sqmclient.value_or(L"{}");
System::get_registry_string(HKEY_CURRENT_USER, R"(Software\Microsoft\SQMClient)", "UserId");
return hkcu_sqmclient.value_or("{}");
#else
return L"{}";
return "{}";
#endif
}