use SplitBackwardsIterator from stdlib
This commit is contained in:
parent
9f884b96f7
commit
ac71195144
@ -74,10 +74,10 @@ pub fn iterator(self: *const ErrCtx) mem.SplitIterator(u8) {
|
|||||||
return mem.split(u8, slice[0..last_byte], "\x00");
|
return mem.split(u8, slice[0..last_byte], "\x00");
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rev(self: *const ErrCtx) SplitIteratorRev(u8) {
|
pub fn rev(self: *const ErrCtx) mem.SplitBackwardsIterator(u8) {
|
||||||
const slice = self.buf.constSlice();
|
const slice = self.buf.constSlice();
|
||||||
if (slice.len == 0) {
|
if (slice.len == 0) {
|
||||||
return SplitIteratorRev(u8){
|
return mem.SplitBackwardsIterator(u8){
|
||||||
.buffer = slice,
|
.buffer = slice,
|
||||||
.index = null,
|
.index = null,
|
||||||
.delimiter = "\x00",
|
.delimiter = "\x00",
|
||||||
@ -85,7 +85,7 @@ pub fn rev(self: *const ErrCtx) SplitIteratorRev(u8) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const last_byte = if (slice[slice.len - 1] == 0) (slice.len - 1) else slice.len;
|
const last_byte = if (slice[slice.len - 1] == 0) (slice.len - 1) else slice.len;
|
||||||
return splitRev(u8, slice[0..last_byte], "\x00");
|
return mem.splitBackwards(u8, slice[0..last_byte], "\x00");
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unwrap(self: *const ErrCtx) BoundedArray(u8, capacity * 2) {
|
pub fn unwrap(self: *const ErrCtx) BoundedArray(u8, capacity * 2) {
|
||||||
@ -154,36 +154,3 @@ test "rev" {
|
|||||||
try testing.expectEqualStrings("yadda xx", it.next().?);
|
try testing.expectEqualStrings("yadda xx", it.next().?);
|
||||||
try testing.expectEqual(it.next(), null);
|
try testing.expectEqual(it.next(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// copied form https://github.com/ziglang/zig/pull/11908
|
|
||||||
pub fn splitRev(comptime T: type, buffer: []const T, delimiter: []const T) SplitIteratorRev(T) {
|
|
||||||
assert(delimiter.len != 0);
|
|
||||||
return SplitIteratorRev(T){
|
|
||||||
.index = buffer.len,
|
|
||||||
.buffer = buffer,
|
|
||||||
.delimiter = delimiter,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn SplitIteratorRev(comptime T: type) type {
|
|
||||||
return struct {
|
|
||||||
buffer: []const T,
|
|
||||||
index: ?usize,
|
|
||||||
delimiter: []const T,
|
|
||||||
|
|
||||||
const Self = @This();
|
|
||||||
|
|
||||||
/// Returns a slice of the next field, or null if splitting is complete.
|
|
||||||
pub fn next(self: *Self) ?[]const T {
|
|
||||||
const end = self.index orelse return null;
|
|
||||||
const start = if (mem.lastIndexOf(T, self.buffer[0..end], self.delimiter)) |delim_start| blk: {
|
|
||||||
self.index = delim_start;
|
|
||||||
break :blk delim_start + self.delimiter.len;
|
|
||||||
} else blk: {
|
|
||||||
self.index = null;
|
|
||||||
break :blk 0;
|
|
||||||
};
|
|
||||||
return self.buffer[start..end];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user