Skip to content

Instantly share code, notes, and snippets.

daurnimator

View GitHub Profile
@daurnimator
daurnimator / shadow.zig
Created Apr 15, 2019
zig struct field vs method shadowing
View shadow.zig
const std = @import("std");
const A = struct {
pub foo: i32,
pub fn foo() void {
}
};
test "shadowing" {
std.debug.warn("{}", @typeName(@typeOf(A.foo))); // fn() void
@daurnimator
daurnimator / http_header.zig
Last active Apr 12, 2019
http header object for zig
View http_header.zig
// HTTP Header data structure/type
// Based on lua-http's http.header module
//
// Design criteria:
// - the same header field is allowed more than once
// - must be able to fetch seperate occurences (important for some headers e.g. Set-Cookie)
// - optionally available as comma seperated list
// - http2 adds flag to headers that they should never be indexed
// - header order should be recoverable
//
@daurnimator
daurnimator / mylib.zig
Last active Apr 21, 2019
Example lua library in zig
View mylib.zig
// test with:
// zig test --library c --library lua -isystem /usr/include/ -L/usr/lib src/mylib.zig
//
// build with:
// zig build-lib -dynamic --library c -isystem /usr/include/ src/mylib.zig
// mv libmylib.so.0.0.0 mylib.so
//
// run example:
// lua -e 'mylib=require"mylib"; mylib.foo()'
@daurnimator
daurnimator / llvm.diff
Last active Mar 21, 2019
llvm 8 PKGBUILD diff
View llvm.diff
diff --git a/trunk/PKGBUILD b/trunk/PKGBUILD
index d6e630c..b2c9672 100644
--- a/trunk/PKGBUILD
+++ b/trunk/PKGBUILD
@@ -2,23 +2,20 @@
# Contributor: Jan "heftig" Steffens <jan.steffens@gmail.com>
pkgname=('llvm' 'llvm-libs' 'llvm-ocaml')
-pkgver=7.0.1
-pkgrel=2
@daurnimator
daurnimator / tobin.lua
Created Feb 3, 2019
lua function for showing binary representation of a string
View tobin.lua
local function tobin(x)
local t = {}
for i=0,#x-1 do
local c = x:byte(i+1)
for j=0,7 do
t[i*8+j+1] = (c & 0x80) ~= 0 and "1" or "0"
c = c << 1
end
end
return table.concat(t)
View stringToEnum.zig
const std = @import("std");
fn stringToEnum(comptime T: type, str: []const u8) ?T {
inline for (@typeInfo(T).Enum.fields) |enumField| {
if (std.mem.eql(u8, str, enumField.name)) {
return @intToEnum(T, @intCast(@TagType(T), enumField.value));
}
}
return null;
}
@daurnimator
daurnimator / tojson.zig
Last active Nov 24, 2018
Zig struct => JSON serializer
View tojson.zig
const std = @import("std");
const Buffer = std.Buffer;
const mem = std.mem;
const assert = std.debug.assert;
const TypeInfo = @import("builtin").TypeInfo;
const TypeId = @import("builtin").TypeId;
fn toJSONacc(buf: *Buffer, value: var) !void {
switch (@typeInfo(@typeOf(value))) {
TypeId.Bool => {
@daurnimator
daurnimator / thing.h
Last active Nov 18, 2018
Attempt at using zig's export to C feature
View thing.h
#ifndef THING_H
#define THING_H
#include <stdint.h>
#ifdef __cplusplus
#define THING_EXTERN_C extern "C"
#else
#define THING_EXTERN_C
#endif
@daurnimator
daurnimator / cursed_mandelbrot.c
Last active Nov 14, 2018 — forked from DavidBuchanan314/cursed_mandelbrot.c
Compile-time mandelbrot in pure C and Zig. Outputs a PGM image file to stdout.
View cursed_mandelbrot.c
#include <stdio.h>
#define SQ(x) (x)*(x)
#define M0(x,y) SQ(x)+SQ(y)<4?0:0xe0
#define M1(x,y,x0,y0) (SQ(x)+SQ(y)<4)?M0(SQ(x)-SQ(y)+(x0),2*(x)*(y)+(y0)):0xc0
#define M2(x,y,x0,y0) (SQ(x)+SQ(y)<4)?M1(SQ(x)-SQ(y)+(x0),2*(x)*(y)+(y0),x0,y0):0xa0
#define M3(x,y,x0,y0) (SQ(x)+SQ(y)<4)?M2(SQ(x)-SQ(y)+(x0),2*(x)*(y)+(y0),x0,y0):0x80
#define M4(x,y,x0,y0) (SQ(x)+SQ(y)<4)?M3(SQ(x)-SQ(y)+(x0),2*(x)*(y)+(y0),x0,y0):0x60
#define M5(x,y,x0,y0) (SQ(x)+SQ(y)<4)?M4(SQ(x)-SQ(y)+(x0),2*(x)*(y)+(y0),x0,y0):0x40
@daurnimator
daurnimator / doh_client.lua
Last active Aug 28, 2018
DNS over HTTP with lua-http
View doh_client.lua
#!/usr/bin/env lua
local basexx = require "basexx"
local cq_auxlib = require "cqueues.auxlib"
local cq_packet = require "cqueues.dns.packet"
local http_request = require "http.request"
local function doh_request(uri, method, name, type, class)
local req = http_request.new_from_uri(uri)
req.headers:upsert("accept", "application/dns-message")
You can’t perform that action at this time.