mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2024-11-22 05:27:19 +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
|
* @returns An std::optional containing the desired value if it is equivalent to the string
|
||||||
*/
|
*/
|
||||||
template <Numeric T>
|
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;
|
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 char* const strEnd = str.data() + str.size();
|
||||||
const auto [parseEnd, ec] = std::from_chars(str.data(), strEnd, result);
|
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
|
* @returns An std::optional containing the desired value if it is equivalent to the string
|
||||||
*/
|
*/
|
||||||
template <std::floating_point T>
|
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 {
|
try {
|
||||||
|
if (!str.empty()) {
|
||||||
|
while (std::isspace(str.front())) str.remove_prefix(1);
|
||||||
|
}
|
||||||
|
|
||||||
size_t parseNum;
|
size_t parseNum;
|
||||||
const T result = details::_parse<T>(str, parseNum);
|
const T result = details::_parse<T>(str, parseNum);
|
||||||
const bool isParsed = str.length() == parseNum;
|
const bool isParsed = str.length() == parseNum;
|
||||||
|
Loading…
Reference in New Issue
Block a user