# Length, size, num\_bytes, num\_chars...?

**URL:** <https://revolt.metaeducation.com/forum/t/length-size-num-bytes-num-chars/393>\
**Category:** Naming\
**Created:** [November 27, 2017, 4:47pm UTC](https://revolt.metaeducation.com/forum/t/length-size-num-bytes-num-chars/393 "2017-11-27T16:47:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hostilefork](https://revolt.metaeducation.com/forum/user_avatar/revolt.metaeducation.com/hostilefork/32/421_2.png) [@hostilefork](https://revolt.metaeducation.com/forum/u/hostilefork)\
**Post date:** [November 27, 2017, 4:47pm UTC](https://revolt.metaeducation.com/forum/t/length-size-num-bytes-num-chars/393/1 "2017-11-27T16:47:31Z")

</div>

In trying to implement UTF-8 everywhere, there is a strong need to discern in the C code the difference between _number of bytes_ and _number of codepoints_. Preferably with different C datatypes, but also preferably with different naming.

It's not clear that the distinction between LENGTH and SIZE will make it to Rebol userspace, because the fact that strings have an underlying byte size different from their number of codepoints shouldn't be visible. So the standard LENGTH should be kept. However, choices on both issues will affect API users.

Here are a few observations:

- Rebol has traditionally used LENGTH to mean number of conceptual units in the series. This is reinforced by operations like `SER_LEN(s)`, which does not return a number of bytes (unless the series units are byte-sized, e.g. a BINARY! series)

- C has a standard datatype for measuring sizes in bytes, called `size_t`. It is defined as [the datatype returned by sizeof()](https://en.wikipedia.org/wiki/Sizeof).

- C has traditionally used the function `strlen()` to count _bytes_ up until a null terminator byte in a series of `char*` (pointers to possibly-signed-or-unsigned-char). strlen() returns `size_t`.

When applied to UTF-8 data, the appearance of "len" in the strlen() function call--or in names of variables holding the result--is contentious with the semantics of "length" for the strings being operated on. Currently the variables in question are a mix of **len\_bytes** , **num\_bytes** , and just plain old "misleading" **len**.

* * *

**I think that probably the best way to attack this is to stick with the Rebol standard of LENGTH being used when talking about "number of abstract units" and SIZE being used when talking specifically about "number of bytes".**

* * *

This isn't the direction C++ went. For containers like std::vector (which we might think of as analogous to series), they went and defined a _different_\* type from size\_t, unique to each container called `container::size_type`:

[https://stackoverflow.com/questions/918567/size-t-vs-containersize-type](https://stackoverflow.com/questions/918567/size-t-vs-containersize-type)

So where Rebol has been conflating "length", C++ is conflating "size".

To square another circle here, I'll suggest doing `#define strsize strlen`, then using strsize() on UTF-8 data, and always using `size_t` when describing byte lengths.
