Jump to content
Janilabo

Indentation()

Recommended Posts

Indents str with spaces (just like the feature in SCAR, Ctrl+Shift+[i/U]).

Count is the amount of spaces, and it can be positive value (indent) or negative value (unindent).

 

Included an example with loop for both (indent & unindent):

 

const
 TEST_STR = '    TESTING Indentation()!' #13 +
            '                        This should work nicely...' #13#10 +
            '               ..AND IT DOES. ';
 INDENT_COUNT = 2;

function Indentation(str: string; count: Integer): string;
var
 s: string; 
 c: Integer;
 m: TRegexMatchArray;
begin
 if ((str <> '') and (count <> 0)) then
   if (count < 1) then
   begin 
     c := iAbs(count);
     Result := PregReplace('/(\r\n?|\n|\r)\s{1,' + IntToStr(c) + '}/', #13#10, str);
     if PregMatchEx('/\A\s{1,' + IntToStr(c) + '}/', Result, m) then
     begin
       Delete(Result, m[0].Offset, m[0].Length);
       SetLength(m, 0);   
     end;
   end else
   begin
     s := StringOfChar(' ', iAbs(count));
     Result := PregReplace('/(\r\n?|\n|\r)/', (#13#10 + s), (s + str)); 
   end; 
end;

var
 str: string;
 c, i: Integer;

begin
 str := TEST_STR;
 c := INDENT_COUNT;
 for i := 1 to 30 do
 begin   
   ClearDebug;   
   if (i = 10) then
     c := (IAbs(INDENT_COUNT) * -1);
   str := Indentation(str, c);
   WriteLn('Indentation(str, ' + IntToStr(c) + ') [' + IntToStr(i) + '/30]' #13#10#13#10 + str);
   Wait(100);
 end;        
end.

Edited by Janilabo
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...