aboutsummaryrefslogtreecommitdiff
path: root/std/cstr.zig
blob: ab250474df905e5012f95a409e53a05e40c9bf29 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const List = @import("list.zig").List;
const mem = @import("mem.zig");
const Allocator = mem.Allocator;
const debug = @import("debug.zig");
const assert = debug.assert;

const strlen = len;

pub fn len(ptr: &const u8) -> usize {
    var count: usize = 0;
    while (ptr[count] != 0; count += 1) {}
    return count;
}

pub fn cmp(a: &const u8, b: &const u8) -> i8 {
    var index: usize = 0;
    while (a[index] == b[index] && a[index] != 0; index += 1) {}
    if (a[index] > b[index]) {
        return 1;
    } else if (a[index] < b[index]) {
        return -1;
    } else {
        return 0;
    };
}

pub fn toSliceConst(str: &const u8) -> []const u8 {
    return str[0...strlen(str)];
}

pub fn toSlice(str: &u8) -> []u8 {
    return str[0...strlen(str)];
}


/// A buffer that allocates memory and maintains a null byte at the end.
pub const Buffer0 = struct {
    list: List(u8),

    /// Must deinitialize with deinit.
    pub fn initEmpty(allocator: &Allocator) -> %Buffer0 {
        return initSize(allocator, 0);
    }

    /// Must deinitialize with deinit.
    pub fn initFromMem(allocator: &Allocator, m: []const u8) -> %Buffer0 {
        var self = %return initSize(allocator, m.len);
        mem.copy(u8, self.list.items, m);
        return self;
    }

    /// Must deinitialize with deinit.
    pub fn initFromCStr(allocator: &Allocator, s: &const u8) -> %Buffer0 {
        return Buffer0.initFromMem(allocator, s[0...strlen(s)]);
    }

    /// Must deinitialize with deinit.
    pub fn initFromOther(cbuf: &const Buffer0) -> %Buffer0 {
        return Buffer0.initFromMem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()]);
    }

    /// Must deinitialize with deinit.
    pub fn initFromSlice(other: &const Buffer0, start: usize, end: usize) -> %Buffer0 {
        return Buffer0.initFromMem(other.list.allocator, other.list.items[start...end]);
    }

    /// Must deinitialize with deinit.
    pub fn initSize(allocator: &Allocator, size: usize) -> %Buffer0 {
        var self = Buffer0 {
            .list = List(u8).init(allocator),
        };
        %return self.resize(size);
        return self;
    }

    pub fn deinit(self: &Buffer0) {
        self.list.deinit();
    }

    pub fn toSlice(self: &Buffer0) -> []u8 {
        return self.list.toSlice()[0...self.len()];
    }

    pub fn toSliceConst(self: &const Buffer0) -> []const u8 {
        return self.list.toSliceConst()[0...self.len()];
    }

    pub fn resize(self: &Buffer0, new_len: usize) -> %void {
        %return self.list.resize(new_len + 1);
        self.list.items[self.len()] = 0;
    }

    pub fn len(self: &const Buffer0) -> usize {
        return self.list.len - 1;
    }

    pub fn appendMem(self: &Buffer0, m: []const u8) -> %void {
        const old_len = self.len();
        %return self.resize(old_len + m.len);
        mem.copy(u8, self.list.toSlice()[old_len...], m);
    }

    pub fn appendOther(self: &Buffer0, other: &const Buffer0) -> %void {
        return self.appendMem(other.toSliceConst());
    }

    pub fn appendCStr(self: &Buffer0, s: &const u8) -> %void {
        self.appendMem(s[0...strlen(s)])
    }

    pub fn appendByte(self: &Buffer0, byte: u8) -> %void {
        %return self.resize(self.len() + 1);
        self.list.items[self.len() - 1] = byte;
    }

    pub fn eqlMem(self: &const Buffer0, m: []const u8) -> bool {
        if (self.len() != m.len) return false;
        return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal;
    }

    pub fn eqlCStr(self: &const Buffer0, s: &const u8) -> bool {
        self.eqlMem(s[0...strlen(s)])
    }

    pub fn eqlOther(self: &const Buffer0, other: &const Buffer0) -> bool {
        self.eqlMem(other.list.items[0...other.len()])
    }

    pub fn startsWithMem(self: &const Buffer0, m: []const u8) -> bool {
        if (self.len() < m.len) return false;
        return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal;
    }

    pub fn startsWithOther(self: &const Buffer0, other: &const Buffer0) -> bool {
        self.startsWithMem(other.list.items[0...other.len()])
    }

    pub fn startsWithCStr(self: &const Buffer0, s: &const u8) -> bool {
        self.startsWithMem(s[0...strlen(s)])
    }
};

test "simple Buffer0" {
    var buf = %%Buffer0.initEmpty(&debug.global_allocator);
    assert(buf.len() == 0);
    %%buf.appendCStr(c"hello");
    %%buf.appendByte(' ');
    %%buf.appendMem("world");
    assert(buf.eqlCStr(c"hello world"));
    assert(buf.eqlMem("hello world"));
    assert(mem.eql(u8, buf.toSliceConst(), "hello world"));

    var buf2 = %%Buffer0.initFromOther(&buf);
    assert(buf.eqlOther(&buf2));

    assert(buf.startsWithMem("hell"));
    assert(buf.startsWithCStr(c"hell"));

    %%buf2.resize(4);
    assert(buf.startsWithOther(&buf2));
}

test "cstr fns" {
    comptime testCStrFnsImpl();
    testCStrFnsImpl();
}

fn testCStrFnsImpl() {
    assert(cmp(c"aoeu", c"aoez") == -1);
    assert(len(c"123456789") == 9);
}