liveBook · Manning

Code unit

As explained in Julia as a Second Language

A code unit is a fixed-size storage unit used by an encoding to represent a Unicode code point in memory.

A code unit is a storage unit that an encoding uses to represent a Unicode code point in memory. The code point identifies a character; the code units hold its encoded representation. Julia strings use UTF-8, so one character can occupy a variable number of code units rather than one fixed-size slot. section 11.1.1 section 11.5

Iterating safely through a UTF-8 string

For s = "Þrúðvangar", use firstindex(s), lastindex(s), and nextind(s, i) to move from one character boundary to the next. The resulting indices are 1, 3, 4, 6, 8, 9, 10, 11, 12, and 13, demonstrating why ordinary integer stepping is unsafe for UTF-8 strings.

How Julia as a Second Language covers it

How deep it goes
Defines the code-point distinction, demonstrates variable counts, and shows code-unit-aware indexing without fully deriving UTF-8 decoding.
Examples
Uses A, Æ, 😏, and Þrúðvangar to compare code-unit counts and character-boundary indices.
What it assumes
Assumes basic Julia REPL and string familiarity while teaching Unicode code points, code units, and variable-width encoding from the beginning.
What you can do after
Evaluate code points and code-unit counts, then iterate through Julia strings using character-aware indices.
How it connects
Connects Unicode code points, UTF-8, ASCII, Unicode characters, and string indexing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.