liveBook · Manning

Unicode

As explained in Julia as a Second Language

Unicode is a universal character standard that assigns identifiers and encoding rules to characters across writing systems.

Text gets tricky as soon as it extends beyond A–Z. Unicode assigns a number, called a code point, to characters across writing systems. UTF-8 is a way to encode those code points as bytes. Julia strings use Unicode text encoded in UTF-8. section 11.0 section 11.1 section 11.1.1

UTF-8 uses one to four bytes for a character. Familiar ASCII letters take one byte; characters such as Æ can take more. A code point identifies the character, while code units represent it in the encoding. That distinction matters when measuring and indexing strings. section 11.1 section 11.1.1

julia> sizeof("Æser")         #1
5
 
julia> sizeof("Þrúðvangr")    #2
12

Compare length with sizeof: "Æser" has four characters but occupies five bytes; "Þrúðvangr" has nine characters but occupies twelve bytes. section 11.1

julia> codepoint('Æ')
0x000000c6
 
julia> ncodeunits('Æ')   #1
2
 
julia> isascii('Æ')      #2
false
 
julia> codepoint('😏')   #3
0x0001f60f
 
julia> ncodeunits('😏')  #4
4
 
julia> isascii('😏')
false

Inspect a character with codepoint and ncodeunits. The examples show that 'Æ' and '😏' have distinct code points and require two and four UTF-8 code units, respectively. section 11.1.1

String indices need not advance one position per character. In "Æser", index 2 raises a StringIndexError because it falls inside the encoding of Æ; the next character begins at index 3. To visit characters without guessing their indices, iterate directly with for, or use eachindex when the indices matter. section 11.1 section 11.1.1

julia> for i in eachindex(s)
          print(i, s[i], " ")
      end
1Þ 3r 4ú 6ð 8v 9a 10n 11g 12a 13r

The eachindex loop prints each valid index beside its character, making the gaps after multibyte characters visible. section 11.1.1

How Julia as a Second Language covers it

How deep it goes
Explains code points and variable-width UTF-8 with REPL examples, then demonstrates safe iteration and excerpts Julia’s nextind implementation.
Examples
Compares Norwegian-word lengths with byte sizes and inspects 'A', 'Æ', and '😏' using Julia functions.
What it assumes
Assumes basic experience with Julia strings while introducing Unicode, code points, and UTF-8 from a familiar ASCII comparison.
What you can do after
Diagnose/debug surprising Julia string indices and iterate over characters without assuming one byte per character.
How it connects
Connects Unicode to ASCII, UTF-8, code points, code units, and string indexing.

Also explained in

Compare how 8 books explain Unicode →

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.