diff --git a/src/utils/yaml_utils.cpp b/src/utils/yaml_utils.cpp index 51ad8deaf..625b1e789 100644 --- a/src/utils/yaml_utils.cpp +++ b/src/utils/yaml_utils.cpp @@ -9,6 +9,7 @@ #include "yaml_utils.hpp" #include "common.hpp" +#include #include #include @@ -16,6 +17,19 @@ namespace yaml { +static YAML::Node OctalFix(const YAML::Node &node) +{ + auto s = node.as(); + + if (!std::all_of(s.begin(), s.end(), ::isdigit)) + return node; + if (!s.empty() && s[0] != '0') + return node; + + s.erase(0, std::min(s.find_first_not_of('0'), s.size() - 1)); + return YAML::Node{s}; +} + [[noreturn]] void FieldError(const std::string &name, const std::string &error) { throw std::runtime_error("Field '" + name + "' " + error + "."); @@ -40,12 +54,8 @@ void AssertHasFields(const YAML::Node &node, const std::vector &fie int GetInt32(const YAML::Node &node, const std::string &name) { - auto nodeValue = node[name].Scalar(); - nodeValue.erase(0, std::min(nodeValue.find_first_not_of('0'), nodeValue.size()-1)); - YAML::Node modifiedNode; - modifiedNode[name] = nodeValue; - AssertHasInt32(modifiedNode, name); - return modifiedNode[name].as(); + AssertHasInt32(node, name); + return OctalFix(node[name]).as(); } int GetInt32(const YAML::Node &node, const std::string &name, std::optional minValue, std::optional maxValue) @@ -63,7 +73,7 @@ void AssertHasInt32(const YAML::Node &node, const std::string &name) AssertHasField(node, name); try { - node[name].as(); + OctalFix(node[name]).as(); } catch (const std::runtime_error &e) { @@ -106,7 +116,7 @@ void AssertHasInt64(const YAML::Node &node, const std::string &name) AssertHasField(node, name); try { - node[name].as(); + OctalFix(node[name]).as(); } catch (const std::runtime_error &e) { @@ -117,7 +127,7 @@ void AssertHasInt64(const YAML::Node &node, const std::string &name) int64_t GetInt64(const YAML::Node &node, const std::string &name) { AssertHasInt64(node, name); - return node[name].as(); + return OctalFix(node[name]).as(); } int64_t GetInt64(const YAML::Node &node, const std::string &name, std::optional minValue,