Repository navigation
Expand file tree
/
Copy pathStackReader.zig
More file actions
65 lines (56 loc) · 1.71 KB
/
Copy pathStackReader.zig
File metadata and controls
65 lines (56 loc) · 1.71 KB
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
const StackReader = @This();
buffer: []const u8 = "",
offset: *usize,
message_start: *usize,
pub fn markMessageStart(this: @This()) void {
this.message_start.* = this.offset.*;
}
pub fn ensureLength(this: @This(), length: usize) bool {
return this.buffer.len >= (this.offset.* + length);
}
pub fn init(buffer: []const u8, offset: *usize, message_start: *usize) NewReader(StackReader) {
return .{
.wrapped = .{
.buffer = buffer,
.offset = offset,
.message_start = message_start,
},
};
}
pub fn peek(this: StackReader) []const u8 {
return this.buffer[this.offset.*..];
}
pub fn skip(this: StackReader, count: usize) void {
if (this.offset.* + count > this.buffer.len) {
this.offset.* = this.buffer.len;
return;
}
this.offset.* += count;
}
pub fn ensureCapacity(this: StackReader, count: usize) bool {
return this.buffer.len >= (this.offset.* + count);
}
pub fn read(this: StackReader, count: usize) AnyPostgresError!Data {
const offset = this.offset.*;
if (!this.ensureCapacity(count)) {
return error.ShortRead;
}
this.skip(count);
return Data{
.temporary = this.buffer[offset..this.offset.*],
};
}
pub fn readZ(this: StackReader) AnyPostgresError!Data {
const remaining = this.peek();
if (bun.strings.indexOfChar(remaining, 0)) |zero| {
this.skip(zero + 1);
return Data{
.temporary = remaining[0..zero],
};
}
return error.ShortRead;
}
const bun = @import("bun");
const AnyPostgresError = @import("../AnyPostgresError.zig").AnyPostgresError;
const Data = @import("../../shared/Data.zig").Data;
const NewReader = @import("./NewReader.zig").NewReader;