Add a constructor from basic_string_view in C++17 or later

Note that we have MrDocs compile as C++17 again (a change that we had
reverted).
This commit is contained in:
Gennaro Prota
2025-09-11 16:49:26 +02:00
parent 0177f4adfa
commit bb489c186a
5 changed files with 51 additions and 5 deletions

View File

@@ -82,6 +82,9 @@ if (DYNAMIC_BITSET_MRDOCS_BUILD)
# Create a custom target for MrDocs.
add_library(dynamic_bitset_mrdocs_target ${TEMP_CPP_FILE})
# This is to get the documentation of the constructor from basic_string_view.
set_target_properties(dynamic_bitset_mrdocs_target PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED YES)
# Set any other target properties here.
target_include_directories(dynamic_bitset_mrdocs_target PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(dynamic_bitset_mrdocs_target PRIVATE boost_dynamic_bitset)

View File

@@ -60,4 +60,8 @@ make_non_const( T t )
# define BOOST_DYNAMIC_BITSET_SPECIALIZE_STD_HASH
#endif
#if ( defined( _MSVC_LANG ) && _MSVC_LANG >= 201703L ) || __cplusplus >= 201703L
# define BOOST_DYNAMIC_BITSET_USE_CPP17_OR_LATER
#endif
#endif // include guard

View File

@@ -386,6 +386,27 @@ public:
template< typename CharT >
dynamic_bitset( const CharT * s, std::size_t n = std::size_t( -1 ), size_type num_bits = npos, const allocator_type & alloc = allocator_type() );
#if defined( BOOST_DYNAMIC_BITSET_USE_CPP17_OR_LATER )
//! Similar to the constructor from a pointer to a C-style
//! string, but takes a `std::basic_string_view`. This
//! constructor is only available if DynamicBitset is compiled
//! as C++17 or later.
//!
//! \pre
//! The characters in `sv` are '0' or '1'.
//!
//! \param sv The basic_string_view to construct from.
//! \param num_bits The size of the bitset to construct, if
//! different from `npos`. (Otherwise the size of the bitset is
//! `sv.length()`.)
//! \param alloc The allocator to use.
// -----------------------------------------------------------------------
template< typename CharT, typename Traits >
dynamic_bitset( std::basic_string_view< CharT, Traits > sv, size_type num_bits = npos, const allocator_type & alloc = allocator_type() );
#endif
//! Constructs a bitset from a range of blocks or from an
//! integer.
//!
@@ -1308,7 +1329,7 @@ private:
void init_from_block_range( BlockIter first, BlockIter last );
template< typename CharT, typename Traits = std::char_traits< CharT > >
void init_from_string( const CharT * s, std::size_t pos, std::size_t n, size_type num_bits );
void init_from_string( const CharT * s, std::size_t string_length, std::size_t pos, std::size_t n, size_type num_bits );
void init_from_unsigned_long( size_type num_bits, unsigned long value /*,
const allocator_type& alloc*/

View File

@@ -494,7 +494,7 @@ dynamic_bitset< Block, AllocatorOrContainer >::dynamic_bitset(
: m_bits( alloc ), m_num_bits( 0 )
{
init_from_string( s.c_str(), pos, n, num_bits );
init_from_string( s.c_str(), s.length(), pos, n, num_bits );
}
template< typename Block, typename AllocatorOrContainer >
@@ -506,9 +506,23 @@ dynamic_bitset< Block, AllocatorOrContainer >::dynamic_bitset(
const allocator_type & alloc )
: m_bits( alloc ), m_num_bits( 0 )
{
init_from_string( s, 0, n, num_bits );
init_from_string( s, std::char_traits< CharT >::length( s ), 0, n, num_bits );
}
#if defined( BOOST_DYNAMIC_BITSET_USE_CPP17_OR_LATER )
template< typename Block, typename AllocatorOrContainer >
template< typename CharT, typename Traits >
dynamic_bitset< Block, AllocatorOrContainer >::dynamic_bitset(
std::basic_string_view< CharT, Traits > sv,
size_type num_bits,
const allocator_type & alloc )
: m_bits( alloc ), m_num_bits( 0 )
{
init_from_string( sv.data(), sv.length(), 0, sv.length(), num_bits );
}
#endif
template< typename Block, typename AllocatorOrContainer >
template< typename BlockInputIterator >
@@ -2061,12 +2075,12 @@ template< typename Block, typename AllocatorOrContainer >
template< typename CharT, typename Traits >
void
dynamic_bitset< Block, AllocatorOrContainer >::init_from_string(
const CharT * s,
const CharT * s, // caution: not necessarily null-terminated
std::size_t string_length,
std::size_t pos,
std::size_t n,
size_type num_bits )
{
const std::size_t string_length = Traits::length( s );
BOOST_ASSERT( pos <= string_length );

View File

@@ -208,6 +208,10 @@ struct bitset_test
// If M < N, remaining bit positions are zero
for ( ; j < actual_size; ++j )
BOOST_TEST( b[ j ] == 0 );
#if defined( BOOST_DYNAMIC_BITSET_USE_CPP17_OR_LATER )
BOOST_TEST( Bitset( std::basic_string_view< Ch, Tr >( str ).substr( pos, rlen ), num_bits ) == b );
#endif
}
static void