113void Prettify( std::string& aSource, FORMAT_MODE aMode )
116 const char quoteChar =
'"';
117 const char indentChar =
'\t';
118 const int indentSize = 1;
122 const int xySpecialCaseColumnLimit = 99;
127 const int consecutiveTokenWrapThreshold = 72;
129 const bool textSpecialCase = aMode == FORMAT_MODE::COMPACT_TEXT_PROPERTIES;
130 const bool libSpecialCase = aMode == FORMAT_MODE::LIBRARY_TABLE;
135 auto cursor = aSource.begin();
140 char lastNonWhitespace = 0;
141 bool inQuote =
false;
142 bool hasInsertedSpace =
false;
143 bool inMultiLineList =
false;
145 bool inShortForm =
false;
146 bool inLibRow =
false;
147 int shortFormDepth = 0;
149 int backslashCount = 0;
151 auto isWhitespace = [](
const char aChar )
153 return ( aChar ==
' ' || aChar ==
'\t' || aChar ==
'\n' || aChar ==
'\r' );
156 auto nextNonWhitespace =
157 [&]( std::string::iterator aIt )
161 while( seek != aSource.end() && isWhitespace( *seek ) )
164 if( seek == aSource.end() )
171 [&]( std::string::iterator aIt )
175 if( ++seek == aSource.end() || *seek !=
'x' )
178 if( ++seek == aSource.end() || *seek !=
'y' )
181 if( ++seek == aSource.end() || *seek !=
' ' )
188 [&]( std::string::iterator aIt )
193 while( ++seek != aSource.end() && isalpha( *seek ) )
196 return token ==
"font" || token ==
"stroke" || token ==
"fill" || token ==
"teardrop"
197 || token ==
"offset" || token ==
"rotate" || token ==
"scale";
201 [&]( std::string::iterator aIt )
206 while( ++seek != aSource.end() && isalpha( *seek ) )
209 return token ==
"lib";
212 while(
cursor != aSource.end() )
217 auto lineEnd = std::find(
cursor, aSource.end(),
'\n' );
222 if( lineEnd == aSource.end() )
225 cursor = std::next( lineEnd );
231 if( isWhitespace( *
cursor ) && !inQuote )
233 if( !hasInsertedSpace
235 && lastNonWhitespace !=
'('
239 if( inXY || column < consecutiveTokenWrapThreshold )
246 else if( inShortForm || inLibRow )
253 std::string( listDepth * indentSize, indentChar ) );
254 column = listDepth * indentSize;
255 inMultiLineList =
true;
258 hasInsertedSpace =
true;
263 hasInsertedSpace =
false;
265 if( *
cursor ==
'(' && !inQuote )
267 bool currentIsXY = isXY(
cursor );
268 bool currentIsShortForm = textSpecialCase && isShortForm(
cursor );
269 bool currentIsLib = libSpecialCase && isLib(
cursor );
276 else if( inXY && currentIsXY && column < xySpecialCaseColumnLimit )
282 else if( inShortForm || inLibRow )
290 std::string( listDepth * indentSize, indentChar ) );
291 column = listDepth * indentSize + 1;
296 if( currentIsShortForm )
299 shortFormDepth = listDepth;
301 else if( currentIsLib )
304 libDepth = listDepth;
309 else if( *
cursor ==
')' && !inQuote )
319 else if( inLibRow && listDepth == libDepth )
324 else if( lastNonWhitespace ==
')' || inMultiLineList )
327 std::string( listDepth * indentSize, indentChar ) );
328 column = listDepth * indentSize + 1;
329 inMultiLineList =
false;
337 if( shortFormDepth == listDepth )
350 else if( *
cursor == quoteChar && ( backslashCount & 1 ) == 0 )
360 lastNonWhitespace = *
cursor;