mirror of
https://github.com/DarkflameUniverse/DarkflameServer
synced 2024-08-30 18:43:58 +00:00
ignore whitespace in try parse (#1536)
This commit is contained in:
parent
feeac2e041
commit
3260a063cb
@ -156,8 +156,11 @@ namespace GeneralUtils {
|
||||
* @returns An std::optional containing the desired value if it is equivalent to the string
|
||||
*/
|
||||
template <Numeric T>
|
||||
[[nodiscard]] std::optional<T> TryParse(const std::string_view str) {
|
||||
[[nodiscard]] std::optional<T> TryParse(std::string_view str) {
|
||||
numeric_parse_t<T> result;
|
||||
if (!str.empty()) {
|
||||
while (std::isspace(str.front())) str.remove_prefix(1);
|
||||
}
|
||||
|
||||
const char* const strEnd = str.data() + str.size();
|
||||
const auto [parseEnd, ec] = std::from_chars(str.data(), strEnd, result);
|
||||
@ -181,8 +184,12 @@ namespace GeneralUtils {
|
||||
* @returns An std::optional containing the desired value if it is equivalent to the string
|
||||
*/
|
||||
template <std::floating_point T>
|
||||
[[nodiscard]] std::optional<T> TryParse(const std::string_view str) noexcept
|
||||
[[nodiscard]] std::optional<T> TryParse(std::string_view str) noexcept
|
||||
try {
|
||||
if (!str.empty()) {
|
||||
while (std::isspace(str.front())) str.remove_prefix(1);
|
||||
}
|
||||
|
||||
size_t parseNum;
|
||||
const T result = details::_parse<T>(str, parseNum);
|
||||
const bool isParsed = str.length() == parseNum;
|
||||
|
Loading…
Reference in New Issue
Block a user