Skip to content

Commit

Permalink
Merge pull request #97 from cppalliance/develop
Browse files Browse the repository at this point in the history
Update packages on master
  • Loading branch information
mborland authored Oct 30, 2023
2 parents 0005843 + 5aa9f57 commit 99b6e54
Showing 8 changed files with 98 additions and 3 deletions.
2 changes: 1 addition & 1 deletion conan/conanfile.py
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@

class CharconvConan(ConanFile):
name = "boost_charconv"
version = "1.0.0"
version = "1.0.1"
description = "Boost provides free peer-reviewed portable C++ source libraries"
url = "https://github.com/cppalliance/charconv"
homepage = "https://github.com/cppalliance/charconv"
8 changes: 8 additions & 0 deletions doc/charconv/from_chars.adoc
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ struct from_chars_result
friend constexpr bool operator==(const from_chars_result& lhs, const from_chars_result& rhs) noexcept
friend constexpr bool operator!=(const from_chars_result& lhs, const from_chars_result& rhs) noexcept
constexpr explicit operator bool() const noexcept { return ec == std::errc{}; }
}
template <typename Integral>
@@ -70,6 +71,7 @@ const char* buffer = "42";
int v = 0;
from_chars_result r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(v == 42);
----
==== Floating Point
@@ -79,6 +81,7 @@ const char* buffer = "1.2345"
double v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(v == 1.2345);
----

@@ -90,6 +93,7 @@ const char* buffer = "2a";
unsigned v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v, 16);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(v == 42);
----
==== Floating Point
@@ -99,6 +103,7 @@ const char* buffer = "1.3a2bp-10";
double v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v, boost::charconv::chars_format::hex);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(v == 8.0427e-18);
----

@@ -109,13 +114,15 @@ const char* buffer = "-123";
unsigned v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v);
assert(r.ec == std::errc::invalid_argument);
assert(!r); // Same as above but less verbose. Added in C++26.
----
[source, c++]
----
const char* buffer = "-1.573e-3";
double v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v, boost::charconv::chars_format::fixed);
assert(r.ec == std::errc::invalid_argument);
assert(!r); // Same as above but less verbose. Added in C++26.
----
Note: In the event of std::errc::invalid_argument, v is not modified by `from_chars`

@@ -126,6 +133,7 @@ const char* buffer = "1234";
unsigned char v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v);
assert(r.ec == std::errc::result_out_of_range);
assert(!r); // Same as above but less verbose. Added in C++26.
assert(v == 0)
----
Note: In the event of std::errc::result_out_of_range, v is not modified by `from_chars`
5 changes: 5 additions & 0 deletions doc/charconv/to_chars.adoc
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ struct to_chars_result
friend constexpr bool operator==(const to_chars_result& lhs, const to_chars_result& rhs) noexcept;
friend constexpr bool operator!=(const to_chars_result& lhs, const to_chars_result& rhs) noexcept;
constexpr explicit operator bool() const noexcept { return ec == std::errc{}; }
};
template <typename Integral>
@@ -86,6 +87,7 @@ char buffer[64] {};
double v = 1e300;
to_chars_result r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer) - 1, v);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(!strcmp(buffer, "1e+300"));
----

@@ -97,6 +99,7 @@ char buffer[64] {};
int v = 42;
to_chars_result r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer) - 1, v, 16);
assert(r.ec == std::errc());
assert(r); // Same as above but less verbose. Added in C++26.
assert(!strcmp(buffer, "2a")); // strcmp returns 0 on match
----
==== Floating Point
@@ -128,6 +131,7 @@ char buffer[3] {};
int v = -1234;
to_chars_result r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer) - 1, v, 16);
assert(r.ec == std::errc::result_out_of_range);
assert(!r); // Same as above but less verbose. Added in C++26.
----
==== Floating Point
[source, c++]
@@ -136,6 +140,7 @@ char buffer[3] {};
double v = 1.2345;
auto r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer) - 1, v);
assert(r.ec == std::errc::result_out_of_range);
assert(!r); // Same as above but less verbose. Added in C++26.
----

In the event of std::errc::result_out_of_range, to_chars_result.ptr is equal to first
2 changes: 2 additions & 0 deletions include/boost/charconv/detail/from_chars_result.hpp
Original file line number Diff line number Diff line change
@@ -31,6 +31,8 @@ struct from_chars_result_t
{
return !(lhs == rhs); // NOLINT : Expression can not be simplified since this is the definition
}

