1
0
forked from Alepha/Alepha

Make it possible to use arbitrary code in making delimited lists.

This commit is contained in:
2024-04-04 15:41:27 -04:00
parent 58327d5afd
commit a6467772f4
3 changed files with 48 additions and 9 deletions

View File

@ -28,23 +28,23 @@ namespace Alepha::Hydrogen ::detail:: delimited_list_m
: virtual public IOStreams::StackableStreambuf, virtual public std::streambuf : virtual public IOStreams::StackableStreambuf, virtual public std::streambuf
{ {
private: private:
std::string delim; DelimiterWriter writer;
bool first= true; bool first= true;
public: public:
explicit explicit
DelimitedListStreambuf( std::ios &is, const std::string delim ) DelimitedListStreambuf( std::ios &is, const DelimiterWriter writer )
: StackableStreambuf( is ), delim( delim ) : StackableStreambuf( is ), writer( writer )
{ {
tracker.get( is )= this; tracker.get( is )= this;
if( C::debug ) error() << "Streambuf adapted." << std::endl; if( C::debug ) error() << "Streambuf adapted." << std::endl;
} }
void void
doNext() doNext( std::ostream &out )
{ {
if( C::debug ) error() << "Do Next called" << std::endl; if( C::debug ) error() << "Do Next called" << std::endl;
if( not first ) out() << delim; if( not first ) writer( out );
first= false; first= false;
} }
@ -66,7 +66,7 @@ namespace Alepha::Hydrogen ::detail:: delimited_list_m
{ {
if( C::debug ) error() << "Marker called" << std::endl; if( C::debug ) error() << "Marker called" << std::endl;
auto thisTracker= tracker.get( os ); auto thisTracker= tracker.get( os );
if( thisTracker != nullptr ) thisTracker->doNext(); if( thisTracker != nullptr ) thisTracker->doNext( os );
return os; return os;
} }
@ -74,6 +74,6 @@ namespace Alepha::Hydrogen ::detail:: delimited_list_m
void void
impl::build_streambuf( std::ostream &os, StartDelimitedList &&params ) impl::build_streambuf( std::ostream &os, StartDelimitedList &&params )
{ {
new impl_cc::DelimitedListStreambuf( os, params.delimiter ); new impl_cc::DelimitedListStreambuf( os, params.writer );
} }
} }

View File

@ -12,11 +12,20 @@ namespace Alepha::Hydrogen ::detail:: delimited_list_m
{ {
inline namespace exports {} inline namespace exports {}
using DelimiterWriter= std::function< void ( std::ostream & ) >;
struct DelimitedList_params struct DelimitedList_params
{ {
const std::string delimiter; DelimiterWriter writer;
explicit DelimitedList_params( const std::string delimiter ) : delimiter( delimiter ) {} explicit
DelimitedList_params( const std::string delimiter )
: writer( [delimiter] ( std::ostream &os ) { os << delimiter; } )
{}
explicit
DelimitedList_params( DelimiterWriter writer )
: writer( writer )
{}
}; };
struct MarkItem_t {}; struct MarkItem_t {};

View File

@ -7,6 +7,8 @@ static_assert( __cplusplus > 2020'99 );
#include <Alepha/Utility/evaluation_helpers.h> #include <Alepha/Utility/evaluation_helpers.h>
#include <Alepha/IOStreams/delimiters.h>
static auto init= Alepha::Utility::enroll <=[] static auto init= Alepha::Utility::enroll <=[]
{ {
using namespace Alepha::Testing::literals; using namespace Alepha::Testing::literals;
@ -32,4 +34,32 @@ static auto init= Alepha::Utility::enroll <=[]
{ {
{ "Simple", { { "Alpha", "Bravo", "Charlie", "Delta", "Echo" } }, "Alpha, Bravo, Charlie, Delta, Echo" }, { "Simple", { { "Alpha", "Bravo", "Charlie", "Delta", "Echo" } }, "Alpha, Bravo, Charlie, Delta, Echo" },
}; };
"Simple Comma Testing using delim function"_test <=TableTest
<
[]( std::vector< std::string > names )
{
std::ostringstream oss;
oss << setDelimiter( Alepha::IOStreams::fieldDelimiter, ", " );
oss << Alepha::StartDelimitedList
{
[]( std::ostream &os )
{
os << Alepha::IOStreams::fieldDelimiter;
}
};
for( const auto &name: names )
{
oss << Alepha::NextItem << name;
}
return oss.str();
}
>
::Cases
{
{ "Simple", { { "Alpha", "Bravo", "Charlie", "Delta", "Echo" } }, "Alpha, Bravo, Charlie, Delta, Echo" },
};
}; };