solved varint-extension

[?]
Jun 15, 2023, 4:48 AM
2PHDYU6PONLBOKJSOH6CWZORYN75GNXSX76A3XGE477QDIFXTH4QC

Dependencies

Change contents

  • file deletion: build.zig (----------)
    [2.6][2.5359:5392](),[2.5392][2.3013:3013]()
    const std = @import("std");
    // Although this function looks imperative, note that its job is to
    // declaratively construct a build graph that will be executed by an external
    // runner.
    pub fn build(b: *std.Build) void {
    // Standard target options allows the person running `zig build` to choose
    // what target to build for. Here we do not override the defaults, which
    // means any target is allowed, and the default is native. Other options
    // for restricting supported target set are available.
    const target = b.standardTargetOptions(.{});
    // Standard optimization options allow the person running `zig build` to select
    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
    // set a preferred release mode, allowing the user to decide how to optimize.
    const optimize = b.standardOptimizeOption(.{});
    const lib = b.addSharedLibrary(.{
    .name = "varint-extension",
    // In this case the main source file is merely a path, however, in more
    // complicated build scripts, this could be a generated file.
    .root_source_file = .{ .path = "varint-extension.zig" },
    .target = target,
    .optimize = optimize,
    });
    // Add protobuf varint Zig implementation as a dependency.
    const protobuf_varint = b.addModule("protobuf-varint", .{ .source_file = .{ .path = "../../bits-and-bytes/protobuf-varint.zig" } });
    lib.addModule("protobuf-varint", protobuf_varint);
    // This declares intent for the library to be installed into the standard
    // location when the user invokes the "install" step (the default step when
    // running `zig build`).
    b.installArtifact(lib);
    // Creates a step for unit testing. This only builds the test executable
    // but does not run it.
    const main_tests = b.addTest(.{
    .root_source_file = .{ .path = "src/main.zig" },
    .target = target,
    .optimize = optimize,
    });
    const run_main_tests = b.addRunArtifact(main_tests);
    // This creates a build step. It will be visible in the `zig build --help` menu,
    // and can be selected like this: `zig build test`
    // This will evaluate the `test` step rather than the default, which is "install".
    const test_step = b.step("test", "Run library tests");
    test_step.dependOn(&run_main_tests.step);
    }
  • file addition: shell.nix (----------)
    [2.6]
    { pkgs ? import <nixpkgs> {} }:
    pkgs.mkShell {
    buildInputs = [
    # pkgs.zig
    pkgs.python311Packages.python
    pkgs.python311Packages.setuptools
    ];
    }
  • file addition: setup.py (----------)
    [2.6]
    # based on https://github.com/adamserafini/zaml/blob/27b2d54ffb39aace5d5d58f0aa75396c3e6fe84d/setup.py
    import os
    from setuptools import setup, Extension
    from setuptools.command.build_ext import build_ext
    class ZigBuilder(build_ext):
    def build_extension(self, ext):
    assert len(ext.sources) == 2
    deps = [os.path.abspath(s) for s in ext.sources[1:]]
    deps_names = [os.path.splitext(os.path.basename(s))[0] for s in deps]
    if not os.path.exists(self.build_lib):
    os.makedirs(self.build_lib)
    self.spawn(
    [
    "zig",
    "build-lib",
    f"-femit-bin={self.get_ext_fullpath(ext.name)}",
    "-fallow-shlib-undefined",
    "-dynamic",
    *[f"-I{d}" for d in self.include_dirs],
    *[
    side_source
    for dep, dep_name in zip(deps, deps_names)
    for side_source in (
    "--mod",
    ":".join([dep_name, "", dep]),
    )
    ],
    "--deps",
    *deps_names,
    ext.sources[0],
    ]
    )
    setup(
    name="cvarint",
    ext_modules=[
    Extension(
    "cvarint",
    sources=[
    "pyvarint.zig",
    os.path.abspath("../../bits-and-bytes/protobuf-varint.zig"),
    ],
    )
    ],
    cmdclass={"build_ext": ZigBuilder},
    )
  • file move: varint-extension.zig (----------)pyvarint.zig (----------)
    [2.6]
    [2.36]
  • replacement in zig/src/computer-systems/intro-to-c/varint-extension/pyvarint.zig at line 16
    [2.437][2.437:469]()
    ) callconv(.C) [*]py.PyObject {
    [2.437]
    [2.469]
    ) callconv(.C) ?[*]py.PyObject {
  • replacement in zig/src/computer-systems/intro-to-c/varint-extension/pyvarint.zig at line 20
    [2.521][2.521:586]()
    if (!py.PyArg_ParseTuple(args, "K", &cvalue_K)) return null;
    [2.521]
    [2.586]
    if (py.PyArg_ParseTuple(args, "K", &cvalue_K) == 0) return null;
  • replacement in zig/src/computer-systems/intro-to-c/varint-extension/pyvarint.zig at line 31
    [2.944][2.944:991]()
    @as(py.Py_ssize_t, result_vbytes.len),
    [2.944]
    [2.991]
    @intCast(py.Py_ssize_t, result_vbytes.len),
  • replacement in zig/src/computer-systems/intro-to-c/varint-extension/pyvarint.zig at line 38
    [2.1074][2.1074:1106]()
    ) callconv(.C) [*]py.PyObject {
    [2.1074]
    [2.1106]
    ) callconv(.C) ?[*]py.PyObject {
  • replacement in zig/src/computer-systems/intro-to-c/varint-extension/pyvarint.zig at line 43
    [2.1208][2.1208:1404]()
    if (!py.PyArg_ParseTuple(args, "K", &cvalue_ptr, &cvalue_len)) return null;
    if (!cvalue_ptr) return null;
    const value = @ptrCast([]const varint.VarintByte, cvalue_ptr[0..cvalue_len]);
    [2.1208]
    [2.1404]
    if (py.PyArg_ParseTuple(args, "y#", &cvalue_ptr, &cvalue_len) == 0) return null;
    if (cvalue_ptr == null) return null;
    const value = @ptrCast([]const varint.VarintByte, cvalue_ptr[0..@intCast(usize, cvalue_len)]);
  • replacement in zig/src/computer-systems/intro-to-c/varint-extension/pyvarint.zig at line 47
    [2.1405][2.1405:1465]()
    const result = varint.decode(value) orelse return null;
    [2.1405]
    [2.1465]
    const result = varint.decode(value) catch return null;
  • replacement in zig/src/computer-systems/intro-to-c/varint-extension/pyvarint.zig at line 59
    [2.1790][2.1790:1858]()
    return @truncate(@TypeOf(value), @truncate(T, value)) != value;
    [2.1790]
    [2.1858]
    return @truncate(@TypeOf(value), @truncate(T, value)) == value;
  • replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 14
    [3.472][3.472:507]()
    const VarintByte = packed struct {
    [3.472]
    [3.507]
    pub const VarintByte = packed struct {
  • replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 22
    [3.584][3.584:628]()
    var buffer: [10]VarintByte = undefined;
    [3.584]
    [3.628]
    var buffer: [MAX_BYTES]VarintByte = undefined;
  • replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 27
    [3.744][3.744:812]()
    .{ std.math.maxInt(u64), [_]u8{0xFF} ** 9 ++ [_]u8{0x01} },
    [3.744]
    [3.812]
    .{ std.math.maxInt(u64), [_]u8{0xFF} ** (MAX_BYTES - 1) ++ [_]u8{0x01} },
  • replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 37
    [3.1038][3.1038:1104]()
    pub fn encode(value: u64, buffer: *[10]VarintByte) []VarintByte {
    [3.1038]
    [3.1104]
    pub fn encode(value: u64, buffer: *[MAX_BYTES]VarintByte) []VarintByte {
  • edit in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 52
    [3.1411]
    [3.1411]
    // TODO compiler bug!
    // test "decode literals" {
    // inline for (.{
    // .{ @as(u64, 0), [_]u8{0x00} },
    // .{ @as(u64, 1), [_]u8{0x01} },
    // .{ @as(u64, 150), [_]u8{ 0x96, 0x01 } },
    // .{ @as(u64, std.math.maxInt(u64)), [_]u8{0xFF} ** (MAX_BYTES - 1) ++ [_]u8{0x01} },
    // }) |vals| {
    // const expt_result = vals[0];
    // const input: []const u8 = &vals[1];
  • replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 64
    [3.1412][3.1412:1758]()
    test "decode literals" {
    inline for (.{
    .{ @as(u64, 0), [_]u8{0x00} },
    .{ @as(u64, 1), [_]u8{0x01} },
    .{ @as(u64, 150), [_]u8{ 0x96, 0x01 } },
    .{ @as(u64, std.math.maxInt(u64)), [_]u8{0xFF} ** 9 ++ [_]u8{0x01} },
    }) |vals| {
    const expt_result = vals[0];
    const input: []const u8 = &vals[1];
    [3.1412]
    [3.1758]
    // const result = try decode(@ptrCast([]const VarintByte, input));
    // try std.testing.expectEqual(expt_result, result);
    // }
    // }
    test "decode literal 0" {
    const expt_result = @as(u64, 0);
    const input: []const u8 = &[_]u8{0x00};
  • replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 72
    [3.1759][3.1759:1895]()
    const result = try decode(@ptrCast([]const VarintByte, input));
    try std.testing.expectEqual(expt_result, result);
    }
    [3.1759]
    [3.1895]
    const result = try decode(@ptrCast([]const VarintByte, input));
    try std.testing.expectEqual(expt_result, result);
    }
    test "decode literal 1" {
    const expt_result = @as(u64, 1);
    const input: []const u8 = &[_]u8{0x01};
    const result = try decode(@ptrCast([]const VarintByte, input));
    try std.testing.expectEqual(expt_result, result);
    }
    test "decode literal 150" {
    const expt_result = @as(u64, 150);
    const input: []const u8 = &[_]u8{ 0x96, 0x01 };
    const result = try decode(@ptrCast([]const VarintByte, input));
    try std.testing.expectEqual(expt_result, result);
  • edit in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 89
    [3.1897]
    [3.1897]
    test "decode literal max value" {
    const expt_result = @as(u64, std.math.maxInt(u64));
    const input: []const u8 = &([_]u8{0xFF} ** (MAX_BYTES - 1) ++ [_]u8{0x01});
  • edit in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 93
    [3.1898]
    [3.1898]
    const result = try decode(@ptrCast([]const VarintByte, input));
    try std.testing.expectEqual(expt_result, result);
    }
  • replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 100
    [2.5457][3.1990:2109](),[3.1990][3.1990:2109]()
    var i: usize = inline for (buffer, 0..10) |item, j| {
    if (!item.has_more and (j < 9 or item.value <= 1)) {
    [2.5457]
    [3.2109]
    var terminal_pos: usize = inline for (0..MAX_BYTES + 1) |j| {
    if (j >= MAX_BYTES or j >= buffer.len) {
    return DecodeError.NoTermination;
    }
    const item = buffer[j];
    if (!item.has_more and (j < MAX_BYTES - 1 or item.value <= 1)) {
  • replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 108
    [3.2140][3.2140:2202]()
    } else {
    return DecodeError.NoTermination;
    };
    [3.2140]
    [3.2202]
    } else unreachable;
  • replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 111
    [3.2228][3.2228:2287]()
    var bytes_rev = std.mem.reverseIterator(buffer[0..i]);
    [3.2228]
    [3.2287]
    var bytes_rev = std.mem.reverseIterator(buffer[0 .. terminal_pos + 1]);
  • replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 113
    [3.2325][3.2325:2393]()
    result = result << 7;
    result = result | item.value;
    [3.2325]
    [3.2393]
    result = (result << 7) | item.value;
  • replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 118
    [3.2421][3.2421:2653]()
    // test "encode-decode round trip" {
    // inline for ([_]u64{ 0, 1, 150, std.math.maxInt(u64) }) |n| {
    // var buffer: [10]u8 = undefined;
    // try std.testing.expectEqual(n, decode(encode(n, buffer)));
    // }
    // }
    [3.2421]
    /// The maximum number of bytes required to store a 64-bit varint.
    const MAX_BYTES = 10;
  • edit in zig/.gitignore at line 3
    [2.5468]
    venv
    build
    dist
    *.egg-info
    *.so