From dcd3a6c62bf3bb6355faf17afc24374e077f6f20 Mon Sep 17 00:00:00 2001 From: chenguoping Date: Mon, 23 Mar 2020 17:24:47 +0800 Subject: [PATCH] move the catch of std::invalid_argument into array_index() --- include/nlohmann/detail/json_pointer.hpp | 79 +++++++----------------- single_include/nlohmann/json.hpp | 79 +++++++----------------- 2 files changed, 44 insertions(+), 114 deletions(-) diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index c8fd2825..0acf924e 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -344,7 +344,15 @@ class json_pointer } std::size_t processed_chars = 0; - const int res = std::stoi(s, &processed_chars); + int res = 0; + JSON_TRY + { + res = std::stoi(s, &processed_chars); + } + JSON_CATCH(std::invalid_argument&) + { + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number")); + } // check if the string was completely read if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size())) @@ -411,14 +419,7 @@ class json_pointer case detail::value_t::array: { // create an entry in the array - JSON_TRY - { - result = &result->operator[](static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + result = &result->operator[](static_cast(array_index(reference_token))); break; } @@ -496,15 +497,8 @@ class json_pointer else { // convert array index to number; unchecked access - JSON_TRY - { - ptr = &ptr->operator[]( - static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->operator[]( + static_cast(array_index(reference_token))); } break; } @@ -548,14 +542,7 @@ class json_pointer } // note: at performs range check - JSON_TRY - { - ptr = &ptr->at(static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->at(static_cast(array_index(reference_token))); break; } @@ -605,15 +592,8 @@ class json_pointer } // use unchecked array access - JSON_TRY - { - ptr = &ptr->operator[]( - static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->operator[]( + static_cast(array_index(reference_token))); break; } @@ -656,14 +636,7 @@ class json_pointer } // note: at performs range check - JSON_TRY - { - ptr = &ptr->at(static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->at(static_cast(array_index(reference_token))); break; } @@ -706,22 +679,14 @@ class json_pointer return false; } - JSON_TRY + const auto idx = static_cast(array_index(reference_token)); + if (idx >= ptr->size()) { - const auto idx = static_cast(array_index(reference_token)); - if (idx >= ptr->size()) - { - // index out of range - return false; - } + // index out of range + return false; + } - ptr = &ptr->operator[](idx); - break; - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->operator[](idx); break; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ca3e8171..5c93426e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10418,7 +10418,15 @@ class json_pointer } std::size_t processed_chars = 0; - const int res = std::stoi(s, &processed_chars); + int res = 0; + JSON_TRY + { + res = std::stoi(s, &processed_chars); + } + JSON_CATCH(std::invalid_argument&) + { + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number")); + } // check if the string was completely read if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size())) @@ -10485,14 +10493,7 @@ class json_pointer case detail::value_t::array: { // create an entry in the array - JSON_TRY - { - result = &result->operator[](static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + result = &result->operator[](static_cast(array_index(reference_token))); break; } @@ -10570,15 +10571,8 @@ class json_pointer else { // convert array index to number; unchecked access - JSON_TRY - { - ptr = &ptr->operator[]( - static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->operator[]( + static_cast(array_index(reference_token))); } break; } @@ -10622,14 +10616,7 @@ class json_pointer } // note: at performs range check - JSON_TRY - { - ptr = &ptr->at(static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->at(static_cast(array_index(reference_token))); break; } @@ -10679,15 +10666,8 @@ class json_pointer } // use unchecked array access - JSON_TRY - { - ptr = &ptr->operator[]( - static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->operator[]( + static_cast(array_index(reference_token))); break; } @@ -10730,14 +10710,7 @@ class json_pointer } // note: at performs range check - JSON_TRY - { - ptr = &ptr->at(static_cast(array_index(reference_token))); - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->at(static_cast(array_index(reference_token))); break; } @@ -10780,22 +10753,14 @@ class json_pointer return false; } - JSON_TRY + const auto idx = static_cast(array_index(reference_token)); + if (idx >= ptr->size()) { - const auto idx = static_cast(array_index(reference_token)); - if (idx >= ptr->size()) - { - // index out of range - return false; - } + // index out of range + return false; + } - ptr = &ptr->operator[](idx); - break; - } - JSON_CATCH(std::invalid_argument&) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number")); - } + ptr = &ptr->operator[](idx); break; }