Skip to content

charconv2 #195

@gpeterhoff

Description

@gpeterhoff

Hello Matt,
I would like to provide an extended library boost::charconv2. This implements:

  • FP types: an additional parameter nonfinite_style which means whether to distinguish between quiet_NaN and signaling_NaN. The default is no distinction - which ensures compatibility with std::from/to_chars. Unfortunately, your boost::charchonv does not behave according to the standard, which is why this measure is necessary.
  • additional overloads for from_chars: it is highly stupid that there are 2 different implementations for string_view in boost (boost::core::string_view and boost::string_view).
  • from/to_chars_binary

Implementation
I need a few more things:

charconv2.hpp.txt

Examples

namespace example
{
namespace C2 = boost::charconv2;
using array_type = std::array<char, 256>;

template <typename Array, typename Result>
void do_println(const Array& arr, const Result& res)
{
	if (res.ec == std::errc{})
	{
		std::cout << std::string_view{arr.data(), size_t(res.ptr-arr.data())};
	}
	else
	{
		std::cout << std::make_error_code(res.ec).message();
	}
	std::cout << std::endl;
}

template <typename Type>
typename std::enable_if<boost::is_integer<Type>::value, void>::type	println(const Type value, const int base = 10)
{
	array_type
		arr{};
	const C2::to_chars_result
		res = C2::to_chars(arr.data(), arr.data()+arr.size(), value, base);
	do_println(arr, res);
}

template <typename Type>
typename std::enable_if<boost::is_floating_point<Type>::value, void>::type	println(const Type value, const C2::chars_format format, const int precision, const C2::nonfinite_style style = C2::nonfinite_style::standard)
{
	array_type
		arr{};
	const C2::to_chars_result
		res = C2::to_chars(arr.data(), arr.data()+arr.size(), value, format, precision, style);
	do_println(arr, res);
}

template <typename Type>
typename std::enable_if<boost::is_floating_point<Type>::value, void>::type	println(const Type value, const C2::chars_format format, const C2::nonfinite_style style = C2::nonfinite_style::standard)
{
	array_type
		arr{};
	const C2::to_chars_result
		res = C2::to_chars(arr.data(), arr.data()+arr.size(), value, format, style);
	do_println(arr, res);
}


template <typename Type>
typename std::enable_if<boost::is_number<Type>::value, void>::type	println_binary(const Type value, const C2::binary_style style = C2::binary_style::standard)
{
	array_type
		arr{};
	const C2::to_chars_result
		res = C2::to_chars_binary(arr.data(), arr.data()+arr.size(), value, style);
	do_println(arr, res);
}

}	//	example

int main()
{
	namespace C2 = boost::charconv2;
	using F = boost::float64_t;
	using I = int;

	I i = 42;
	example::println(i);
	example::println_binary(i);
	example::println_binary(i, C2::binary_style::showpoint);
	std::cout << std::endl;

	F f = 42;
	example::println(f, C2::chars_format::general);
	example::println(f, C2::chars_format::general, C2::nonfinite_style::explicite_nan);
	example::println_binary(f);
	example::println_binary(f, C2::binary_style::showpoint);
	std::cout << std::endl;

	f = std::numeric_limits<F>::signaling_NaN();
	example::println(f, C2::chars_format::general);
	example::println(f, C2::chars_format::general, C2::nonfinite_style::explicite_nan);
	example::println_binary(f);
	example::println_binary(f, C2::binary_style::showpoint);
	std::cout << std::endl;

	return EXIT_SUCCESS;
}

Results

42
00000000000000000000000000101010
00000000.00000000.00000000.00101010

42
42
0100000001000101000000000000000000000000000000000000000000000000
0.10000000100.0101000000000000000000000000000000000000000000000000

nan
snan
0111111111110100000000000000000000000000000000000000000000000000
0.11111111111.0100000000000000000000000000000000000000000000000000

What is your opinion on this? I would be delighted to receive your comments.

regards
Gero

Edit
I just realized that this will not work for you. This is because I have rewritten boost::math::cstdfloat -> into a separate library boost::cstdfloat. Why?

  • boost::math::cstdfloat contains many errors
  • boost::math::cstdfloat is implemented in a very complicated and outdated style; nobody can understand it anymore -> unmaintainable

Here for example my types.hpp vs. boost::math::cstdfloat::cstdfloat_types.hpp types.hpp.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions