2020-01-10 03:31:12 +00:00
|
|
|
#pragma once
|
2020-03-06 11:55:25 +00:00
|
|
|
#include "types.h"
|
2020-01-10 03:31:12 +00:00
|
|
|
#include <cstdarg>
|
2020-02-28 07:00:05 +00:00
|
|
|
#include <cstddef>
|
2020-01-10 03:31:12 +00:00
|
|
|
#include <cstring>
|
2020-12-10 21:42:04 +00:00
|
|
|
#include <iomanip>
|
2020-02-28 07:00:05 +00:00
|
|
|
#include <optional>
|
2020-01-10 03:31:12 +00:00
|
|
|
#include <string>
|
2020-07-22 16:36:23 +00:00
|
|
|
#include <string_view>
|
2020-12-10 21:42:04 +00:00
|
|
|
#include <vector>
|
2020-01-10 03:31:12 +00:00
|
|
|
|
2020-02-28 07:12:49 +00:00
|
|
|
#if defined(__has_include) && __has_include(<charconv>)
|
2020-02-28 07:00:16 +00:00
|
|
|
#include <charconv>
|
2020-06-30 14:33:40 +00:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
#include <sstream>
|
|
|
|
#endif
|
2020-02-28 07:00:16 +00:00
|
|
|
#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);
|
|
|
|
|
2020-07-22 16:36:23 +00:00
|
|
|
/// Strlcpy from string_view.
|
|
|
|
std::size_t Strlcpy(char* dst, const std::string_view& src, std::size_t size);
|
|
|
|
|
2020-01-10 03:31:12 +00:00
|
|
|
/// Platform-independent strcasecmp
|
2020-03-06 11:55:25 +00:00
|
|
|
static inline int Strcasecmp(const char* s1, const char* s2)
|
2020-01-10 03:31:12 +00:00
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return _stricmp(s1, s2);
|
|
|
|
#else
|
|
|
|
return strcasecmp(s1, s2);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-06-30 14:33:40 +00:00
|
|
|
/// Platform-independent strcasecmp
|
|
|
|
static inline int Strncasecmp(const char* s1, const char* s2, std::size_t n)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return _strnicmp(s1, s2, n);
|
|
|
|
#else
|
|
|
|
return strncasecmp(s1, s2, n);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:05 +00:00
|
|
|
/// Wrapper arond std::from_chars
|
2020-12-10 21:42:04 +00:00
|
|
|
template<typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
|
|
|
inline std::optional<T> FromChars(const std::string_view& str, int base = 10)
|
2020-02-28 07:00:05 +00:00
|
|
|
{
|
|
|
|
T value;
|
2020-02-28 07:00:16 +00:00
|
|
|
|
2020-02-28 07:12:49 +00:00
|
|
|
#if defined(__has_include) && __has_include(<charconv>)
|
2020-12-10 21:42:04 +00:00
|
|
|
const std::from_chars_result result = std::from_chars(str.data(), str.data() + str.length(), value, base);
|
|
|
|
if (result.ec != std::errc())
|
|
|
|
return std::nullopt;
|
|
|
|
#else
|
|
|
|
std::string temp(str);
|
|
|
|
std::istringstream ss(temp);
|
|
|
|
ss >> std::setbase(base) >> value;
|
|
|
|
if (ss.fail())
|
|
|
|
return std::nullopt;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
|
|
|
|
inline std::optional<T> FromChars(const std::string_view& str)
|
|
|
|
{
|
|
|
|
T value;
|
|
|
|
|
|
|
|
#if defined(__has_include) && __has_include(<charconv>) && defined(_MSC_VER)
|
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
|
2020-12-10 21:42:04 +00:00
|
|
|
/// libstdc++ does not support from_chars with floats yet
|
2020-02-28 07:00:16 +00:00
|
|
|
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-06-30 14:33:40 +00:00
|
|
|
/// Explicit override for booleans
|
|
|
|
template<>
|
2020-12-10 21:42:04 +00:00
|
|
|
inline std::optional<bool> FromChars(const std::string_view& str, int base)
|
2020-06-30 14:33:40 +00:00
|
|
|
{
|
|
|
|
if (Strncasecmp("true", str.data(), str.length()) == 0 || Strncasecmp("yes", str.data(), str.length()) == 0 ||
|
|
|
|
Strncasecmp("on", str.data(), str.length()) == 0 || Strncasecmp("1", str.data(), str.length()) == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Strncasecmp("false", str.data(), str.length()) == 0 || Strncasecmp("no", str.data(), str.length()) == 0 ||
|
|
|
|
Strncasecmp("off", str.data(), str.length()) == 0 || Strncasecmp("0", str.data(), str.length()) == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2020-12-10 21:42:04 +00:00
|
|
|
/// Encode/decode hexadecimal byte buffers
|
|
|
|
std::optional<std::vector<u8>> DecodeHex(const std::string_view& str);
|
|
|
|
std::string EncodeHex(const u8* data, int length);
|
2020-06-30 14:33:40 +00:00
|
|
|
|
2020-03-06 11:55:25 +00:00
|
|
|
/// starts_with from C++20
|
2020-07-31 06:02:32 +00:00
|
|
|
ALWAYS_INLINE static bool StartsWith(const std::string_view& str, const char* prefix)
|
2020-03-06 11:55:25 +00:00
|
|
|
{
|
|
|
|
return (str.compare(0, std::strlen(prefix), prefix) == 0);
|
|
|
|
}
|
2020-10-31 04:39:38 +00:00
|
|
|
ALWAYS_INLINE static bool EndsWith(const std::string_view& str, const char* suffix)
|
|
|
|
{
|
|
|
|
const std::size_t suffix_length = std::strlen(suffix);
|
|
|
|
return (str.length() >= suffix_length && str.compare(str.length() - suffix_length, suffix_length, suffix) == 0);
|
|
|
|
}
|
2020-03-06 11:55:25 +00:00
|
|
|
|
2020-07-31 06:02:32 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
|
|
|
|
/// Converts the specified UTF-8 string to a wide string.
|
|
|
|
std::wstring UTF8StringToWideString(const std::string_view& str);
|
2020-08-01 04:00:58 +00:00
|
|
|
bool UTF8StringToWideString(std::wstring& dest, const std::string_view& str);
|
2020-07-31 06:02:32 +00:00
|
|
|
|
|
|
|
/// Converts the specified wide string to a UTF-8 string.
|
|
|
|
std::string WideStringToUTF8String(const std::wstring_view& str);
|
2020-08-01 04:00:58 +00:00
|
|
|
bool WideStringToUTF8String(std::string& dest, const std::wstring_view& str);
|
2020-07-31 06:02:32 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-02-28 07:00:16 +00:00
|
|
|
} // namespace StringUtil
|