Common/StringUtil: Add a FromChars std::optional wrapper
This commit is contained in:
parent
86094b8080
commit
da0059fb21
|
@ -1,7 +1,9 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <cstddef>
|
#include <charconv>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
|
#include <cstddef>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace StringUtil {
|
namespace StringUtil {
|
||||||
|
@ -26,4 +28,16 @@ inline int Strcasecmp(const char* s1, const char* s2)
|
||||||
#endif
|
#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;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace StringUtil
|
} // namespace StringUtil
|
Loading…
Reference in New Issue