Strings

How strings work in AHA! — a real struct, safe concatenation, and O(1) length

Strings

Strings in AHA! are a real {pointer, length} struct — not a bare pointer hack. That makes concatenation safe and len() O(1).

String Literals

let name = "world";

Supported escape sequences: \n, \t, \\, \", \r, \0.

Concatenation

+ on strings allocates a new buffer via malloc, copies both parts with memcpy, and null-terminates the result:

let name = "world";
let greeting = "Hello, " + name;
print_str(greeting); // Hello, world

Comparison

== and != compare the full contents via strcmp:

if "aha" == "aha" {
    print(1); // 1
}

Length

len() reads the length field of the struct — O(1), no scanning:

print(len(name));  // 5
print(len(""));    // 0

Printing

  • print_str(s) — print a string
  • print(n) — print an integer

See Builtins for the full list.

Strings in Functions

Strings can be passed as arguments and returned from functions. The return type is inferred automatically by a pre-pass:

fn shout(s) {
    s + "!"
}

print_str(shout("aha")); // aha!