Common/StringUtil: Support compiling on gcc7
This commit is contained in:
parent
bbdee22ea8
commit
122726fe65
|
@ -1,11 +1,16 @@
|
|||
#pragma once
|
||||
#include <charconv>
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#if __cplusplus >= 201703L
|
||||
#include <charconv>
|
||||
#else
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
namespace StringUtil {
|
||||
|
||||
/// Constructs a std::string from a format string.
|
||||
|
@ -33,11 +38,21 @@ template<typename T>
|
|||
std::optional<T> FromChars(const std::string_view str)
|
||||
{
|
||||
T value;
|
||||
|
||||
#if __cplusplus >= 201703L
|
||||
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;
|
||||
#else
|
||||
std::string temp(str);
|
||||
std::istringstream ss(temp);
|
||||
ss >> value;
|
||||
if (ss.fail())
|
||||
return std::nullopt;
|
||||
#endif
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
} // namespace StringUtil
|
||||
} // namespace StringUtil
|
||||
|
|
Loading…
Reference in New Issue