String Formatting in Chapel

Our first program demonstrates string formatting in Chapel. Here’s the full source code:

use IO;

record Point {
  var x, y: int;
}

proc main() {
  // Chapel offers several formatting options for various data types.
  // Here are some examples of common string formatting tasks.

  // Formatting a record (similar to a struct in other languages)
  var p = new Point(1, 2);
  writef("record1: %t\n", p);

  // To include field names when printing a record
  writef("record2: %t\n", p);

  // For basic string representation
  writef("record3: %t\n", p);

  // To print the type of a value
  writef("type: %t\n", p.type:string);

  // Formatting booleans
  writef("bool: %t\n", true);

  // Formatting integers
  writef("int: %i\n", 123);

  // Binary representation
  writef("bin: %b\n", 14);

  // Character representation
  writef("char: %c\n", 33:uint(8));

  // Hexadecimal representation
  writef("hex: %x\n", 456);

  // Formatting floating-point numbers
  writef("float1: %r\n", 78.9);

  // Scientific notation
  writef("float2: %e\n", 123400000.0);
  writef("float3: %E\n", 123400000.0);

  // Basic string printing
  writef("str1: %s\n", "\"string\"");

  // Quoting strings
  writef("str2: %Q\n", "\"string\"");

  // Hexadecimal representation of a string
  writef("str3: %zr\n", b"hex this");

  // Pointer representation
  writef("pointer: %p\n", c_ptrTo(p));

  // Controlling width for integers
  writef("width1: |%6i|%6i|\n", 12, 345);

  // Controlling width and precision for floats
  writef("width2: |%6.2r|%6.2r|\n", 1.2, 3.45);

  // Left-justifying within a given width
  writef("width3: |%-6.2r|%-6.2r|\n", 1.2, 3.45);

  // Controlling width for strings
  writef("width4: |%6s|%6s|\n", "foo", "b");

  // Left-justifying strings
  writef("width5: |%-6s|%-6s|\n", "foo", "b");

  // Formatting without printing (similar to sprintf)
  var s = "%s: a %s".format("format", "string");
  writeln(s);

  // Writing to a different channel (similar to stderr)
  var errorChannel = stderr;
  errorChannel.writef("io: an %s\n", "error");
}

To run the program, save it as string_formatting.chpl and use the Chapel compiler:

$ chpl string_formatting.chpl -o string_formatting
$ ./string_formatting

This will compile and run the program, producing output that demonstrates various string formatting options in Chapel.

Chapel’s writef function is similar to printf in C or fmt.Printf in Go. It allows for formatted output using format specifiers. The format method on strings is similar to Sprintf in other languages, creating a formatted string without printing it.

Note that Chapel’s formatting options might differ slightly from Go’s. For example, Chapel uses %t for general formatting (similar to Go’s %v), and %r for real numbers (similar to Go’s %f). Always refer to Chapel’s documentation for the most accurate and up-to-date information on string formatting.

Chapel also provides type-specific formatting options and allows for precise control over width and alignment, making it flexible for various output formatting needs.