Strings and Runes in Pascal

program StringsAndRunes;

uses
  SysUtils;

const
  s = 'สวัสดี';

procedure ExamineRune(r: WideChar);
begin
  if r = 't' then
    WriteLn('found tee')
  else if r = 'ส' then
    WriteLn('found so sua');
end;

var
  i, ByteCount, CharCount: Integer;
  c: Char;
  w: WideChar;

begin
  // `s` is a string assigned a literal value
  // representing the word "hello" in the Thai language.
  // Pascal string literals are typically stored in the
  // system's default encoding (often Windows-1252 or UTF-8).

  // This will produce the length of the raw bytes stored within.
  WriteLn('Len: ', Length(s));

  // This loop generates the hex values of all
  // the bytes that constitute the string `s`.
  for i := 1 to Length(s) do
  begin
    Write(IntToHex(Ord(s[i]), 2), ' ');
  end;
  WriteLn;

  // To count how many characters are in a string, we can use
  // the `Length` function. Note that this may not accurately
  // represent the number of visible characters for multi-byte
  // character sets like Thai.
  WriteLn('Character count: ', Length(s));

  // A for-loop handles strings and processes each character.
  // Note: This won't properly handle multi-byte characters.
  for i := 1 to Length(s) do
  begin
    c := s[i];
    WriteLn(Format('Character at %d is %s', [i, c]));
  end;

  WriteLn;
  WriteLn('Using WideString');
  
  // Convert to WideString for better Unicode support
  ByteCount := 1;
  CharCount := 1;
  while ByteCount <= Length(s) do
  begin
    w := WideString(s)[CharCount];
    WriteLn(Format('Character at %d is %s', [ByteCount, w]));
    Inc(CharCount);
    Inc(ByteCount, UTF8CharacterLength(s[ByteCount]));
    
    // This demonstrates passing a character value to a function.
    ExamineRune(w);
  end;
end.

This Pascal program demonstrates working with strings and characters, which is the closest equivalent to Go’s strings and runes. Here are some key points about the translation:

  1. Pascal doesn’t have a direct equivalent to Go’s rune type. We use WideChar as the closest alternative for Unicode characters.

  2. Pascal strings are typically stored in the system’s default encoding. For better Unicode support, we convert to WideString when necessary.

  3. The utf8 package functions in Go don’t have direct equivalents in Pascal. We use built-in functions and custom logic to achieve similar results.

  4. Pascal uses 1-based indexing for strings, unlike Go’s 0-based indexing.

  5. The range loop in Go doesn’t have a direct equivalent in Pascal. We use regular for loops and manual iteration over the string.

  6. Pascal doesn’t have anonymous functions or closures like Go does. We define a separate ExamineRune procedure.

  7. Error handling in Pascal is typically done through exceptions, which is different from Go’s explicit error returns.

This program demonstrates basic string manipulation, character examination, and Unicode handling in Pascal. It shows how to iterate over strings both as bytes and as wide characters, count characters, and examine individual characters.