Skip to content

Commit f90bc87

Browse files
committed
have collapseRepeats return slice intead of just len
1 parent 7616b13 commit f90bc87

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lib/std/mem.zig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,7 @@ pub fn replaceScalar(comptime T: type, slice: []T, needle: T, replacement: T) vo
18391839
}
18401840

18411841
/// Collapse consecutive duplicate elements into one entry.
1842-
pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) usize {
1842+
pub fn collapseRepeatsLen(comptime T: type, slice: []T, elem: T) usize {
18431843
if (slice.len == 0) return 0;
18441844
var write_idx: usize = 1;
18451845
var read_idx: usize = 1;
@@ -1852,11 +1852,15 @@ pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) usize {
18521852
return write_idx;
18531853
}
18541854

1855+
/// Collapse consecutive duplicate elements into one entry.
1856+
pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) []T {
1857+
return slice[0 .. collapseRepeatsLen(T, slice, elem)];
1858+
}
1859+
18551860
fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void {
18561861
const mutable = try std.testing.allocator.dupe(u8, str);
18571862
defer std.testing.allocator.free(mutable);
1858-
const actual = mutable[0..collapseRepeats(u8, mutable, elem)];
1859-
testing.expect(std.mem.eql(u8, actual, expected));
1863+
testing.expect(std.mem.eql(u8, collapseRepeats(u8, mutable, elem), expected));
18601864
}
18611865
test "collapseRepeats" {
18621866
try testCollapseRepeats("", '/', "");

lib/std/os/windows.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ pub fn removeDotDirsSanitized(comptime T: type, path: []T) RemoveDotDirsError!us
15911591
/// Returns the length of the new path.
15921592
pub fn normalizePath(comptime T: type, path: []T) RemoveDotDirsError!usize {
15931593
mem.replaceScalar(T, path, '/', '\\');
1594-
const new_len = mem.collapseRepeats(T, path, '\\');
1594+
const new_len = mem.collapseRepeatsLen(T, path, '\\');
15951595

15961596
const prefix_len: usize = init: {
15971597
if (new_len >= 1 and path[0] == '\\') break :init 1;

0 commit comments

Comments
 (0)