authorgravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2021-01-06 23:55:33-07:00
committergravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2021-05-20 14:00:41-06:00
loga72ad61cd4b718dd1ce2bf8ba4a3404f8304648c
treead7bb93e4c6078e1eab4d00765c2dc6e81bce09c
parent666584067a97da6d39867ff0c57fcd3be2d50e96

have collapseRepeats return slice intead of just len


2 files changed, 8 insertions(+), 4 deletions(-)

lib/std/mem.zig+7-3
......@@ -2130,7 +2130,7 @@ pub fn replaceScalar(comptime T: type, slice: []T, needle: T, replacement: T) vo
21302130}
21312131
21322132/// Collapse consecutive duplicate elements into one entry.
2133pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) usize {
2133pub fn collapseRepeatsLen(comptime T: type, slice: []T, elem: T) usize {
21342134 if (slice.len == 0) return 0;
21352135 var write_idx: usize = 1;
21362136 var read_idx: usize = 1;
......@@ -2143,11 +2143,15 @@ pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) usize {
21432143 return write_idx;
21442144}
21452145
2146/// Collapse consecutive duplicate elements into one entry.
2147pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) []T {
2148 return slice[0 .. collapseRepeatsLen(T, slice, elem)];
2149}
2150
21462151fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void {
21472152 const mutable = try std.testing.allocator.dupe(u8, str);
21482153 defer std.testing.allocator.free(mutable);
2149 const actual = mutable[0..collapseRepeats(u8, mutable, elem)];
2150 testing.expect(std.mem.eql(u8, actual, expected));
2154 testing.expect(std.mem.eql(u8, collapseRepeats(u8, mutable, elem), expected));
21512155}
21522156test "collapseRepeats" {
21532157 try testCollapseRepeats("", '/', "");
lib/std/os/windows.zig+1-1
......@@ -1786,7 +1786,7 @@ pub fn removeDotDirsSanitized(comptime T: type, path: []T) RemoveDotDirsError!us
17861786/// Returns the length of the new path.
17871787pub fn normalizePath(comptime T: type, path: []T) RemoveDotDirsError!usize {
17881788 mem.replaceScalar(T, path, '/', '\\');
1789 const new_len = mem.collapseRepeats(T, path, '\\');
1789 const new_len = mem.collapseRepeatsLen(T, path, '\\');
17901790
17911791 const prefix_len: usize = init: {
17921792 if (new_len >= 1 and path[0] == '\\') break :init 1;