Elixir basics · Lesson 12 (open contents)
01 · Let the code speak02 · Meet six kinds of values03 · Put values together04 · Build a new list05 · Make the shapes match06 · Choose with case07 · Parse a number safely08 · Unpack &1 and the pipe09 · Put code in a project10 · Ask precise true-or-false questions11 · Read and update nested data12 · Count visible text and bytes13 · Let function clauses choose14 · Solve a list twice15 · Choose the smallest clear control form16 · Give domain data a shape17 · Put one tested module in Mix18 · Make success and failure predictable19 · Read and write one real file20 · Take only the data you need21 · Give different data one shared action22 · Write a callback contract23 · Write down the data contract24 · Test behavior and boundaries25 · Turn a project into a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ELIXIR · FOUNDATION · LESSON 1225 minutes

Count visible text and bytes

Compare graphemes, code points, bytes, and a simple Unicode-aware sigil.

01 · Start with the whole example

Run this first

Run it once, change one input, and compare the new result.

Elixir
# Compare visible text with its UTF-8 bytes
text = "长安🙂"
{String.length(text), byte_size(text), String.codepoints(text), Regex.match?(~r/安/u, text)}
Check the result first
  • The result is {3, 10, ["长", "安", "🙂"], true}.
02 · Take the code apart

Read from the first line down

  1. Read String.length/1: Count graphemes in a string.
  2. Read byte_size/1: Count bytes in a binary.
  3. Read ~r/.../u: Build a Unicode-aware regular expression.
03 · Meet the new symbols

Symbols are not secret signs

String.length/1

Count graphemes in a string.

byte_size/1

Count bytes in a binary.

~r/.../u

Build a Unicode-aware regular expression.

04 · Ideas inside the code

Match each name to its meaning

01

grapheme

A grapheme is one user-visible character, even when several code points build it.

02

code point

A code point is one Unicode number; it is not always one visible character.

03

sigil

A sigil begins with ~ and gives a compact notation for text, regexes, dates, and other values.

05 · Make the idea clear

Why these forms are useful

Run the example first. Predict one result, then change one input and run it again.

A grapheme is one user-visible character, even when several code points build it.

A code point is one Unicode number; it is not always one visible character.

06 · Change it yourself

Close the answer and try

Split Go🙂 into graphemes and count its bytes.

Practice starting point
text = "Go🙂"
{String.____(text), ____(text)}

Target result: Return {["G", "o", "🙂"], 6}.

Stuck? Read one hint

Use graphemes and byte_size.

After you run it, see one answer
One answer
text = "Go🙂"
{String.graphemes(text), byte_size(text)}
Think about it: Must grapheme count equal byte count?

No. UTF-8 characters can use several bytes.

Take with you

Remember these three lines

  1. 1

    Use graphemes for visible text.

  2. 2

    Use bytes for binary sizes and protocols.

  3. 3

    Sigils are compact constructors, not a new data type.

Lesson completeRun the code once, then mark this lesson as done.