Variable-length character encoding stores different characters in different numbers of bytes or code units. UTF-8 saves space by using one byte for frequently used characters while remaining compatible with ASCII. section 11.1.1
Keep these related ideas distinct:
- A code point identifies a Unicode character; code units store that code point in memory. section 11.1.1
- ASCII character A uses one code unit, Æ uses two, and 😏 uses four. section 11.1.1
- String indexes can therefore skip numbers, so Julia uses nextind rather than assuming every character occupies one position. section 11.1.1
Inspecting characters in Julia
codepoint('A') returns 0x00000041 and ncodeunits('A') returns 1; ncodeunits('Æ') returns 2, while ncodeunits('😏') returns 4. Iterating with nextind correctly traverses strings such as "Þrúðvangar". section 11.1.1
Use the encoding diagram to see how Unicode code points map to UTF-8 code units and why byte indexes do not always equal character positions. section 11.1.1
