Strings

PetWay strings can be embedded, allocated, and static. Character size can vary from 1 to 4 for embedded and allocated strings. For static strings it can be only 1 or 4.

Embedded strings are stored in 128-bit wide PwValue. Maximal length depends on character size:

Longer strings are stored in dynamically allocated memory.

Static strings ease interfacing with C code. Only char* and char32_t* are supported. wchat_t is not supported to avoid the hell with surrogate pairs.

The length of string is always 32 bit wide, even on ILP64. This is because it is stored in PwValue to minimize memory footprint of allocated data.

Allocated strings are reference counted, so making a copy is a fast operation (clone, in terms of PW).

Strings are mutable, even static, but they are copied on write. In other words a task that wants to modify a shared string gets its own copy to modify in place. Allocated strings are copied on write only if reference count is greater than 1.

C strings

PetWay provides PW_CSTRING macro to interface with C functions. This macro declares a local array of chars on stack and copies PetWay string to it, converting to UTF-8. An example from pw-curl:

[[nodiscard]] bool urljoin_cstr(char* base_url, char* other_url, PwValuePtr result);

[[nodiscard]] bool urljoin(PwValuePtr base_url, PwValuePtr other_url, PwValuePtr result)
{
    PW_CSTRING(cstr_base_url, base_url);
    PW_CSTRING(cstr_other_url, other_url);
    return urljoin_cstr(cstr_base_url, cstr_other_url, result);
}