20#include <fmt/format.h>
32 aOut->
Print( aNestLevel,
"(%ls %s)%c", aKey.wc_str(), aValue ?
"yes" :
"no", aSuffix );
65void Prettify( std::string& aSource,
char aQuoteChar )
68 const char indentChar =
'\t';
69 const int indentSize = 1;
73 const int xySpecialCaseColumnLimit = 99;
78 const int consecutiveTokenWrapThreshold = 72;
80 std::string formatted;
81 formatted.reserve( aSource.length() );
83 auto cursor = aSource.begin();
87 char lastNonWhitespace = 0;
89 bool hasInsertedSpace =
false;
90 bool inMultiLineList =
false;
93 int backslashCount = 0;
95 auto isWhitespace = [](
const char aChar )
97 return ( aChar ==
' ' || aChar ==
'\t' || aChar ==
'\n' || aChar ==
'\r' );
100 auto nextNonWhitespace =
101 [&]( std::string::iterator aIt )
105 while( seek != aSource.end() && isWhitespace( *seek ) )
108 if( seek == aSource.end() )
115 [&]( std::string::iterator aIt )
119 if( ++seek == aSource.end() || *seek !=
'x' )
122 if( ++seek == aSource.end() || *seek !=
'y' )
125 if( ++seek == aSource.end() || *seek !=
' ' )
131 while(
cursor != aSource.end() )
135 if( isWhitespace( *
cursor ) && !inQuote )
137 if( !hasInsertedSpace
139 && lastNonWhitespace !=
'('
143 if( inXY || column < consecutiveTokenWrapThreshold )
147 formatted.push_back(
' ' );
152 formatted += fmt::format(
"\n{}",
153 std::string( listDepth * indentSize, indentChar ) );
154 column = listDepth * indentSize;
155 inMultiLineList =
true;
158 hasInsertedSpace =
true;
163 hasInsertedSpace =
false;
165 if( *
cursor ==
'(' && !inQuote )
167 bool currentIsXY = isXY(
cursor );
171 formatted.push_back(
'(' );
174 else if( inXY && currentIsXY && column < xySpecialCaseColumnLimit )
183 formatted += fmt::format(
"\n{}(",
184 std::string( listDepth * indentSize, indentChar ) );
185 column = listDepth * indentSize + 1;
191 else if( *
cursor ==
')' && !inQuote )
196 if( lastNonWhitespace ==
')' || inMultiLineList )
198 formatted += fmt::format(
"\n{})",
199 std::string( listDepth * indentSize, indentChar ) );
200 column = listDepth * indentSize + 1;
201 inMultiLineList =
false;
205 formatted.push_back(
')' );
216 else if( *
cursor == aQuoteChar && ( backslashCount & 1 ) == 0 )
222 formatted.push_back( *
cursor );
226 lastNonWhitespace = *
cursor;
235 aSource = std::move( formatted );
wxString AsString() const
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.