Skip to content
zit Artifact [170741c7e9]
Login

Artifact [170741c7e9]

Artifact 170741c7e943458cce499c2d6e08fc5704b4ee90cace427ae19134e53118a9fc:


//! Comptime repetition of a byte string, for the test vectors and fixed
//! fields that were written with array multiplication before Zig 0.17
//! removed that operator.

/// `s` repeated `n` times, as a sentinel-terminated comptime array.
pub inline fn repeat(comptime s: []const u8, comptime n: usize) *const [s.len * n:0]u8 {
    const out = comptime blk: {
        @setEvalBranchQuota(1000 + 64 * (s.len * n + 1));
        var o: [s.len * n:0]u8 = undefined;
        for (0..n) |i| @memcpy(o[i * s.len ..][0..s.len], s);
        o[s.len * n] = 0;
        break :blk o;
    };
    return &out;
}

test repeat {
    const std = @import("std");
    try std.testing.expectEqualStrings("ababab", repeat("ab", 3));
    try std.testing.expectEqualStrings("", repeat("x", 0));
    try std.testing.expectEqual(@as(usize, 40), repeat("0123456789", 4).len);
}