aboutsummaryrefslogtreecommitdiff
path: root/system/libraries/string/builder.zig
blob: 16bd35cc3e29698fa49618a6bdb6bbf47963acb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! String building

pub fn build(buf: []u8, parts: anytype) []const u8 {
    var pos: usize = 0;
    inline for (parts) |part| {
        for (part) |c| {
            if (pos >= buf.len) break;
            buf[pos] = c;
            pos += 1;
        }
    }
    return buf[0..pos];
}

pub fn concat(buf: []u8, a: []const u8, b: []const u8) []const u8 {
    var pos: usize = 0;
    for (a) |c| {
        if (pos >= buf.len) break;
        buf[pos] = c;
        pos += 1;
    }
    for (b) |c| {
        if (pos >= buf.len) break;
        buf[pos] = c;
        pos += 1;
    }
    return buf[0..pos];
}

pub fn concat3(buf: []u8, a: []const u8, b: []const u8, c: []const u8) []const u8 {
    var pos: usize = 0;
    for (a) |ch| {
        if (pos >= buf.len) break;
        buf[pos] = ch;
        pos += 1;
    }
    for (b) |ch| {
        if (pos >= buf.len) break;
        buf[pos] = ch;
        pos += 1;
    }
    for (c) |ch| {
        if (pos >= buf.len) break;
        buf[pos] = ch;
        pos += 1;
    }
    return buf[0..pos];
}