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...@@ -2130,7 +2130,7 @@ pub fn replaceScalar(comptime T: type, slice: []T, needle: T, replacement: T) vo
2130}2130}
21312131
2132/// Collapse consecutive duplicate elements into one entry.2132/// 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 {
2134 if (slice.len == 0) return 0;2134 if (slice.len == 0) return 0;
2135 var write_idx: usize = 1;2135 var write_idx: usize = 1;
2136 var read_idx: usize = 1;2136 var read_idx: usize = 1;
...@@ -2143,11 +2143,15 @@ pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) usize {...@@ -2143,11 +2143,15 @@ pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) usize {
2143 return write_idx;2143 return write_idx;
2144}2144}
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
2146fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void {2151fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void {
2147 const mutable = try std.testing.allocator.dupe(u8, str);2152 const mutable = try std.testing.allocator.dupe(u8, str);
2148 defer std.testing.allocator.free(mutable);2153 defer std.testing.allocator.free(mutable);
2149 const actual = mutable[0..collapseRepeats(u8, mutable, elem)];2154 testing.expect(std.mem.eql(u8, collapseRepeats(u8, mutable, elem), expected));
2150 testing.expect(std.mem.eql(u8, actual, expected));
2151}2155}
2152test "collapseRepeats" {2156test "collapseRepeats" {
2153 try testCollapseRepeats("", '/', "");2157 try testCollapseRepeats("", '/', "");
lib/std/os/windows.zig+1-1
...@@ -1786,7 +1786,7 @@ pub fn removeDotDirsSanitized(comptime T: type, path: []T) RemoveDotDirsError!us...@@ -1786,7 +1786,7 @@ pub fn removeDotDirsSanitized(comptime T: type, path: []T) RemoveDotDirsError!us
1786/// Returns the length of the new path.1786/// Returns the length of the new path.
1787pub fn normalizePath(comptime T: type, path: []T) RemoveDotDirsError!usize {1787pub fn normalizePath(comptime T: type, path: []T) RemoveDotDirsError!usize {
1788 mem.replaceScalar(T, path, '/', '\\');1788 mem.replaceScalar(T, path, '/', '\\');
1789 const new_len = mem.collapseRepeats(T, path, '\\');1789 const new_len = mem.collapseRepeatsLen(T, path, '\\');
17901790
1791 const prefix_len: usize = init: {1791 const prefix_len: usize = init: {
1792 if (new_len >= 1 and path[0] == '\\') break :init 1;1792 if (new_len >= 1 and path[0] == '\\') break :init 1;