Getting the actual length of a UTF-8 encoded stdstring?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The phrase "actual length" is the source of most confusion around UTF-8 strings in C++. std::string::size() returns the number of bytes, not the number of Unicode code points and definitely not the number of user-perceived characters on screen.
What std::string::size() Really Means
A UTF-8 string is stored as a sequence of bytes. Because UTF-8 uses one to four bytes per code point, a single visible symbol may occupy several bytes.
That means this code measures storage length, not text length:
The string contains four code points, but the accented letter uses two bytes, so size() reports five.
Counting UTF-8 Code Points
If by "actual length" you mean the number of Unicode code points, you can count leading bytes in the UTF-8 sequence. Continuation bytes always begin with the bit pattern 10, so every non-continuation byte starts a new code point.
This approach is simple and fast, but it assumes the input is valid UTF-8. If the bytes are malformed, the count may be meaningless.
Code Points Are Not Always Visible Characters
Even code point count may not match what a user thinks of as the number of characters. Some visible characters are built from multiple code points. A common example is an accented letter formed by a base character plus a combining mark.
For example, one rendered glyph can be stored as:
- a single precomposed code point
- or a base letter followed by a combining accent
Those two forms may look identical on screen but produce different code point counts. So there are really three different measurements:
- bytes in memory
- Unicode code points
- grapheme clusters, meaning user-perceived characters
If your UI needs cursor movement, truncation, or text layout, code point count is often still not enough.
Validating Before Counting
When data comes from files, APIs, or user input, do not assume the bytes are valid UTF-8. A more robust implementation validates each sequence first.
This still counts code points, but it fails fast on broken byte sequences instead of returning a misleading answer.
When You Need Grapheme Clusters
If you need to count what users visually perceive as characters, use a Unicode-aware text library such as ICU. Standard C++ does not provide a full grapheme cluster API, and trying to implement Unicode segmentation rules yourself is not realistic for most applications.
That distinction matters for:
- text editors
- terminal rendering
- UI truncation with ellipses
- cursor movement
- input validation based on displayed length
In those cases, "actual length" should usually mean grapheme clusters, not bytes and not raw code points.
Common Pitfalls
- Treating
std::string::size()as a character count. It is a byte count. - Assuming code points and visible characters are always the same thing.
- Counting non-continuation bytes without validating the input first.
- Switching to
std::wstringand assuming the problem disappears. Wide strings are platform-dependent and still do not solve Unicode text segmentation. - Forgetting to define what "length" means before writing the code.
Summary
- '
std::string::size()reports bytes, not Unicode characters.' - Counting non-continuation bytes gives a code point count for valid UTF-8.
- Invalid UTF-8 should be validated before counting.
- User-visible character count requires grapheme cluster handling, usually via a library like ICU.
- The correct solution depends on whether you need bytes, code points, or displayed characters.
Related reading
- Given a string, find two identical subsequences with consecutive indexes C
- Group the numbers C
- gRPC cpp async server vs sync server
- Hashing of pointer values
- Hidden Markov Models with C
- How can I benchmark the performance of C code?
- How can I concatenate two arrays in C?
- How can I distinguish between high- and low-performance cores/threads in C?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.