Code patternsDataRead a line into memory

Read a line into memory

Reads letters and stores them in cells next to each other, up to a newline or the end of the input.

Watch it run

The form “Up to a newline” works on 7 cells; the named ones are “zero”, “letter” and “end”. Press Play, drag the step slider forwards and back, or edit the line:

The figure needs JavaScript. The program below is the result for the example values.

One cell per letter, from cell 1 on. Cell 0 stays zero and the cell after the last letter is zero: a string in brainfuck is a row of non-zero cells between two zeros.

Those two zeros are what make the string usable. [<] walks to its start and [>] to its end, whatever its length: see find a zero cell or a marker.

To stop at a newline, take 10 off each letter as it arrives. A newline becomes zero and ends the loop; any other letter gets its 10 back.

The code

This is the program the figure ran. It is brainfuck as it stands: copy it, or open it in the playground, where you can change it:

>,----------[++++++++++>,----------]
Open in the playground36 characters

What it needs

Common mistakes
  • Starting on cell 0. Then there is no zero to the left of the string and [<] runs off the tape.
  • Input without a final newline in the newline form: at the end of the input it reads zeros for ever.

What it costs

A plain step is one command. An optimised step is one step of an interpreter that folds runs of the same command and flat loops. Choose the values and watch the two bars; What costs what explains how cost is counted:

The figure needs JavaScript. The table below has the measured steps.

Cost as a formula, and measured
Table 1. The cost as a function of the values. Exact formulas are checked on every case they run.
FormPlain stepsOptimised steps
Up to a newline13 + 23 per letter3 + 4 per letter
Up to the end of the input3 + 3 per letter2 + 2 per letter
The form from the wiki, keeping the newline44 + 23 per letter7 + 4 per letter
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Up to a newlinethe line = ""836133
Up to a newlinethe line = "hello"83612823
Up to the end of the inputthe input = "hello"861812
The form from the wiki, keeping the newlinethe line = "hello"84415927

Other forms

Up to the end of the input

Shorter. Needs 0 at the end of the input.

>,[>,]
Open in the playground6 characters
Run this form step by step

The form “Up to the end of the input” works on 7 cells; the named ones are “zero”, “letter” and “end”. Press Play, drag the step slider forwards and back, or edit the input:

The figure needs JavaScript. The program below is the result for the example values.

The form from the wiki, keeping the newline

Starts by taking 10 off the empty first cell, so cell 0 serves as the left zero. The newline is stored as the last letter and the pointer ends on it.

----------[++++++++++>,----------]++++++++++
Open in the playground44 characters
Run this form step by step

The form “The form from the wiki, keeping the newline” works on 7 cells; the named ones are “zero”, “letter” and “newline”. Press Play, drag the step slider forwards and back, or edit the line:

The figure needs JavaScript. The program below is the result for the example values.

Where it is used

Sources

Every program on this page was run on many inputs, in plain and in optimised mode, and compared with JavaScript. The explanations are written for this page.