Janilabo

Janilabo | Indentation()

Oct 14th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.18 KB | None | 0 0
  1. const
  2.   TEST_STR = 'TESTING Indentation()!' #13 +
  3.              '      This should work nicely...' #13#10 +
  4.              '    ..AND IT DOES. ;)';
  5.   INDENT_COUNT = 2;
  6.          
  7. function Indentation(str: string; count: Integer): string;
  8. var
  9.   s: string;
  10.   c: Integer;
  11.   m: TRegexMatchArray;
  12. begin
  13.   if ((str <> '') and (count <> 0)) then
  14.     if (count < 1) then
  15.     begin
  16.       c := iAbs(count);
  17.       Result := PregReplace('/(\r\n?|\n|\r)\s{1,' + IntToStr(c) + '}/', #13#10, str);
  18.       if PregMatchEx('/\A\s{1,' + IntToStr(c) + '}/', Result, m) then
  19.       begin
  20.         Delete(Result, m[0].Offset, m[0].Length);
  21.         SetLength(m, 0);  
  22.       end;
  23.     end else
  24.     begin
  25.       s := StringOfChar(' ', iAbs(count));
  26.       Result := PregReplace('/(\r\n?|\n|\r)/', (#13#10 + s), (s + str));
  27.     end;
  28. end;
  29.  
  30. var
  31.   str: string;
  32.   c, i: Integer;
  33.  
  34. begin
  35.   str := TEST_STR;
  36.   c := INDENT_COUNT;
  37.   for i := 1 to 21 do
  38.   begin  
  39.     ClearDebug;  
  40.     if (i = 10) then
  41.       c := (IAbs(INDENT_COUNT) * -1);
  42.     str := Indentation(str, c);
  43.     WriteLn('Indentation(str, ' + IntToStr(c) + ') [' + IntToStr(i) + '/21]' #13#10#13#10 + str);
  44.     Wait(100);
  45.   end;        
  46. end.
Advertisement
Add Comment
Please, Sign In to add comment