[−][src]Module inc::strings
A string is a blob of UTF-8 encoded bytes prefixed with the length if it.
Strings can be stack or heap allocated but static strings found in the source code is retained as it is in the data section.
Example memory layout:
A literal "hello world" gets statically allocated at offset 4000 along with
the length. There is no extra allocation required for the string object
after immediate tagging the address as 4005 (4000 | strtag)
.
-------------------------
| Address | Value |
-------------------------
| 4000 | 11 |
| 4000 | "hello world" |
| | |
| 8000 | 4005 |
-------------------------
The C runtime would get the value 4005 and would identify it as a string
with the tag (8005 & mask == strtag)
. The raw pointer is obtained by
removing the tag (p = val - strtag)
and length is found at the base
addresses (*p)
and the data at (*p + 1)
. fwrite can safely print the
exact number of bytes using the length and pointer.
TODO: Consider switching to SDS. https://github.com/antirez/sds
Functions
eval | Evaluate a string object |
inline | Inline static strings in source directly into the binary |
lift | Lift static strings into a symbol table for inlining later. |
make | Allocate a string object in heap with a specific size |