Make code a bit more readable

Changed so that int_conv<string_view>() called on a
non-constexpr doesn't compiler anymore (because code
would try to go through a consteval method). It
wouldn't work anyways as the returned value would be
always garbage. If you want a string_view then you
must either give a constexpr input value or you must
call int_conv_raw and call to_string_view() yourself.
This is better than throwing a runtime exception (which
was working ok btw).

All the SafeRetVal stuff is not needed anymore.

Also no need for all that confusing sfinae, I can just
use a bool FromInt and specialise on that.
This commit is contained in:
King_DuckZ 2021-05-29 16:44:06 +02:00
parent a675624cab
commit 938c8fa9a4
2 changed files with 44 additions and 23 deletions

View file

@ -23,6 +23,7 @@
#include <cstdint>
#include <random>
#include <string>
#include <ctime>
template <typename T> using int_info_10 = dhandy::implem::int_info<T, 10>;
template <typename T> using int_info_16 = dhandy::implem::int_info<T, 16>;
@ -142,7 +143,7 @@ TEST_CASE ("Check int to char array conversions", "[s2i][int_conv]") {
{
//Try a random test, which should not compile as constexpr
std::mt19937 gen;
gen.seed(1234);
gen.seed(std::time(nullptr));
for (int z = 0; z < 10; ++z) {
const int num = gen();
CHECK(int_to_ary(num) == std::to_string(num));
@ -199,6 +200,30 @@ TEST_CASE ("Check char array to int conversions", "[i2s][int_conv]") {
AryConversionTestHelperIns<std::int32_t, 16>("aAbBc", 0xaabbc, true);
}
TEST_CASE ("Check string_view conversions work as expected", "[i2s][int_conv]") {
using dhandy::int_conv;
using dhandy::int_conv_raw;
using std::string_view;
using std::integral_constant;
using std::string;
constexpr auto str71 = int_conv<string_view>(integral_constant<short int, 71>{});
CHECK("71" == str71);
{
//test random number to force non-constexpr
std::mt19937 gen;
gen.seed(std::time(nullptr));
const int num = gen();
const string num_str {int_conv_raw(num).to_string_view()};
CHECK(num_str == std::to_string(num));
}
const auto str123 = int_conv<string>(123);
CHECK("123" == str123);
}
TEST_CASE ("Check upcase/downcase int to array conversions", "[i2s][int_conv]") {
using dhandy::int_conv;
using std::string_view;