forked from Alepha/Alepha
92 lines
1.6 KiB
C++
92 lines
1.6 KiB
C++
static_assert( __cplusplus > 2020'99 );
|
|
|
|
#pragma once
|
|
|
|
#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 {}
|
|
|
|
namespace exports::inline literals
|
|
{
|
|
std::string operator ""_format ( const char *p, std::size_t sz );
|
|
}
|
|
|
|
inline std::string
|
|
exports::literals::operator ""_format ( const char *const p, const std::size_t sz )
|
|
{
|
|
const std::string_view s{ p, p + sz };
|
|
if( s.empty() ) return std::string{ s };
|
|
|
|
const auto indent= Alepha::Utility::evaluate <=[&]
|
|
{
|
|
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 ) )
|
|
{
|
|
if( ch == '\n' ) { rv= 0; continue; }
|
|
if( not isspace( ch ) ) break;
|
|
++rv;
|
|
}
|
|
|
|
return rv;
|
|
};
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|