diff --git a/include/etl/to_arithmetic.h b/include/etl/to_arithmetic.h index 47caf310..8018e653 100644 --- a/include/etl/to_arithmetic.h +++ b/include/etl/to_arithmetic.h @@ -829,6 +829,7 @@ namespace etl //*************************************************************************** /// Text to integral from view and radix value type. + ///\tparam TIntermediate One of int32t, uint32_t, int64_t or uint64_t. //*************************************************************************** template ETL_NODISCARD @@ -836,11 +837,9 @@ namespace etl etl::optional to_arithmetic_integral(const etl::basic_string_view& view, const etl::radix::value_type radix) { - using namespace etl::private_to_arithmetic; - ETL_ASSERT_AND_RETURN_VALUE(is_valid_radix(radix), ETL_ERROR(etl::to_arithmetic_invalid_radix), false); - etl::optional result; + etl::optional intermediate_result; etl::basic_string_view::const_iterator itr = view.begin(); const etl::basic_string_view::const_iterator itr_end = view.end(); @@ -853,23 +852,19 @@ namespace etl { const char first_char = convert(*itr); - ETL_ASSERT(!((first_char == valid_character_set::negative_char) && etl::is_unsigned::value), ETL_ERROR(etl::to_arithmetic_signed_to_unsigned)); + ETL_ASSERT(!((first_char == valid_character_set::negative_char) && etl::is_unsigned::value), ETL_ERROR(etl::to_arithmetic_signed_to_unsigned)); - typedef typename intermediate::type intermediate_type; - integral_accumulator accumulator(radix); + integral_accumulator accumulator(radix); while ((itr != itr_end) && accumulator.add(convert(*itr))) { // Keep looping until done or an error occurs. ++itr; - } - - etl::optional intermediate_result; + } if (accumulator.has_value()) { intermediate_result = accumulator.get_value(); - result = static_cast(intermediate_result.value()); } else { @@ -877,7 +872,7 @@ namespace etl } } - return result; + return intermediate_result; } } @@ -893,45 +888,17 @@ namespace etl { using namespace etl::private_to_arithmetic; - ETL_ASSERT_AND_RETURN_VALUE(is_valid_radix(radix), ETL_ERROR(etl::to_arithmetic_invalid_radix), false); - etl::optional result; - - etl::basic_string_view::const_iterator itr = view.begin(); - const etl::basic_string_view::const_iterator itr_end = view.end(); - - if (itr == itr_end) - { - ETL_ASSERT_FAIL_AND_RETURN_VALUE(ETL_ERROR(etl::to_arithmetic_invalid_format), false); - } - else - { - const char first_char = convert(*itr); - - ETL_ASSERT(!((first_char == valid_character_set::negative_char) && etl::is_unsigned::value), ETL_ERROR(etl::to_arithmetic_signed_to_unsigned)); - - typedef typename intermediate::type intermediate_type; - integral_accumulator accumulator(radix); - - while ((itr != itr_end) && accumulator.add(convert(*itr))) - { - // Keep looping until done or an error occurs. - ++itr; - } - - etl::optional intermediate_result; - - if (accumulator.has_value()) - { - intermediate_result = accumulator.get_value(); - result = static_cast(intermediate_result.value()); - } - else - { - ETL_ASSERT_FAIL_AND_RETURN_VALUE(ETL_ERROR(etl::to_arithmetic_invalid_format), false); - } - } + typedef typename intermediate::type intermediate_type; + etl::optional intermediate_result; + intermediate_result = to_arithmetic_integral(view, radix); + + if (intermediate_result.has_value()) + { + result = intermediate_result.value(); + } + return result; }