Remove pointer only etl::to_arithmetic API

This commit is contained in:
John Wellbelove 2022-10-31 17:04:53 +00:00
parent ae649322c1
commit 85d40b9dda
2 changed files with 33 additions and 45 deletions

View File

@ -164,7 +164,7 @@ namespace etl
}
//*******************************************
///
/// Assignment from a value.
//*******************************************
ETL_CONSTEXPR14
to_arithmetic_result& operator =(value_type value_)
@ -175,7 +175,7 @@ namespace etl
}
//*******************************************
///
/// Assignment from an unexpected_type.
//*******************************************
ETL_CONSTEXPR14
to_arithmetic_result& operator =(unexpected_type status_)
@ -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
@ -326,7 +327,7 @@ namespace etl
}
//***************************************************************************
///
/// Checks to see if the radix is valid.
//***************************************************************************
ETL_NODISCARD
inline
@ -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);
}
//***************************************************************************
@ -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.
//***************************************************************************
@ -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);;
@ -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);;
}

View File

@ -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)
{