Загрузка данных


unit ConsoleUtils;

{$mode objfpc}{$H+}
{$codepage UTF-8}
{$modeswitch typehelpers}

interface

uses
  {$ifdef unix}cwstring,{$endif}
  SysUtils;

type
  TStringHelperUTF8 = type helper(TStringHelper) for string
    function CharLength: Integer;
    function PadRightUTF8(AWidth: Integer): string;
    function PadLeftUTF8(AWidth: Integer): string;
    function PadCenterUTF8(AWidth: Integer): string;
  end;

function PadRightUTF8(const S: string; AWidth: Integer): string;
function PadLeftUTF8(const S: string; AWidth: Integer): string;
function PadCenterUTF8(const S: string; AWidth: Integer): string;
function CharLength(const S: string): Integer;

implementation

function CharLength(const S: string): Integer;
begin
  Result := Length(UnicodeString(S));
end;

function PadRightUTF8(const S: string; AWidth: Integer): string;
var
  Pad: Integer;
begin
  Pad := AWidth - CharLength(S);
  if Pad > 0 then
    Result := S + StringOfChar(' ', Pad)
  else
    Result := S;
end;

function PadLeftUTF8(const S: string; AWidth: Integer): string;
var
  Pad: Integer;
begin
  Pad := AWidth - CharLength(S);
  if Pad > 0 then
    Result := StringOfChar(' ', Pad) + S
  else
    Result := S;
end;

function PadCenterUTF8(const S: string; AWidth: Integer): string;
var
  Pad, LeftPad: Integer;
begin
  Pad := AWidth - CharLength(S);
  if Pad > 0 then
  begin
    LeftPad := Pad div 2;
    Result := StringOfChar(' ', LeftPad) + S +
              StringOfChar(' ', Pad - LeftPad);
  end
  else
    Result := S;
end;

{ TStringHelperUTF8 }

function TStringHelperUTF8.CharLength: Integer;
begin
  Result := ConsoleUtils.CharLength(Self);
end;

function TStringHelperUTF8.PadRightUTF8(AWidth: Integer): string;
begin
  Result := ConsoleUtils.PadRightUTF8(Self, AWidth);
end;

function TStringHelperUTF8.PadLeftUTF8(AWidth: Integer): string;
begin
  Result := ConsoleUtils.PadLeftUTF8(Self, AWidth);
end;

function TStringHelperUTF8.PadCenterUTF8(AWidth: Integer): string;
begin
  Result := ConsoleUtils.PadCenterUTF8(Self, AWidth);
end;

end.