From 3dd279106671d36eebbc7a1ee20d53dcee2e32e0 Mon Sep 17 00:00:00 2001 From: David Markowitz <39972741+EmosewaMC@users.noreply.github.com> Date: Mon, 9 Oct 2023 13:22:40 -0700 Subject: [PATCH] chore: Use TryParse for LDF parsing (#1206) * LDF: Simplify parsing * Update GeneralUtils.h --- dCommon/GeneralUtils.h | 5 ++++ dCommon/LDFFormat.cpp | 44 ++++++++++++---------------- tests/dCommonTests/TestLDFFormat.cpp | 2 +- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/dCommon/GeneralUtils.h b/dCommon/GeneralUtils.h index e9e20ba00..ec41c19ba 100644 --- a/dCommon/GeneralUtils.h +++ b/dCommon/GeneralUtils.h @@ -126,6 +126,11 @@ namespace GeneralUtils { template T Parse(const char* value); + template <> + inline bool Parse(const char* value) { + return std::stoi(value); + } + template <> inline int32_t Parse(const char* value) { return std::stoi(value); diff --git a/dCommon/LDFFormat.cpp b/dCommon/LDFFormat.cpp index cb921842b..5278747ca 100644 --- a/dCommon/LDFFormat.cpp +++ b/dCommon/LDFFormat.cpp @@ -61,35 +61,33 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) { } case LDF_TYPE_S32: { - try { - int32_t data = static_cast(strtoul(ldfTypeAndValue.second.data(), &storage, 10)); - returnValue = new LDFData(key, data); - } catch (std::exception) { + int32_t data; + if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid int32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } + returnValue = new LDFData(key, data); + break; } case LDF_TYPE_FLOAT: { - try { - float data = strtof(ldfTypeAndValue.second.data(), &storage); - returnValue = new LDFData(key, data); - } catch (std::exception) { + float data; + if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid float value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } + returnValue = new LDFData(key, data); break; } case LDF_TYPE_DOUBLE: { - try { - double data = strtod(ldfTypeAndValue.second.data(), &storage); - returnValue = new LDFData(key, data); - } catch (std::exception) { + double data; + if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid double value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } + returnValue = new LDFData(key, data); break; } @@ -102,9 +100,7 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) { } else if (ldfTypeAndValue.second == "false") { data = 0; } else { - try { - data = static_cast(strtoul(ldfTypeAndValue.second.data(), &storage, 10)); - } catch (std::exception) { + if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid uint32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } @@ -122,9 +118,7 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) { } else if (ldfTypeAndValue.second == "false") { data = false; } else { - try { - data = static_cast(strtol(ldfTypeAndValue.second.data(), &storage, 10)); - } catch (std::exception) { + if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid bool value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } @@ -135,24 +129,22 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) { } case LDF_TYPE_U64: { - try { - uint64_t data = static_cast(strtoull(ldfTypeAndValue.second.data(), &storage, 10)); - returnValue = new LDFData(key, data); - } catch (std::exception) { + uint64_t data; + if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid uint64 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } + returnValue = new LDFData(key, data); break; } case LDF_TYPE_OBJID: { - try { - LWOOBJID data = static_cast(strtoll(ldfTypeAndValue.second.data(), &storage, 10)); - returnValue = new LDFData(key, data); - } catch (std::exception) { + LWOOBJID data; + if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) { Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid LWOOBJID value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data()); return nullptr; } + returnValue = new LDFData(key, data); break; } diff --git a/tests/dCommonTests/TestLDFFormat.cpp b/tests/dCommonTests/TestLDFFormat.cpp index 36326e38d..9ca847759 100644 --- a/tests/dCommonTests/TestLDFFormat.cpp +++ b/tests/dCommonTests/TestLDFFormat.cpp @@ -17,7 +17,7 @@ class LDFTests : public dCommonDependenciesTest { } }; -#define LdfUniquePtr std::unique_ptr +typedef std::unique_ptr LdfUniquePtr; // Suite of tests for parsing LDF values