class

String

A list of characters representing text.

THP strings are...

Common operations

  • Op 1
  • Op 2: ref
val name = "hello"

Functions

fun to_lowercase(self) -> String

Clones the string as lowercase

fun to_uppercase(self) -> String

Clones the string as uppercase

fun len(self) -> u64

Returns the length of the string, in UTF-8 code units

fun size(self) -> u64

Returns the number of bytes of the string

fun split(self, split_char: u8) -> Array[String]

Splits the array by `split_char`

fun bytes(self) -> Array[u8]

Returns an array with the bytes of the string

fun from_bytes(self, bytes: Array[u8]) -> !String

Returns an array with the bytes of the string

fun replace_once(self, needle: String, _: String) -> B) -> String

Creates a new string with the first occurence replaced

fun is_empty(self) -> Bool

Returns true if the string is empty

SEE ALSO

Internals

Internally, a nara String is just a slice of bytes stored on the heap, as []u8. There may be checks in place at build time and when building a new String, but the concept is simple.

On memory, a String is represented by a zig ObjectString, which contains a base Object, and the bytes.

pub const Object = struct {
    t: ObjectType,
};

pub const ObjectString = struct {
    base: Object,
    bytes: union(enum) {
        constant: []const u8,
        heap: []u8,
    },
}

The Object is a struct stored in the heap, and thus allows ObjectString to live on the heap. Then, the VM just stores pointers to that ObjectString.

Settings

Regular