Duckstation/src/common/string_util.h

43 lines
1.1 KiB
C
Raw Normal View History

2020-01-10 03:31:12 +00:00
#pragma once
#include <charconv>
2020-01-10 03:31:12 +00:00
#include <cstdarg>
#include <cstddef>
2020-01-10 03:31:12 +00:00
#include <cstring>
#include <optional>
2020-01-10 03:31:12 +00:00
#include <string>
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
}
/// Wrapper arond std::from_chars
template<typename T>
std::optional<T> FromChars(const std::string_view str)
{
T value;
const std::from_chars_result result = std::from_chars(str.data(), str.data() + str.length(), value);
if (result.ec != std::errc())
return std::nullopt;
return value;
}
2020-01-10 03:31:12 +00:00
} // namespace StringUtil