solved varint-extension
[?]
Jun 15, 2023, 4:48 AM
2PHDYU6PONLBOKJSOH6CWZORYN75GNXSX76A3XGE477QDIFXTH4QCDependencies
- [2]
H3JTOAUEWIP: varint-extension - [3]
BW2CMZHKWIP: solving protobuf-varint - [*]
32TAVAVCinit zig project
Change contents
- file deletion: build.zig
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.zigpkgs.python311Packages.pythonpkgs.python311Packages.setuptools];} - file addition: setup.py[2.6]
# based on https://github.com/adamserafini/zaml/blob/27b2d54ffb39aace5d5d58f0aa75396c3e6fe84d/setup.pyimport osfrom setuptools import setup, Extensionfrom setuptools.command.build_ext import build_extclass ZigBuilder(build_ext):def build_extension(self, ext):assert len(ext.sources) == 2deps = [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_sourcefor 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
- replacement in zig/src/computer-systems/intro-to-c/varint-extension/pyvarint.zig at line 16
) callconv(.C) [*]py.PyObject {) callconv(.C) ?[*]py.PyObject { - replacement in zig/src/computer-systems/intro-to-c/varint-extension/pyvarint.zig at line 20
if (!py.PyArg_ParseTuple(args, "K", &cvalue_K)) return null;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
@as(py.Py_ssize_t, result_vbytes.len),@intCast(py.Py_ssize_t, result_vbytes.len), - replacement in zig/src/computer-systems/intro-to-c/varint-extension/pyvarint.zig at line 38
) callconv(.C) [*]py.PyObject {) callconv(.C) ?[*]py.PyObject { - replacement in zig/src/computer-systems/intro-to-c/varint-extension/pyvarint.zig at line 43
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]);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
const result = varint.decode(value) orelse return null;const result = varint.decode(value) catch return null; - replacement in zig/src/computer-systems/intro-to-c/varint-extension/pyvarint.zig at line 59
return @truncate(@TypeOf(value), @truncate(T, value)) != value;return @truncate(@TypeOf(value), @truncate(T, value)) == value; - replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 14
const VarintByte = packed struct {pub const VarintByte = packed struct { - replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 22
var buffer: [10]VarintByte = undefined;var buffer: [MAX_BYTES]VarintByte = undefined; - replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 27
.{ std.math.maxInt(u64), [_]u8{0xFF} ** 9 ++ [_]u8{0x01} },.{ 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
pub fn encode(value: u64, buffer: *[10]VarintByte) []VarintByte {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
// 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
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];// 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
const result = try decode(@ptrCast([]const VarintByte, input));try std.testing.expectEqual(expt_result, result);}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
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
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
var i: usize = inline for (buffer, 0..10) |item, j| {if (!item.has_more and (j < 9 or item.value <= 1)) {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
} else {return DecodeError.NoTermination;};} else unreachable; - replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 111
var bytes_rev = std.mem.reverseIterator(buffer[0..i]);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
result = result << 7;result = result | item.value;result = (result << 7) | item.value; - replacement in zig/src/computer-systems/bits-and-bytes/protobuf-varint.zig at line 118
// 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]
venvbuilddist*.egg-info*.so