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