constexpr explicit operator bool() const noexcept { return ec == std::errc{}; }
};
using from_chars_result = from_chars_result_t<char>;

2 changes: 2 additions & 0 deletions include/boost/charconv/detail/to_chars_result.hpp
Original file line number Diff line number Diff line change
@@ -25,6 +25,8 @@ struct to_chars_result
{
return !(lhs == rhs);
}

constexpr explicit operator bool() const noexcept { return ec == std::errc{}; }
};

}} // Namespaces
4 changes: 2 additions & 2 deletions ports/charconv/portfile.cmake
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO cppalliance/charconv
REF v1.0.0
SHA512 ced9e5ca45df285709e19ac0a142b58447bcff91d5a14ffcc3b7d6686120dca631497aa145508fd1f2c4ffea4d7b6bf50397fb86f3eafec7ab281014aee8f6b5
REF v1.0.1
SHA512 b1d0accec1a4251f3107061c80e5c72a76aa4de0d5fdc085c934a3da8b55a5e72bb3e6a3a1b51d4e7debe6917a3c64be731d56eb8e7cacb890434a154f140726
HEAD_REF master
)

1 change: 1 addition & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
@@ -53,3 +53,4 @@ run to_chars_float_STL_comp.cpp : : : [ requires cxx17_hdr_charconv ] ;
run from_chars_float2.cpp ;
run-fail STL_benchmark.cpp : : : [ requires cxx17_hdr_charconv ] [ check-target-builds ../config//has_double_conversion "Google double-coversion support" : <library>"double-conversion" ] ;
run test_float128.cpp : : : [ check-target-builds ../config//has_float128 "GCC libquadmath and __float128 support" : <library>"quadmath" ] ;
run P2497.cpp ;
77 changes: 77 additions & 0 deletions test/P2497.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2023 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/charconv.hpp>
#include <boost/core/lightweight_test.hpp>
#include <system_error>
#include <type_traits>
#include <limits>
#include <cstring>
#include <cstdint>
#include <cerrno>
#include <utility>

// No overflows, negative numbers, locales, etc.
template <typename T>
void simple_from_chars_test()
{
const char* buffer = "34";

T v = 0;
auto r = boost::charconv::from_chars(buffer, buffer + std::strlen(buffer), v);

BOOST_TEST( r ) && BOOST_TEST_EQ(v, static_cast<T>(34));
BOOST_TEST(r == r);

boost::charconv::from_chars_result r2 {r.ptr, std::errc()};
BOOST_TEST(r == r2);

const char* buffer2 = "12";
T v2 = 0;
auto r3 = boost::charconv::from_chars(buffer2, buffer2 + std::strlen(buffer), v2);
BOOST_TEST(r != r3);
BOOST_TEST(r3) && BOOST_TEST_EQ(v2, static_cast<T>(12));
}

template <typename T>
void simple_to_chars_test()
{
char buffer1[64] {};
T v = 34;
auto r1 = boost::charconv::to_chars(buffer1, buffer1 + sizeof(buffer1) - 1, v);
BOOST_TEST(r1);
BOOST_TEST_CSTR_EQ(buffer1, "34");

boost::charconv::to_chars_result r {r1.ptr, r1.ec};
BOOST_TEST(r1 == r);

char buffer2[64] {};
T v2 = 12;
auto r2 = boost::charconv::to_chars(buffer2, buffer2 + sizeof(buffer2) - 1, v2);
BOOST_TEST(r1 != r2);
BOOST_TEST(r2);
BOOST_TEST_CSTR_EQ(buffer2, "12");

// If the base is not 2-36 inclusive return invalid value
char buffer3[64] {};
T v3 = 12;
auto r3 = boost::charconv::to_chars(buffer3, buffer3 + sizeof(buffer3) - 1, v3, -2);
BOOST_TEST(r3.ec == std::errc::invalid_argument);
BOOST_TEST(!r3);
auto r4 = boost::charconv::to_chars(buffer3, buffer3 + sizeof(buffer3) - 1, v3, 90);
BOOST_TEST(r4.ec == std::errc::invalid_argument);
BOOST_TEST(!r4);
}

int main()
{
simple_from_chars_test<std::int32_t>();
simple_from_chars_test<std::uint64_t>();

simple_to_chars_test<std::int32_t>();
simple_to_chars_test<std::uint64_t>();

return boost::report_errors();
}

0 comments on commit 99b6e54

Please sign in to comment.