Skip to content
Building text

Building text

Text is bytes, and bytes cannot be changed, so text is built by joining pieces into new bytes.

fn main() -> int {
  let names = bytes_cons(head: "mira", tail: bytes_cons(head: "tomas", tail: one_bytes("ines")));
  print(concat3("players: ", joined(sorted(names), ", "), "\n"));
  print(concat5("rounds: ", int_to_text(4), ", best: ", int_to_text(24), "\n"))
}
bootstrap/qdx run std/*.qdx text.qdx
players: ines, mira, tomas
rounds: 4, best: 24

The pieces

  • concat joins two pieces, and concat3, concat4 and concat5 join more in one call.
  • int_to_text turns a number into its digits. Nothing converts a number to text automatically.
  • joined puts a separator between the items of a bytes_list, and sorted orders them first.
  • byte(10) makes one byte from its number, for the rare byte that has no escape.

Cost

Every concat copies both sides into new bytes. Adding one small piece at a time to a long text copies the long part again on each step, so the work grows with the square of the length. For a few lines of output that does not matter.

For long output, do not build it at all. Print each piece as you reach it, as the score program does with one print per player.

Line ends

print adds nothing. A line end is the \n you write.