mirror of
https://github.com/boostorg/charconv.git
synced 2026-02-09 11:02:30 +00:00
Merge pull request #20 from mborland/assert
Remove assertions on pre-conditions
This commit is contained in:
@@ -36,34 +36,4 @@
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_CHARCONV_STANDALONE
|
||||
# include <boost/assert.hpp>
|
||||
# define BOOST_CHARCONV_ASSERT(expr) BOOST_ASSERT(expr)
|
||||
# define BOOST_CHARCONV_ASSERT_MSG(expr, msg) BOOST_ASSERT_MSG(expr, msg)
|
||||
#else // Use plain asserts
|
||||
# include <cassert>
|
||||
# define BOOST_CHARCONV_ASSERT(expr) assert(expr)
|
||||
# define BOOST_CHARCONV_ASSERT_MSG(expr, msg) assert((expr)&&(msg))
|
||||
#endif
|
||||
|
||||
// Use 128 bit integers and supress warnings for using extensions
|
||||
#if defined(BOOST_HAS_INT128)
|
||||
# define BOOST_CHARCONV_HAS_INT128
|
||||
# define BOOST_CHARCONV_INT128_MAX (boost::int128_type)(((boost::uint128_type) 1 << 127) - 1)
|
||||
# define BOOST_CHARCONV_INT128_MIN (-BOOST_CHARCONV_INT128_MAX - 1)
|
||||
# define BOOST_CHARCONV_UINT128_MAX ((2 * (boost::uint128_type) BOOST_CHARCONV_INT128_MAX) + 1)
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_NO_CXX14_CONSTEXPR
|
||||
# define BOOST_CHARCONV_CXX14_CONSTEXPR BOOST_CXX14_CONSTEXPR
|
||||
#else
|
||||
# define BOOST_CHARCONV_CXX14_CONSTEXPR inline
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ == 5
|
||||
# define BOOST_CHARCONV_GCC5_CONSTEXPR inline
|
||||
#else
|
||||
# define BOOST_CHARCONV_GCC5_CONSTEXPR BOOST_CHARCONV_CXX14_CONSTEXPR
|
||||
#endif
|
||||
|
||||
#endif // BOOST_CHARCONV_CONFIG_HPP_INCLUDED
|
||||
|
||||
42
include/boost/charconv/detail/config.hpp
Normal file
42
include/boost/charconv/detail/config.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright 2023 Matt Borland
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#ifndef BOOST_CHARCONV_DETAIL_CONFIG_HPP
|
||||
#define BOOST_CHARCONV_DETAIL_CONFIG_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
// TODO: BOOST_ASSERT is currently unused.
|
||||
// Once library is complete remove this block, and Boost.Assert from the CML if still unused.
|
||||
#ifndef BOOST_CHARCONV_STANDALONE
|
||||
# include <boost/assert.hpp>
|
||||
# define BOOST_CHARCONV_ASSERT(expr) BOOST_ASSERT(expr)
|
||||
# define BOOST_CHARCONV_ASSERT_MSG(expr, msg) BOOST_ASSERT_MSG(expr, msg)
|
||||
#else // Use plain asserts
|
||||
# include <cassert>
|
||||
# define BOOST_CHARCONV_ASSERT(expr) assert(expr)
|
||||
# define BOOST_CHARCONV_ASSERT_MSG(expr, msg) assert((expr)&&(msg))
|
||||
#endif
|
||||
|
||||
// Use 128 bit integers and supress warnings for using extensions
|
||||
#if defined(BOOST_HAS_INT128)
|
||||
# define BOOST_CHARCONV_HAS_INT128
|
||||
# define BOOST_CHARCONV_INT128_MAX (boost::int128_type)(((boost::uint128_type) 1 << 127) - 1)
|
||||
# define BOOST_CHARCONV_INT128_MIN (-BOOST_CHARCONV_INT128_MAX - 1)
|
||||
# define BOOST_CHARCONV_UINT128_MAX ((2 * (boost::uint128_type) BOOST_CHARCONV_INT128_MAX) + 1)
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_NO_CXX14_CONSTEXPR
|
||||
# define BOOST_CHARCONV_CXX14_CONSTEXPR BOOST_CXX14_CONSTEXPR
|
||||
#else
|
||||
# define BOOST_CHARCONV_CXX14_CONSTEXPR inline
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ == 5
|
||||
# define BOOST_CHARCONV_GCC5_CONSTEXPR inline
|
||||
#else
|
||||
# define BOOST_CHARCONV_GCC5_CONSTEXPR BOOST_CHARCONV_CXX14_CONSTEXPR
|
||||
#endif
|
||||
|
||||
#endif // BOOST_CHARCONV_DETAIL_CONFIG_HPP
|
||||
@@ -5,7 +5,7 @@
|
||||
#ifndef BOOST_CHARCONV_DETAIL_CONCATENATE_HPP
|
||||
#define BOOST_CHARCONV_DETAIL_CONCATENATE_HPP
|
||||
|
||||
#include <boost/charconv/config.hpp>
|
||||
#include <boost/charconv/detail/config.hpp>
|
||||
#include <utility>
|
||||
#include <limits>
|
||||
#include <cstdint>
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
// https://stackoverflow.com/questions/1489830/efficient-way-to-determine-number-of-digits-in-an-integer?page=1&tab=scoredesc#tab-top
|
||||
// https://graphics.stanford.edu/~seander/bithacks.html
|
||||
|
||||
#include "../config.hpp"
|
||||
#include <boost/charconv/detail/config.hpp>
|
||||
#include <limits>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#ifndef BOOST_CHARCONV_TOOLS_IS_CONSTANT_EVALUATED_HPP
|
||||
#define BOOST_CHARCONV_TOOLS_IS_CONSTANT_EVALUATED_HPP
|
||||
|
||||
#include <boost/charconv/config.hpp>
|
||||
#include <boost/charconv/detail/config.hpp>
|
||||
#include <type_traits>
|
||||
|
||||
#ifdef __cpp_lib_is_constant_evaluated
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#ifndef BOOST_CHARCONV_DETAIL_MEMCPY_HPP
|
||||
#define BOOST_CHARCONV_DETAIL_MEMCPY_HPP
|
||||
|
||||
#include <boost/charconv/detail/config.hpp>
|
||||
#include <boost/charconv/detail/is_constant_evaluated.hpp>
|
||||
#include <cstring>
|
||||
#include <cstdint>
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#define BOOST_CHARCONV_FROM_CHARS_HPP_INCLUDED
|
||||
|
||||
#include <boost/charconv/detail/apply_sign.hpp>
|
||||
#include <boost/charconv/detail/config.hpp>
|
||||
#include <boost/charconv/config.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <type_traits>
|
||||
@@ -87,8 +88,7 @@ BOOST_CXX14_CONSTEXPR from_chars_result from_chars_integer_impl(const char* firs
|
||||
Unsigned_Integer max_digit = 0;
|
||||
|
||||
// Check pre-conditions
|
||||
BOOST_CHARCONV_ASSERT_MSG(base >= 2 && base <= 36, "Base must be between 2 and 36 (inclusive)");
|
||||
if (!(first <= last))
|
||||
if (!((first <= last) && (base >= 2 && base <= 36)))
|
||||
{
|
||||
return {first, EINVAL};
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <boost/charconv/detail/integer_search_trees.hpp>
|
||||
#include <boost/charconv/detail/integer_conversion.hpp>
|
||||
#include <boost/charconv/detail/memcpy.hpp>
|
||||
#include <boost/charconv/detail/config.hpp>
|
||||
#include <boost/charconv/config.hpp>
|
||||
#include <type_traits>
|
||||
#include <array>
|
||||
@@ -222,10 +223,6 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
BOOST_CHARCONV_ASSERT_MSG(sizeof(Integer) < 1, "Your type is unsupported. Use a built-in integral type");
|
||||
}
|
||||
|
||||
return {first + converted_value_digits, 0};
|
||||
}
|
||||
@@ -322,11 +319,9 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_128integer_impl(char* first, c
|
||||
template <typename Integer, typename Unsigned_Integer>
|
||||
BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char* last, Integer value, int base) noexcept
|
||||
{
|
||||
BOOST_CHARCONV_ASSERT_MSG(base >= 2 && base <= 36, "Base must be between 2 and 36 (inclusive)");
|
||||
|
||||
const std::ptrdiff_t output_length = last - first;
|
||||
|
||||
if (!(first <= last))
|
||||
if (!((first <= last) && (base >= 2 && base <= 36)))
|
||||
{
|
||||
return {last, EINVAL};
|
||||
}
|
||||
|
||||
@@ -124,6 +124,14 @@ void invalid_argument_test()
|
||||
auto r4 = boost::charconv::from_chars(buffer4, buffer4 + std::strlen(buffer4), v4);
|
||||
BOOST_TEST_EQ(r4.ec, EINVAL);
|
||||
}
|
||||
|
||||
// Bases outside 2-36 inclusive return EINVAL
|
||||
const char* buffer5 = "23";
|
||||
T v5 = 0;
|
||||
auto r5 = boost::charconv::from_chars(buffer5, buffer5 + std::strlen(buffer5), v5, 1);
|
||||
BOOST_TEST_EQ(r5.ec, EINVAL);
|
||||
auto r6 = boost::charconv::from_chars(buffer5, buffer5 + std::strlen(buffer5), v5, 50);
|
||||
BOOST_TEST_EQ(r6.ec, EINVAL);
|
||||
}
|
||||
|
||||
// No overflows, negative numbers, locales, etc.
|
||||
|
||||
@@ -252,6 +252,14 @@ void simple_test()
|
||||
BOOST_TEST(r1 != r2);
|
||||
BOOST_TEST_EQ(r2.ec, 0);
|
||||
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_EQ(r3.ec, EINVAL);
|
||||
auto r4 = boost::charconv::to_chars(buffer3, buffer3 + sizeof(buffer3) - 1, v3, 90);
|
||||
BOOST_TEST_EQ(r4.ec, EINVAL);
|
||||
}
|
||||
|
||||
int main()
|
||||
|
||||
Reference in New Issue
Block a user