2020-01-10 03:31:12 +00:00
|
|
|
#pragma once
|
|
|
|
#include <cstdarg>
|
2020-02-28 07:00:05 +00:00
|
|
|
#include <cstddef>
|
2020-01-10 03:31:12 +00:00
|
|
|
#include <cstring>
|
2020-02-28 07:00:05 +00:00
|
|
|
#include <optional>
|
2020-01-10 03:31:12 +00:00
|
|
|
#include <string>
|
|
|
|
|
2020-02-28 07:00:16 +00:00
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
#include <charconv>
|
|
|
|
#else
|
|
|
|
#include <sstream>
|
|
|
|
#endif
|
|
|
|
|
2020-01-10 03:31:12 +00:00
|
|
|
namespace StringUtil {
|
|
|
|
|
|
|
|
/// Constructs a std::string from a format string.
|
|
|
|
std::string StdStringFromFormat(const char* format, ...);
|
|
|
|
std::string StdStringFromFormatV(const char* format, std::va_list ap);
|
|
|
|
|
|
|
|
/// Checks if a wildcard matches a search string.
|
|
|
|
bool WildcardMatch(const char* subject, const char* mask, bool case_sensitive = true);
|
|
|
|
|
|
|
|
/// Safe version of strlcpy.
|
|
|
|
std::size_t Strlcpy(char* dst, const char* src, std::size_t size);
|
|
|
|
|
|
|
|
/// Platform-independent strcasecmp
|
|
|
|
inline int Strcasecmp(const char* s1, const char* s2)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return _stricmp(s1, s2);
|
|
|
|
#else
|
|
|
|
return strcasecmp(s1, s2);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:05 +00:00
|
|
|
/// Wrapper arond std::from_chars
|
|
|
|
template<typename T>
|
|
|
|
std::optional<T> FromChars(const std::string_view str)
|
|
|
|
{
|
|
|
|
T value;
|
2020-02-28 07:00:16 +00:00
|
|
|
|
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
T value;
|
2020-02-28 07:00:05 +00:00
|
|
|
const std::from_chars_result result = std::from_chars(str.data(), str.data() + str.length(), value);
|
|
|
|
if (result.ec != std::errc())
|
|
|
|
return std::nullopt;
|
2020-02-28 07:00:16 +00:00
|
|
|
#else
|
|
|
|
std::string temp(str);
|
|
|
|
std::istringstream ss(temp);
|
|
|
|
ss >> value;
|
|
|
|
if (ss.fail())
|
|
|
|
return std::nullopt;
|
|
|
|
#endif
|
2020-02-28 07:00:05 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:16 +00:00
|
|
|
} // namespace StringUtil
|