Skip to content

Commit 204465f

Browse files
committed
Adapt StrWrap to support different 1st line offset.
Add new, optional, FirstLineOffset parameter to one of the StrWrap overloads that offsets first line relative to margin used for remaining lines. Leaving out the parameter reverts to old behaviour of adding same margin for each line.
1 parent 6728e64 commit 204465f

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

Src/UStrUtils.pas

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,19 @@ function StrSplit(const Str: UnicodeString; const Delim: UnicodeString;
226226

227227
/// <summary>Word wraps text Str to form lines of maximum length MaxLen and
228228
/// offsets each line using spaces to form a left margin of size given by
229-
/// Margin.</summary>
230-
/// <remarks>Output lines are separated by CRLF.</remarks>
231-
function StrWrap(const Str: UnicodeString; const MaxLen, Margin: UInt16):
232-
UnicodeString; overload;
229+
/// Margin. The first line is offset from the margin by FirstLineOffset spaces.
230+
/// </summary>
231+
/// <remarks>
232+
/// <para>FirstLineOffset offsets to the left of Margin if -ve and to the right
233+
/// of Margin if +ve.</para>
234+
/// <para>If FirstLineOffset is -ve then Abs(FirstLineOffset) must be less than
235+
/// or equal to Margin.</para>
236+
/// <para>If FirstLineOffset is +ve then FirstLineOffset + Margin must fit in
237+
/// a UInt16.</para>
238+
/// <para>Output lines are separated by CRLF.</para>
239+
/// </remarks>
240+
function StrWrap(const Str: UnicodeString; const MaxLen, Margin: UInt16;
241+
const FirstLineOffset: Int16 = 0): UnicodeString; overload;
233242

234243
/// <summary>Word wraps each paragraph of text in Paras so that each line of a
235244
/// paragraph has lines of maximum length MaxLineLen and is offset by the
@@ -773,8 +782,8 @@ function StrWindowsLineBreaks(const Str: UnicodeString): UnicodeString;
773782
Result := StrReplace(Result, LF, CRLF);
774783
end;
775784

776-
function StrWrap(const Str: UnicodeString; const MaxLen, Margin: UInt16):
777-
UnicodeString;
785+
function StrWrap(const Str: UnicodeString; const MaxLen, Margin: UInt16;
786+
const FirstLineOffset: Int16): UnicodeString; overload;
778787
var
779788
Word: UnicodeString; // next word in input Str
780789
Line: UnicodeString; // current output line
@@ -783,14 +792,25 @@ function StrWrap(const Str: UnicodeString; const MaxLen, Margin: UInt16):
783792
// -------------------------------------------------------------------------
784793
/// Adds a line of text to output, offseting line by Margin spaces
785794
procedure AddLine(const Line: string);
795+
var
796+
AdjustedMargin: UInt16;
786797
begin
798+
AdjustedMargin := Margin;
787799
if Result <> '' then // not first line: insert new line
788-
Result := Result + EOL;
789-
Result := Result + StrOfSpaces(Margin) + Line;
800+
Result := Result + EOL
801+
else // 1st line - adjust margin
802+
AdjustedMargin := Margin + FirstLineOffset;
803+
Result := Result + StrOfSpaces(AdjustedMargin) + Line;
790804
end;
791805
// -------------------------------------------------------------------------
792806

793807
begin
808+
// FirstLineOffset, if negative, must have absolute value <= Margin and
809+
// FirstLineOffset, if positive, added to Margin must fit in UInt16
810+
Assert((Margin + FirstLineOffset >= 0)
811+
and (Margin + FirstLineOffset < High(Margin)),
812+
'StrWrap: FirstLineOffset + Margin out of range'
813+
);
794814
// Get all words in Str
795815
Words := TStringList.Create;
796816
try

0 commit comments

Comments
 (0)