Skip to content

Commit

Permalink
Remove pointer only etl::to_arithmetic API
Browse files Browse the repository at this point in the history
  • Loading branch information
jwellbelove committed Oct 31, 2022
1 parent ae64932 commit 85d40b9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 45 deletions.
55 changes: 10 additions & 45 deletions include/etl/to_arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ namespace etl
}

//*******************************************
///
/// Assignment from a value.
//*******************************************
ETL_CONSTEXPR14
to_arithmetic_result& operator =(value_type value_)
Expand All @@ -175,7 +175,7 @@ namespace etl
}

//*******************************************
///
/// Assignment from an unexpected_type.
//*******************************************
ETL_CONSTEXPR14
to_arithmetic_result& operator =(unexpected_type status_)
Expand Down Expand Up @@ -300,7 +300,8 @@ namespace etl
}

//***************************************************************************
///
/// Checks to see if the text starts with a '+' or '-' prefix, and modifies the view to remove it.
/// Returns true if the text has a '-' prefix.
//***************************************************************************
template <typename TChar>
ETL_NODISCARD
Expand All @@ -326,7 +327,7 @@ namespace etl
}

//***************************************************************************
///
/// Checks to see if the radix is valid.
//***************************************************************************
ETL_NODISCARD
inline
Expand Down Expand Up @@ -775,9 +776,9 @@ namespace etl
ETL_NODISCARD
ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<TValue>::value, etl::to_arithmetic_result<TValue> >::type
to_arithmetic(const etl::basic_string_view<TChar>& view, const etl::basic_format_spec<etl::ibasic_string<TChar> >& spec)
to_arithmetic(const etl::basic_string_view<TChar>& view, const typename etl::private_basic_format_spec::base_spec& spec)
{
return etl::to_arithmetic<TValue, TChar>(view, spec.get_base());
return etl::to_arithmetic<TValue, TChar>(view, spec.base);
}

//***************************************************************************
Expand Down Expand Up @@ -816,42 +817,6 @@ namespace etl
return etl::to_arithmetic<TValue, TChar>(etl::basic_string_view<TChar>(cp, length), spec.base);
}

//***************************************************************************
/// Text to integral from pointer and radix value type.
//***************************************************************************
template <typename TValue, typename TChar>
ETL_NODISCARD
ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<TValue>::value, etl::to_arithmetic_result<TValue> >::type
to_arithmetic(const TChar* cp, const etl::radix::value_type radix)
{
return etl::to_arithmetic<TValue, TChar>(etl::basic_string_view<TChar>(cp, etl::strlen<TChar>(cp), radix));
}

//***************************************************************************
/// Text to integral from pointer and default decimal radix.
//***************************************************************************
template <typename TValue, typename TChar>
ETL_NODISCARD
ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<TValue>::value, etl::to_arithmetic_result<TValue> >::type
to_arithmetic(const TChar* cp)
{
return etl::to_arithmetic<TValue, TChar>(etl::basic_string_view<TChar>(cp, etl::strlen<TChar>(cp)), etl::radix::decimal);;
}

//***************************************************************************
/// Text to integral from pointer and radix format spec.
//***************************************************************************
template <typename TValue, typename TChar>
ETL_NODISCARD
ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<TValue>::value, etl::to_arithmetic_result<TValue> >::type
to_arithmetic(const TChar* cp, const typename etl::private_basic_format_spec::base_spec& spec)
{
return etl::to_arithmetic<TValue, TChar>(etl::basic_string_view<TChar>(cp, etl::strlen<TChar>(cp)), spec.base);;
}

//***************************************************************************
/// Text to integral from string and radix value type.
//***************************************************************************
Expand All @@ -869,8 +834,8 @@ namespace etl
//***************************************************************************
template <typename TValue, typename TChar>
ETL_NODISCARD
ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<TValue>::value, etl::to_arithmetic_result<TValue> >::type
ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<TValue>::value, etl::to_arithmetic_result<TValue> >::type
to_arithmetic(const etl::ibasic_string<TChar>& str)
{
return etl::to_arithmetic<TValue, TChar>(etl::basic_string_view<TChar>(str), etl::radix::decimal);;
Expand All @@ -883,7 +848,7 @@ namespace etl
ETL_NODISCARD
ETL_CONSTEXPR14
typename etl::enable_if<etl::is_integral<TValue>::value, etl::to_arithmetic_result<TValue> >::type
to_arithmetic(const etl::ibasic_string<TChar>& str, const etl::basic_format_spec<etl::ibasic_string<TChar> >& spec)
to_arithmetic(const etl::ibasic_string<TChar>& str, const typename etl::private_basic_format_spec::base_spec& spec)
{
return etl::to_arithmetic<TValue, TChar>(etl::basic_string_view<TChar>(str), spec);;
}
Expand Down
23 changes: 23 additions & 0 deletions test/test_to_arithmetic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,29 @@ namespace
CHECK_EQUAL(etl::to_arithmetic_status::Overflow, etl::to_arithmetic<long double>(text.c_str(), text.size()).error());
}

//*************************************************************************
TEST(test_all_api_variants)
{
const Text text(STR("83"));

using ETLText = etl::string<2>;

// Default radix
CHECK_EQUAL(int(83), int(etl::to_arithmetic<int8_t>(etl::string_view(text.c_str(), text.size())).value()));
CHECK_EQUAL(int(83), int(etl::to_arithmetic<int8_t>(text.c_str(), text.size()).value()));
CHECK_EQUAL(int(83), int(etl::to_arithmetic<int8_t>(ETLText(text.c_str(), text.size())).value()));

// Format spec radix
CHECK_EQUAL(int(83), int(etl::to_arithmetic<int8_t>(etl::string_view(text.c_str(), text.size()), etl::dec).value()));
CHECK_EQUAL(int(83), int(etl::to_arithmetic<int8_t>(text.c_str(), text.size(), etl::dec).value()));
CHECK_EQUAL(int(83), int(etl::to_arithmetic<int8_t>(ETLText(text.c_str(), text.size()), etl::dec).value()));

// Numeric radix
CHECK_EQUAL(int(83), int(etl::to_arithmetic<int8_t>(etl::string_view(text.c_str(), text.size()), etl::radix::decimal).value()));
CHECK_EQUAL(int(83), int(etl::to_arithmetic<int8_t>(text.c_str(), text.size(), etl::radix::decimal).value()));
CHECK_EQUAL(int(83), int(etl::to_arithmetic<int8_t>(ETLText(text.c_str(), text.size()), etl::radix::decimal).value()));
}

//*************************************************************************
TEST(test_constexpr_integral)
{
Expand Down

0 comments on commit 85d40b9

Please sign in to comment.