[ox/std] Make strToInt return error for empty string
Some checks failed
Build / build (push) Has been cancelled
Some checks failed
Build / build (push) Has been cancelled
This commit is contained in:
5
deps/ox/src/ox/std/stringview.hpp
vendored
5
deps/ox/src/ox/std/stringview.hpp
vendored
@ -104,13 +104,16 @@ constexpr ox::Result<int> strToInt(StringViewCR str) noexcept {
|
|||||||
OX_ALLOW_UNSAFE_BUFFERS_BEGIN
|
OX_ALLOW_UNSAFE_BUFFERS_BEGIN
|
||||||
int total = 0;
|
int total = 0;
|
||||||
int multiplier = 1;
|
int multiplier = 1;
|
||||||
|
if (str.len() == 0) [[unlikely]] {
|
||||||
|
return Error{1, "Empty string passed to strToInt"};
|
||||||
|
}
|
||||||
for (auto i = static_cast<int64_t>(str.len()) - 1; i != -1; --i) {
|
for (auto i = static_cast<int64_t>(str.len()) - 1; i != -1; --i) {
|
||||||
auto s = static_cast<std::size_t>(i);
|
auto s = static_cast<std::size_t>(i);
|
||||||
if (str[s] >= '0' && str[s] <= '9') {
|
if (str[s] >= '0' && str[s] <= '9') {
|
||||||
total += (str[s] - '0') * multiplier;
|
total += (str[s] - '0') * multiplier;
|
||||||
multiplier *= 10;
|
multiplier *= 10;
|
||||||
} else {
|
} else {
|
||||||
return ox::Error(1);
|
return ox::Error{1};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return total;
|
return total;
|
||||||
|
Reference in New Issue
Block a user