From 26d9ff52a0f06d6defce29770758eacbd8b17dca Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Fri, 5 Apr 2024 00:55:53 -0400 Subject: [PATCH] Formatting literals. --- Format/format.h | 74 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 11 deletions(-) diff --git a/Format/format.h b/Format/format.h index 678d03f..6d43acc 100644 --- a/Format/format.h +++ b/Format/format.h @@ -4,33 +4,80 @@ static_assert( __cplusplus > 2020'99 ); #include +#include + #include +#include +#include + +#include 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; +}