From c5773202b5d4933297e4fedc506e2cb8a36291be Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 3 Mar 2021 00:30:16 -0600 Subject: [PATCH] [ox/std] Make ox_atoi return an ox::Result --- deps/ox/src/ox/clargs/clargs.cpp | 4 +++- deps/ox/src/ox/claw/read.cpp | 4 +++- deps/ox/src/ox/std/strops.hpp | 13 ++++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/deps/ox/src/ox/clargs/clargs.cpp b/deps/ox/src/ox/clargs/clargs.cpp index 35aacf65..be748018 100644 --- a/deps/ox/src/ox/clargs/clargs.cpp +++ b/deps/ox/src/ox/clargs/clargs.cpp @@ -28,7 +28,9 @@ ClArgs::ClArgs(int argc, const char **args) { m_bools[arg] = false; } m_strings[arg] = val; - m_ints[arg] = ox_atoi(val.c_str()); + if (auto r = ox_atoi(val.c_str()); r.error == 0) { + m_ints[arg] = r.value; + } i++; } } diff --git a/deps/ox/src/ox/claw/read.cpp b/deps/ox/src/ox/claw/read.cpp index da3d6a5b..58d5f0b7 100644 --- a/deps/ox/src/ox/claw/read.cpp +++ b/deps/ox/src/ox/claw/read.cpp @@ -49,7 +49,9 @@ Result readHeader(const char *buff, std::size_t buffLen) noexcept { return OxError(1); } hdr.typeName = typeName; - hdr.typeVersion = ox_atoi(versionStr.c_str()); + if (auto r = ox_atoi(versionStr.c_str()); r.error == 0) { + hdr.typeVersion = r.value; + } hdr.data = buff; hdr.dataSize = buffLen; return hdr; diff --git a/deps/ox/src/ox/std/strops.hpp b/deps/ox/src/ox/std/strops.hpp index d2534638..5dc646e4 100644 --- a/deps/ox/src/ox/std/strops.hpp +++ b/deps/ox/src/ox/std/strops.hpp @@ -8,6 +8,7 @@ #pragma once +#include "error.hpp" #include "math.hpp" #include "types.hpp" #include "typetraits.hpp" @@ -125,15 +126,17 @@ template return retval; } -[[nodiscard]] constexpr int ox_atoi(const char *str) noexcept { +[[nodiscard]] constexpr ox::Result ox_atoi(const char *str) noexcept { int total = 0; int multiplier = 1; - for (auto i = static_cast(ox_strlen(str)) - 1; i != -1; i--) { - total += (str[i] - '0') * multiplier; - multiplier *= 10; + if (str[i] >= '0' && str[i] <= '9') { + total += (str[i] - '0') * multiplier; + multiplier *= 10; + } else { + return OxError(1); + } } - return total; }