typestar

Bytes versus characters in Mojo

A String holds UTF-8, so length depends on what you count.

def main():
    var plain = String("cafe")
    # Escapes keep this source ASCII; the stored text is not.
    var accented = String("caf\u00e9")
    print(plain.byte_length(), plain.count_codepoints())
    print(accented.byte_length(), accented.count_codepoints())
    var snowy = String("snow \u2603")
    print(snowy.byte_length(), snowy.count_codepoints())

How it works

  1. cafe is pure ASCII, so both measurements agree.
  2. The escaped accent costs two bytes but only one codepoint.
  3. byte_length() counts storage; count_codepoints() counts characters.

Keywords and builtins used here

The run, in numbers

Lines
8
Characters to type
330
Tokens
63
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 50 seconds.

Type this snippet

Step 5 of 5 in Strings, step 22 of 26 in Language basics.

← Previous Next →