mirror of
https://github.com/ziglang/zig.git
synced 2024-11-26 23:22:44 +00:00
std.RingBuffer: use sliceAt/sliceFirst in read*AssumeLength
This commit is contained in:
parent
d942096a44
commit
6fe2b40778
@ -150,12 +150,8 @@ pub fn readFirst(self: *RingBuffer, dest: []u8, length: usize) Error!void {
|
|||||||
/// Uses memcpy and so `dest` must not overlap ring buffer data.
|
/// Uses memcpy and so `dest` must not overlap ring buffer data.
|
||||||
pub fn readFirstAssumeLength(self: *RingBuffer, dest: []u8, length: usize) void {
|
pub fn readFirstAssumeLength(self: *RingBuffer, dest: []u8, length: usize) void {
|
||||||
assert(length <= self.len() and length <= dest.len);
|
assert(length <= self.len() and length <= dest.len);
|
||||||
const data_start = self.mask(self.read_index);
|
const slice = self.sliceAt(self.read_index, length);
|
||||||
const part1_data_end = @min(self.data.len, data_start + length);
|
slice.copyTo(dest);
|
||||||
const part1_len = part1_data_end - data_start;
|
|
||||||
const part2_len = length - part1_len;
|
|
||||||
@memcpy(dest[0..part1_len], self.data[data_start..part1_data_end]);
|
|
||||||
@memcpy(dest[part1_len..length], self.data[0..part2_len]);
|
|
||||||
self.read_index = self.mask2(self.read_index + length);
|
self.read_index = self.mask2(self.read_index + length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,13 +170,12 @@ pub fn readLast(self: *RingBuffer, dest: []u8, length: usize) Error!void {
|
|||||||
/// Reduces write index by `length`.
|
/// Reduces write index by `length`.
|
||||||
pub fn readLastAssumeLength(self: *RingBuffer, dest: []u8, length: usize) void {
|
pub fn readLastAssumeLength(self: *RingBuffer, dest: []u8, length: usize) void {
|
||||||
assert(length <= self.len() and length <= dest.len);
|
assert(length <= self.len() and length <= dest.len);
|
||||||
const data_start = self.mask(self.write_index + self.data.len - length);
|
const slice = self.sliceLast(length);
|
||||||
const part1_data_end = @min(self.data.len, data_start + length);
|
slice.copyTo(dest);
|
||||||
const part1_len = part1_data_end - data_start;
|
self.write_index = if (self.write_index >= self.data.len)
|
||||||
const part2_len = length - part1_len;
|
self.write_index - length
|
||||||
@memcpy(dest[0..part1_len], self.data[data_start..part1_data_end]);
|
else
|
||||||
@memcpy(dest[part1_len..length], self.data[0..part2_len]);
|
self.mask(self.write_index + self.data.len - length);
|
||||||
self.write_index = if (self.write_index >= self.data.len) self.write_index - length else data_start;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the ring buffer is empty and `false` otherwise.
|
/// Returns `true` if the ring buffer is empty and `false` otherwise.
|
||||||
@ -206,6 +201,12 @@ pub fn len(self: RingBuffer) usize {
|
|||||||
pub const Slice = struct {
|
pub const Slice = struct {
|
||||||
first: []u8,
|
first: []u8,
|
||||||
second: []u8,
|
second: []u8,
|
||||||
|
|
||||||
|
/// Copy data from `self` into `dest`
|
||||||
|
pub fn copyTo(self: Slice, dest: []u8) void {
|
||||||
|
@memcpy(dest[0..self.first.len], self.first);
|
||||||
|
@memcpy(dest[self.first.len..][0..self.second.len], self.second);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Returns a `Slice` for the region of the ring buffer starting at
|
/// Returns a `Slice` for the region of the ring buffer starting at
|
||||||
|
Loading…
Reference in New Issue
Block a user