mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 22:33:08 +00:00
have collapseRepeats return slice intead of just len
This commit is contained in:
parent
666584067a
commit
a72ad61cd4
@ -2130,7 +2130,7 @@ pub fn replaceScalar(comptime T: type, slice: []T, needle: T, replacement: T) vo
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Collapse consecutive duplicate elements into one entry.
|
/// Collapse consecutive duplicate elements into one entry.
|
||||||
pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) usize {
|
pub fn collapseRepeatsLen(comptime T: type, slice: []T, elem: T) usize {
|
||||||
if (slice.len == 0) return 0;
|
if (slice.len == 0) return 0;
|
||||||
var write_idx: usize = 1;
|
var write_idx: usize = 1;
|
||||||
var read_idx: usize = 1;
|
var read_idx: usize = 1;
|
||||||
@ -2143,11 +2143,15 @@ pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) usize {
|
|||||||
return write_idx;
|
return write_idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Collapse consecutive duplicate elements into one entry.
|
||||||
|
pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) []T {
|
||||||
|
return slice[0 .. collapseRepeatsLen(T, slice, elem)];
|
||||||
|
}
|
||||||
|
|
||||||
fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void {
|
fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void {
|
||||||
const mutable = try std.testing.allocator.dupe(u8, str);
|
const mutable = try std.testing.allocator.dupe(u8, str);
|
||||||
defer std.testing.allocator.free(mutable);
|
defer std.testing.allocator.free(mutable);
|
||||||
const actual = mutable[0..collapseRepeats(u8, mutable, elem)];
|
testing.expect(std.mem.eql(u8, collapseRepeats(u8, mutable, elem), expected));
|
||||||
testing.expect(std.mem.eql(u8, actual, expected));
|
|
||||||
}
|
}
|
||||||
test "collapseRepeats" {
|
test "collapseRepeats" {
|
||||||
try testCollapseRepeats("", '/', "");
|
try testCollapseRepeats("", '/', "");
|
||||||
|
|||||||
@ -1786,7 +1786,7 @@ pub fn removeDotDirsSanitized(comptime T: type, path: []T) RemoveDotDirsError!us
|
|||||||
/// Returns the length of the new path.
|
/// Returns the length of the new path.
|
||||||
pub fn normalizePath(comptime T: type, path: []T) RemoveDotDirsError!usize {
|
pub fn normalizePath(comptime T: type, path: []T) RemoveDotDirsError!usize {
|
||||||
mem.replaceScalar(T, path, '/', '\\');
|
mem.replaceScalar(T, path, '/', '\\');
|
||||||
const new_len = mem.collapseRepeats(T, path, '\\');
|
const new_len = mem.collapseRepeatsLen(T, path, '\\');
|
||||||
|
|
||||||
const prefix_len: usize = init: {
|
const prefix_len: usize = init: {
|
||||||
if (new_len >= 1 and path[0] == '\\') break :init 1;
|
if (new_len >= 1 and path[0] == '\\') break :init 1;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user