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.

Conceptually, it looks like this:

CODE_PLAYGROUND
val name = "John "
val surname = "Doe"
print(name ++ surname)
constants:
  0: "John "
  1: "Doe"

stack:
  name ~> 0x00A0
  surname ~> 0x00A0
  r2 ~> 0xFA00

heap:
  0x00A0: ObjectString {
    base: Object {
      t: ObjectType.String,
    }
    bytes: .constant ~> 0
  }
  0x00B0: ObjectString {
    base: Object {
      t: ObjectType.String,
    }
    bytes: .constant ~> 1
  }
  0x00C0: ObjectString {
    base: Object {
      t: ObjectType.String,
    }
    bytes: .heap ~> 0xFA00
  }
  // ...
  0xFA00: "John Doe"

run:
  op_concat(r2, name, surname)
  op_print(r2)