authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2023-07-27 12:57:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-18 20:42:59-07:00
logc139b9d4ad68d1cb5e2c489891493232a11db76c
tree402540e5c920f3b86f334ea246a2f6c8ac2875bf
parentf6a6cdbba372414e441bbcea8c680e71d12c4a9a

path.ComponentIterator: Add peekNext and peekPrevious functions


1 files changed, 20 insertions(+), 8 deletions(-)

lib/std/fs/path.zig+20-8
...@@ -1507,6 +1507,14 @@ pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type {...@@ -1507,6 +1507,14 @@ pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type {
1507 /// For example, if the path is `/a/b/c` and the most recently returned component1507 /// For example, if the path is `/a/b/c` and the most recently returned component
1508 /// is `b`, then this will return the `c` component.1508 /// is `b`, then this will return the `c` component.
1509 pub fn next(self: *Self) ?Component {1509 pub fn next(self: *Self) ?Component {
1510 const peek_result = self.peekNext() orelse return null;
1511 self.start_index = peek_result.path.len - peek_result.name.len;
1512 self.end_index = peek_result.path.len;
1513 return peek_result;
1514 }
1515
1516 /// Like `next`, but does not modify the iterator state.
1517 pub fn peekNext(self: Self) ?Component {
1510 var start_index = self.end_index;1518 var start_index = self.end_index;
1511 while (start_index < self.path.len and path_type.isSep(T, self.path[start_index])) {1519 while (start_index < self.path.len and path_type.isSep(T, self.path[start_index])) {
1512 start_index += 1;1520 start_index += 1;
...@@ -1516,11 +1524,9 @@ pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type {...@@ -1516,11 +1524,9 @@ pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type {
1516 end_index += 1;1524 end_index += 1;
1517 }1525 }
1518 if (start_index == end_index) return null;1526 if (start_index == end_index) return null;
1519 self.start_index = start_index;
1520 self.end_index = end_index;
1521 return .{1527 return .{
1522 .name = self.path[self.start_index..self.end_index],1528 .name = self.path[start_index..end_index],
1523 .path = self.path[0..self.end_index],1529 .path = self.path[0..end_index],
1524 };1530 };
1525 }1531 }
15261532
...@@ -1529,6 +1535,14 @@ pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type {...@@ -1529,6 +1535,14 @@ pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type {
1529 /// For example, if the path is `/a/b/c` and the most recently returned component1535 /// For example, if the path is `/a/b/c` and the most recently returned component
1530 /// is `b`, then this will return the `a` component.1536 /// is `b`, then this will return the `a` component.
1531 pub fn previous(self: *Self) ?Component {1537 pub fn previous(self: *Self) ?Component {
1538 const peek_result = self.peekPrevious() orelse return null;
1539 self.start_index = peek_result.path.len - peek_result.name.len;
1540 self.end_index = peek_result.path.len;
1541 return peek_result;
1542 }
1543
1544 /// Like `previous`, but does not modify the iterator state.
1545 pub fn peekPrevious(self: Self) ?Component {
1532 var end_index = self.start_index;1546 var end_index = self.start_index;
1533 while (true) {1547 while (true) {
1534 if (end_index == self.root_end_index) return null;1548 if (end_index == self.root_end_index) return null;
...@@ -1542,11 +1556,9 @@ pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type {...@@ -1542,11 +1556,9 @@ pub fn ComponentIterator(comptime path_type: PathType, comptime T: type) type {
1542 start_index -= 1;1556 start_index -= 1;
1543 }1557 }
1544 if (start_index == end_index) return null;1558 if (start_index == end_index) return null;
1545 self.start_index = start_index;
1546 self.end_index = end_index;
1547 return .{1559 return .{
1548 .name = self.path[self.start_index..self.end_index],1560 .name = self.path[start_index..end_index],
1549 .path = self.path[0..self.end_index],1561 .path = self.path[0..end_index],
1550 };1562 };
1551 }1563 }
1552 };1564 };