⚡ Zig Guide LiveUnofficialbut fully verified
✓ Zig 0.18.0-dev.738+ae9a8acc7What's newOn an older Zig?Blograylib-zig

Survive the Terminal

Before a shell can run a command, the shell must measure, compare and copy strings. A copy must not write past the end of the buffer. The shell must also split a line into arguments, and loop until the user or the input stops. This section has three chapters. Each chapter builds one primitive by hand, and does not import it. The parts that need a real kernel (fork, exec and pipes) are in the operating system section.

  1. strlen walks the string each time that you call it. That cost is the reason why a slice stores its length.
  2. A buffer for a 5-byte string needs 6 bytes. A bounds check that forgets the terminator passes a quick test, and then the copy writes one byte past the end.
  3. A trim returns a view, not a copy. You do not need to move bytes to remove spaces.
  4. A REPL has two exits, not one. If a loop only looks for quit, the loop runs forever when a user presses Ctrl-D.

3 chapters.