String Formatting in Pascal

program StringFormatting;

uses
  SysUtils;

type
  TPoint = record
    x, y: Integer;
  end;

procedure Main;
var
  p: TPoint;
  s: string;
begin
  // Pascal offers several formatting options for various data types.
  // Here are some examples of common string formatting tasks.

  // Formatting a record (struct equivalent in Pascal)
  p.x := 1;
  p.y := 2;
  WriteLn(Format('record1: (%d, %d)', [p.x, p.y]));

  // Pascal doesn't have a direct equivalent to Go's %+v or %#v,
  // but we can create a custom string representation
  WriteLn(Format('record2: TPoint(x: %d, y: %d)', [p.x, p.y]));

  // To print the type of a value, we can use TypeInfo
  WriteLn(Format('type: %s', [TPoint.ClassName]));

  // Formatting booleans
  WriteLn(Format('bool: %s', [BoolToStr(True, True)]));

  // Formatting integers
  WriteLn(Format('int: %d', [123]));

  // Binary representation
  WriteLn(Format('bin: %.8b', [14]));

  // Character representation
  WriteLn(Format('char: %s', [Chr(33)]));

  // Hexadecimal representation
  WriteLn(Format('hex: %x', [456]));

  // Formatting floats
  WriteLn(Format('float1: %f', [78.9]));

  // Scientific notation
  WriteLn(Format('float2: %e', [123400000.0]));
  WriteLn(Format('float3: %E', [123400000.0]));

  // String formatting
  WriteLn(Format('str1: %s', ['"string"']));

  // Pascal doesn't have a direct equivalent to Go's %q,
  // but we can manually escape the quotes
  WriteLn(Format('str2: "%s"', [StringReplace('"string"', '"', '\"', [rfReplaceAll])]));

  // Hexadecimal representation of a string
  WriteLn(Format('str3: %s', [StringToHex('hex this')]));

  // Pointer formatting
  WriteLn(Format('pointer: %p', [@p]));

  // Width specification for integers
  WriteLn(Format('width1: |%6d|%6d|', [12, 345]));

  // Width and precision for floats
  WriteLn(Format('width2: |%6.2f|%6.2f|', [1.2, 3.45]));

  // Left-justified floats
  WriteLn(Format('width3: |%-6.2f|%-6.2f|', [1.2, 3.45]));

  // Width specification for strings
  WriteLn(Format('width4: |%6s|%6s|', ['foo', 'b']));

  // Left-justified strings
  WriteLn(Format('width5: |%-6s|%-6s|', ['foo', 'b']));

  // Formatting without printing
  s := Format('sprintf: a %s', ['string']);
  WriteLn(s);

  // Writing to a different output (in Pascal, we use AssignFile)
  AssignFile(Output, 'error.log');
  Rewrite(Output);
  WriteLn(Format('io: an %s', ['error']));
  CloseFile(Output);
  
  // Reassign standard output
  AssignFile(Output, '');
  Rewrite(Output);
end;

begin
  Main;
end.

This Pascal program demonstrates various string formatting techniques similar to the Go example. Here are some key differences and explanations:

  1. Pascal uses the Format function for string formatting, which is similar to Go’s fmt.Sprintf.

  2. Pascal doesn’t have direct equivalents for some of Go’s formatting verbs (like %+v or %#v), so we’ve used alternative approaches or omitted them where not applicable.

  3. For binary representation, Pascal’s Format function uses %b, but we need to specify the width to get leading zeros.

  4. Pascal doesn’t have a built-in way to format pointers, so we’ve used %p which might not work in all Pascal compilers.

  5. For file output, Pascal uses AssignFile and Rewrite instead of Go’s os.Stderr.

  6. Some formatting options (like left-justification) are achieved using the same symbols as in Go (-).

To run this program, save it as StringFormatting.pas and compile it with a Pascal compiler. The output will be similar to the Go version, with some differences due to language-specific features and limitations.