forked from Alepha/Alepha
Formatting literals.
This commit is contained in:
@ -4,33 +4,80 @@ static_assert( __cplusplus > 2020'99 );
|
||||
|
||||
#include <Alepha/Alepha.h>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <sstream>
|
||||
|
||||
#include <Alepha/Utility/evaluation_helpers.h>
|
||||
|
||||
namespace Alepha::Hydrogen::Format ::detail:: format_m
|
||||
{
|
||||
inline namespace exports
|
||||
inline namespace exports {}
|
||||
|
||||
namespace exports::inline literals
|
||||
{
|
||||
std::string operator ""_format ( const char *p, std::size_t sz );
|
||||
}
|
||||
|
||||
inline std::string
|
||||
exports::operator ""_format ( const char *const p, const std::size_t sz )
|
||||
exports::literals::operator ""_format ( const char *const p, const std::size_t sz )
|
||||
{
|
||||
std::string s{ p, p + s };
|
||||
const std::string_view s{ p, p + sz };
|
||||
if( s.empty() ) return std::string{ s };
|
||||
|
||||
if( s.empty() ) return s;
|
||||
std::size_t indent= 0;
|
||||
if( s.at( 0 ) == '\n' )
|
||||
const auto indent= Alepha::Utility::evaluate <=[&]
|
||||
{
|
||||
s= s.substr( 1 );
|
||||
while( s.at( 0 ) == ' ' )
|
||||
if( s.at( 0 ) != '\n' ) return 0uz;
|
||||
const auto view= s.substr( 1 );
|
||||
std::size_t rv= 0;
|
||||
for( const char ch: s.substr( 1 ) )
|
||||
{
|
||||
++indent;
|
||||
s= s.substr( 1 );
|
||||
if( ch == '\n' ) { rv= 0; continue; }
|
||||
if( not isspace( ch ) ) break;
|
||||
++rv;
|
||||
}
|
||||
|
||||
return rv;
|
||||
};
|
||||
std::cerr << "See indent at " << indent << std::endl;
|
||||
|
||||
std::istringstream iss{ std::string{ s } };
|
||||
if( indent ) iss.get();
|
||||
std::ostringstream oss;
|
||||
|
||||
std::size_t count= 0;
|
||||
bool skipping= indent;
|
||||
while( iss )
|
||||
{
|
||||
const char ch= iss.get();
|
||||
|
||||
if( skipping )
|
||||
{
|
||||
if( not isspace( ch ) )
|
||||
{
|
||||
oss << ch;
|
||||
skipping= false;
|
||||
}
|
||||
else if( ++count == indent )
|
||||
{
|
||||
skipping= false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
oss.put( ch );
|
||||
|
||||
if( ch == '\n' )
|
||||
{
|
||||
skipping= true;
|
||||
count= 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return std::move( oss ).str();
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,3 +85,8 @@ namespace Alepha::Hydrogen::Format::inline exports::inline format_m
|
||||
{
|
||||
using namespace detail::format_m::exports;
|
||||
}
|
||||
|
||||
namespace Alepha::Hydrogen::Format::inline exports::inline literals::inline format_literals
|
||||
{
|
||||
using namespace format_m::literals;
|
||||
}
|
||||
|
Reference in New Issue
Block a user