Standard Utilities — the sysutils unit

The sysutils unit implements FPC-compatible standard utility routines for string conversion, path manipulation, process execution, and memory operations. Almost every non-trivial Pascal program imports this unit.


Exception Class

The sysutils unit defines the base Exception class used by the PXX exception handling system.

type
  Exception = class
  private
    FMessage: string;
    FHelpContext: Integer;
  public
    constructor Create(const msg: string);
    property Message: string read FMessage write FMessage;
    property HelpContext: Integer read FHelpContext write FHelpContext;
  end;

Conversions and Formatting

Integer Conversions

Floating-Point Conversions

String Formatting


String Manipulation


File System and Path Helpers


Process and System Primitives


Low-Level Memory Helpers


Compiling Example

The following program demonstrates formatting, string conversions, string manipulation, and path helpers. It compiles and runs on the pinned compiler:

program sysutils_demo;

uses sysutils;

procedure DemoFormatting;
var
  s: AnsiString;
begin
  writeln('--- Formatting & Conversions ---');
  writeln('IntToStr: ', IntToStr(42));
  writeln('IntToHex: ', IntToHex(255, 4));
  writeln('FloatToStr: ', FloatToStr(3.14159));
  writeln('FloatToStrF: ', FloatToStrF(3.14159, 2));
  
  // Format with array of const
  s := Format('Hello %s, the answer is %d, float is %.2f', ['PXX', 42, 3.14159]);
  writeln('Format: ', s);
end;

procedure DemoStrings;
var
  s: AnsiString;
begin
  writeln('--- String Manipulation ---');
  s := '  PXX Compiler  ';
  writeln('Trimmed: "', Trim(s), '"');
  writeln('Upper: ', UpperCase(s));
  writeln('Lower: ', LowerCase(s));
  writeln('Pos of "Comp": ', Pos('Comp', s));
  
  // StringReplace
  s := 'apple, banana, apple';
  writeln('Replace: ', StringReplace(s, 'apple', 'orange', [rfReplaceAll]));
end;

procedure DemoPaths;
var
  path: AnsiString;
begin
  writeln('--- Path Helpers ---');
  path := '/usr/local/bin/pxx.exe';
  writeln('FileName: ', ExtractFileName(path));
  writeln('FilePath: ', ExtractFilePath(path));
  writeln('FileDir:  ', ExtractFileDir(path));
  writeln('FileExt:  ', ExtractFileExt(path));
  writeln('ChangeExt: ', ChangeFileExt(path, '.o'));
end;

begin
  DemoFormatting;
  DemoStrings;
  DemoPaths;
end.

Output

--- Formatting & Conversions ---
IntToStr: 42
IntToHex: 00FF
FloatToStr: 3.14159
FloatToStrF: 3.14
Format: Hello PXX, the answer is 42, float is 3.14
--- String Manipulation ---
Trimmed: "PXX Compiler"
Upper:   PXX COMPILER  
Lower:   pxx compiler  
Pos of "Comp": 7
Replace: orange, banana, orange
--- Path Helpers ---
FileName: pxx.exe
FilePath: /usr/local/bin/
FileDir:  /usr/local/bin
FileExt:  .exe
ChangeExt: /usr/local/bin/pxx.o

Next