8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
18 module ALib.Format.Paragraphs;
21 import ALib.Exceptions;
23 import ALib.Camp.Base;
74 const String& pIndentOtherLines ) {
75 String indentOtherLines= pIndentOtherLines.
IsNull() ? indentFirstLine
98 "Paragraphs: PopIndent without prior push." )
103 "Paragraphs: PopIndent without prior push." )
134 #if defined( _WIN32 )
147 integer exceptStart= errPos - 25;
148 if( exceptStart <= 0 ) {
149 exceptPos+= exceptStart;
157 actText.
_( markedBuffer, exceptStart, 50 );
158 if( markedBuffer.
Length() > exceptStart + 50 )
173 searchCharBuf[1]=
'\n';
174 String searchChars(searchCharBuf, 2);
182 integer pos= parser.template IndexOfAny<lang::Inclusion::Include, NC>( searchChars );
191 if( parser.
CharAt( pos ) ==
'\n' ) {
192 parser.template ConsumeChars<NC, lang::CurrentData::Keep>( pos,
Buffer, 1 );
193 if (
Buffer.CharAtEnd<NC>() ==
'\r')
200 lastTextStart=
Buffer.Length();
204 parser.template ConsumeChars<NC, lang::CurrentData::Keep>( pos,
Buffer, 1 );
263 if( lastTextStart <
Buffer.Length() ) {
282 const String& pIndentFirstLine,
283 const String& pIndentOtherLines ) {
286 String indentOtherLines= pIndentOtherLines.
IsNotNull() ? pIndentOtherLines : pIndentFirstLine;
288 bool isFirstLine=
true;
291 bool indentAreJustSpaces=
false;
294 integer maxLineWidthDetectionStartIdx= startIdx;
297 maxLineWidth= (std::max)( maxLineWidth, startIdx - maxLineWidthDetectionStartIdx
299 #
if defined( _WIN32 )
306 if ( startIdx ==
text.Length() )
308 maxLineWidthDetectionStartIdx= startIdx;
312 int isWinNL=
text[ startIdx ] ==
'\r' ? 1 : 0;
313 if (
text[ startIdx + isWinNL ] ==
'\n' ) {
318 indent = isFirstLine ? indentFirstLines : indentOtherLines;
319 indentAreJustSpaces= (indent.template IndexOfAny<lang::Inclusion::Exclude>(
A_CHAR(
" " ) ) < 0 );
323 if ( !indentAreJustSpaces ) {
324 text.InsertAt( indent, startIdx );
325 startIdx+= indent.
Length();
328 #if defined( _WIN32 )
330 text.template InsertChars<NC>(
'\r', 1, startIdx );
335 text.template Delete<NC>(startIdx, 1);
341 startIdx+= 1 + isWinNL;
352 indent = isFirstLine ? indentFirstLines : indentOtherLines;
353 indentAreJustSpaces= (indent.template IndexOfAny<lang::Inclusion::Exclude>(
A_CHAR(
" " ) ) < 0 );
355 text.InsertAt( indent, startIdx );
366 bool isLastLine =
true;
367 bool exceeds =
false;
368 while (++idx <
text.Length() ) {
375 exceeds= lineWidth > 0 && idx - startIdx >= lineWidth;
378 if(idx - startIdx <= lineWidth )
379 lastSpaceInLine= idx;
387 #if defined( _WIN32 )
388 if(
text[idx-1] ==
'\n' &&
text[idx-2] !=
'\r' ) {
389 text.template InsertChars<NC>(
'\r', 1, idx-1 );
393 if(
text[idx-1] ==
'\n' &&
text[idx-2] ==
'\r' ) {
394 text.template Delete<NC>((idx-2), 1);
400 if( exceeds && ( lastSpaceInLine || !isLastLine ) ) {
401 integer wrapPos= lastSpaceInLine > 0 ? lastSpaceInLine : idx;
402 text.template ReplaceSubstring<NC>(
NEW_LINE, wrapPos, 1 );
407 if( justifyChar !=
'\0' ) {
408 integer qtyInserts= lineWidth - (wrapPos - startIdx );
409 if( qtyInserts > 0 ) {
412 while ( leftInsertBoundary < idx &&
text[leftInsertBoundary] ==
' ' )
413 ++leftInsertBoundary;
415 if( leftInsertBoundary < idx ) {
416 while( qtyInserts > 0 ) {
418 bool foundOne=
false;
419 while( qtyInserts > 0 ) {
420 actPos=
text.LastIndexOf(
' ', actPos );
421 if( actPos < leftInsertBoundary )
424 text.InsertChars( justifyChar, 1, actPos );
427 while( --actPos > leftInsertBoundary &&
text[actPos] ==
' ' )
TAString & DeleteEnd(integer regionLength)
ALIB_DLL integer SearchAndReplace(TChar needle, TChar replacement, integer startIdx=0, integer endIdx=strings::MAX_LEN)
TAString & _(const TAppendable &src)
constexpr integer Length() const
bool EndsWith(const TString &needle) const
constexpr bool IsNotNull() const
TChar CharAt(integer idx) const
constexpr bool IsNotEmpty() const
constexpr bool IsNull() const
bool ConsumeString(const TString< TChar > &consumable)
TString< TChar > ConsumeToken(TChar separator=',', lang::Inclusion includeSeparator=lang::Inclusion::Include)
#define ALIB_CALLER_NULLED
#define ALIB_ASSERT_ERROR(cond, domain,...)
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
LocalString< 64 > String64
Type alias name for TLocalString<character,64>.
constexpr CString NEW_LINE
A zero-terminated string containing the new-line character sequence.
constexpr const String EMPTY_STRING
An empty string of the default character type.
lang::integer integer
Type alias in namespace alib.
exceptions::Exception Exception
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
std::deque< T, StdMA< T > > StdDequeMA
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.
lang::uinteger uinteger
Type alias in namespace alib.
strings::TSubstring< character > Substring
Type alias in namespace alib.