Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 21 do
- begin
- ClearDebug;
- if (i = 10) then
- c := (IAbs(INDENT_COUNT) * -1);
- str := Indentation(str, c);
- WriteLn('Indentation(str, ' + IntToStr(c) + ') [' + IntToStr(i) + '/21]' #13#10#13#10 + str);
- Wait(100);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment