The library · lesson 2 of 18

Strings and bytes

A []U8 is both a string and a block of bytes. Str has what is done with them: comparing, finding, hashing, copying into memory of your own, UTF-8, numbers, and the fixed byte orders of file formats and network protocols. Bytes has the searches, made fast.

Ordering and hashing

Str.cmp gives -1, 0 or 1, byte by byte, so it orders UTF-8 text by code point. Str.eq is equality. Str.hash is FNV-1a, 64 bits; it is what a Map of strings uses.

Order.raz
module Order;
import Out, Str, Fmt;

proc main*;
var names: [4][]U8; i, j: INT; t: []U8;
begin
    names := ["pear", "Apple", "fig", "apple"];
    (* a small insertion sort, with Str.cmp *)
    for i := 1 to 3 do
        t := names[i];
        j := i - 1;
        while (j >= 0) and (Str.cmp(names[j], t) > 0) do
            names[j + 1] := names[j];
            DEC(j);
        end;
        names[j + 1] := t;
    end;
    for i := 0 to 3 do Out.str(names[i]); Out.char(' '); end;
    Out.ln;
    Fmt.print("{}\n", [Fmt.hex(Str.hash("raz"))]);
    if Str.hash("raz") = Str.hash("raz") then Out.str("stable\n"); end;
end;

end Order.
Apple apple fig pear 
89F6BC1960FF4912
stable

Upper case sorts before lower, since 'A' is 65 and 'a' is 97: Str.cmp doesn't pretend to know a language's alphabet.

Keeping a string

A slice of a string constant lives forever, but a slice of a buffer lives only as long as the buffer. To keep one past that, Str.copy takes memory from an allocator and says whether it got any.

Keep.raz
module Keep;
import Out, Str, Heap, Mem;

type Entry = record name: []U8; n: INT; end;

var entries: [3]Entry;

(* remembers a name that the caller will overwrite *)
proc remember(slot: INT; name: []U8);
var copy: []U8; ok: BOOL;
begin
    copy, ok := Str.copy(Heap.heap, name);
    if ok then entries[slot].name := copy; entries[slot].n := slot; end;
end;

proc main*;
var scratch: [16]U8; i: INT;
begin
    for i := 0 to 2 do
        scratch := "name-";
        scratch[5] := U8('0' + i);
        remember(i, scratch[0..6]);
        scratch := "overwritten!";
    end;
    for i := 0 to 2 do
        Out.str(entries[i].name); Out.char(' ');
        Mem.free(Heap.heap, ^U8(ADR(entries[i].name[0])));
    end;
    Out.ln;
end;

end Keep.
name-0 name-1 name-2 

Searching

Bytes finds bytes many at a time: Bytes.index and Bytes.last give the first and the last place of a byte in a []U8, or -1, Bytes.count how many places it has, and Bytes.find where one []U8 first starts in another (Str.find is the same). Each is what a loop over the bytes would give, only faster: the library's own body compares eight bytes at once in a U64, and on x86-64 a file of its own, Bytes.x86_64.raz, compares sixteen with SSE2, in code procedures.

Paths.raz
module Paths;
import Out, Fmt, Bytes;

(* the last part of a path: what follows its last slash *)
proc base(path: []U8): []U8;
begin
    return path[Bytes.last(path, '/') + 1..LEN(path)];
end;

proc main*;
var text: []U8; at, n, line: INT;
begin
    Out.str(base("/usr/local/lib/raz/Bytes.raz")); Out.char(' ');
    Out.str(base("Out.raz")); Out.ln;

    text := "one line\nanother line\nthe line with raz in it\nthe last\n";
    Fmt.print("{} lines\n", [Fmt.int(Bytes.count(text, 10))]);
    at := Bytes.find(text, "raz");
    if at >= 0 then
        (* the line it is on: the newlines before it, plus one *)
        line := Bytes.count(text[0..at], 10) + 1;
        n := Bytes.index(text[at..LEN(text)], 10);
        Fmt.print("raz on line {}, at {}, and the line goes on {} more\n",
            [Fmt.int(line), Fmt.int(at), Fmt.int(n)]);
    end;
    Fmt.print("{} {}\n", [Fmt.int(Bytes.find(text, "zzz")), Fmt.int(Bytes.find(text, ""))]);
end;

end Paths.
Bytes.raz Out.raz
4 lines
raz on line 3, at 36, and the line goes on 9 more
-1 0

When the byte isn't there, Bytes.last gives -1, so base of a name with no slash is the whole name. A search of a slice gives places in the slice, counted from its own start: add the place the slice starts at to get one in the whole.

Binary formats

A file format or a network protocol has a byte order of its own, not the machine's. Str.le and Str.be read an unsigned number of up to 8 bytes in a fixed order (little- or big-endian) and Str.putLE and Str.putBE write one. They work on any slice, so a header is read where it lies, with no struct to overlay and no byte swapping to forget.

Here is a small record format: a 4-byte tag, a 2-byte big-endian length, a 4-byte little-endian checksum, then the data.

Frame.raz
module Frame;
import Out, Fmt, Str;

const HeaderSize = 10;

(* the sum of the bytes of data, all mod 2^32 *)
proc checksum(data: []U8): U32;
var i: INT; sum: U32;
begin
    for i := 0 to LEN(data) - 1 do sum := sum + U32(data[i]); end;
    return sum;
end;

(* writes a frame to out, which must have room; its length *)
proc pack(out: []U8; tag, data: []U8): INT;
begin
    COPY(out[0..4], tag);
    Str.putBE(out[4..6], U64(LEN(data)));
    Str.putLE(out[6..10], U64(checksum(data)));
    COPY(out[HeaderSize..HeaderSize + LEN(data)], data);
    return HeaderSize + LEN(data);
end;

proc main*;
var buf: [64]U8; n, len: INT; sum: U32; i: INT;
begin
    n := pack(buf, "DATA", "hello");
    for i := 0 to n - 1 do
        Out.hex(U64(buf[i]), 2); Out.char(' ');
    end;
    Out.ln;
    (* and back *)
    len := INT(Str.be(buf[4..6]));
    sum := U32(Str.le(buf[6..10]));
    Fmt.print("tag {}, {} bytes, checksum {}, ", [Fmt.str(buf[0..4]), Fmt.int(len), Fmt.hex(U64(sum))]);
    if sum = checksum(buf[HeaderSize..HeaderSize + len]) then
        Out.str("valid: "); Out.str(buf[HeaderSize..HeaderSize + len]);
    else
        Out.str("corrupt");
    end;
    Out.ln;
end;

end Frame.
44 41 54 41 00 05 14 02 00 00 68 65 6C 6C 6F 
tag DATA, 5 bytes, checksum 214, valid: hello

The output is the same on a little-endian and a big-endian machine, because the bytes are written one at a time in the order the format says.

Numbers and text

Str.toInt reads a decimal number with an optional sign and nothing else, and says whether it was one; Str.toHex reads hexadecimal digits, and Math.toReal a real, as a constant is written but with e for E too, to the nearest F64 exactly. Str.fromInt goes the other way into an array of 20 bytes, with no allocator, and Fmt does the rest. Str.encode and Str.decode move between characters and UTF-8, as the first tour showed.

Numbers.raz
module Numbers;
import Out, Str, Math, Fmt;

(* the sum of the numbers in a comma separated list, or -1 if any isn't *)
proc total(s: []U8): I64;
var part: []U8; sum, n: I64; ok, more: BOOL;
begin
    more := true;
    while more do
        part, s, more := Str.cut(s, ",");
        n, ok := Str.toInt(part);
        if not ok then return -1; end;
        sum := sum + n;
    end;
    return sum;
end;

proc main*;
var u: U64; x: F64; ok: BOOL; digits: [20]U8; k: INT;
begin
    Out.int(total("10,20,-5,+7")); Out.ln;
    Out.int(total("10,x,20")); Out.ln;
    Out.int(total("10,,20")); Out.ln;
    u, ok := Str.toHex("ff00");
    if ok then Out.hex(u, 1); Out.ln; end;
    x, ok := Math.toReal("-1.5e3");
    if ok then Fmt.print("{}\n", [Fmt.real(x, 1)]); end;
    digits, k := Str.fromInt(-9223372036854775807 - 1);
    Out.str(digits[0..k]); Out.ln;
end;

end Numbers.
32
-1
-1
FF00
-1500.0
-9223372036854775808

Save a program under its module’s name and run it with raz run Name.raz.