Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Function

sliceWeightedWithHash

Public Function
std.testing.Smith.sliceWeightedWithHash
pub fn sliceWeightedWithHash(
    s: *Smith,
    buf: []u8,
    len_weights: []const Weight,
    byte_weights: []const Weight,
    hash: u32,
) u32

Returns the length of the filled slice

It is asserted that buf.len fits within a u32

Params

  • s: *Smith
  • buf: []u8
  • len_weights: []const Weight
  • byte_weights: []const Weight
  • hash: u32

File

testing/Smith.zig:623
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
pub fn sliceWeightedWithHash(
    s: *Smith,
    buf: []u8,
    len_weights: []const Weight,
    byte_weights: []const Weight,
    hash: u32,
) u32 {
    @disableInstrumentation();
    checkWeights(byte_weights, 255);
    checkWeights(len_weights, @as(u32, @intCast(buf.len)));

    if (s.in) |*in| {
        const in_len = len: {
            if (in.len < 4) {
                @branchHint(.unlikely);
                in.* = &.{};
                break :len 0;
            }
            const len = std.mem.readInt(u32, in.*[0..4], .little);
            in.* = in.*[4..];
            break :len @min(len, in.len);
        };
        const out_len: u32 = if (weightsContain(in_len, len_weights))
            in_len
        else
            @intCast(len_weights[0].min);

        var present_weights: [256]bool = @splat(false);
        for (byte_weights) |w| {
            @memset(present_weights[@intCast(w.min)..@intCast(w.max + 1)], true);
        }
        const default: u8 = @intCast(byte_weights[0].min);

        const copy_len = @min(out_len, in_len);
        for (in.*[0..copy_len], buf[0..copy_len]) |i, *o| {
            o.* = if (present_weights[i]) i else default;
        }
        in.* = in.*[in_len..];
        @memset(buf[copy_len..], default);
        return out_len;
    } else if (builtin.fuzz) {
        @branchHint(.likely);
        return fuzz_abi.fuzzer_slice(
            bytesUid(hash),
            .fromSlice(buf),
            .fromSlice(len_weights),
            .fromSlice(byte_weights),
        );
    } else unreachable;
}