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:
>,----------[++++++++++>,----------]
What it needs
- A zero cell before the string and enough zero cells after it.
- The newline form needs wrapping cells and an input that has a newline in it.
- The end-of-input form needs
,to give 0 (or leave the cell alone) at the end of the input.
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
| Form | Plain steps | Optimised steps |
|---|---|---|
| Up to a newline | 13 + 23 per letter | 3 + 4 per letter |
| Up to the end of the input | 3 + 3 per letter | 2 + 2 per letter |
| The form from the wiki, keeping the newline | 44 + 23 per letter | 7 + 4 per letter |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| Up to a newline | the line = "" | 8 | 36 | 13 | 3 |
| Up to a newline | the line = "hello" | 8 | 36 | 128 | 23 |
| Up to the end of the input | the input = "hello" | 8 | 6 | 18 | 12 |
| The form from the wiki, keeping the newline | the line = "hello" | 8 | 44 | 159 | 27 |
Other forms
Up to the end of the input
Shorter. Needs 0 at the end of the input.
>,[>,]
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.
----------[++++++++++>,----------]++++++++++
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
- Reverse a string and compare two strings start with this.
- The chat program reads its question this way, one byte per cell, before it turns it into word pieces.
Sources
- Source: Esolang wiki: Brainfuck algorithms: sections "Read all characters into memory" and "Read until newline/other char".
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.