2PHDYU6PONLBOKJSOH6CWZORYN75GNXSX76A3XGE477QDIFXTH4QC
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);
}
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [
# pkgs.zig
pkgs.python311Packages.python
pkgs.python311Packages.setuptools
];
}
# 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},
)
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)]);
// 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];
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};
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);
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)) {
// 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)));
// }
// }
/// The maximum number of bytes required to store a 64-bit varint.
const MAX_BYTES = 10;
venv
build
dist
*.egg-info
*.so