Skip to content

Instantly share code, notes, and snippets.

@jrfondren
jrfondren / payment.dats
Last active October 5, 2018 21:00
ML-style datatypes in Zig and ATS
#include "share/atspre_staload.hats"
typedef amount = Nat
typedef ccn = $extype "int64_t"
typedef cvc = [i:nat | i < 9999] int(i)
datatype payment =
| card of (string, ccn, cvc, amount)
| paypal of (string, amount)
| cash of (amount)
@jrfondren
jrfondren / angle-exact.zig
Last active October 14, 2018 07:58
Translating 'elegant' C to Zig
const c = @cImport({
@cInclude("stdio.h");
});
pub fn main() void {
const str = c"\x00" ++ "*" ** 8;
var s = @ptrCast([*]const u8, &str[8]);
while (s[0] != 0) : (s -= 1) {
_ = c.puts(s);
}
@jrfondren
jrfondren / getr.zig
Last active November 6, 2018 20:20
unwanted struct initialization
// on macOS: zig build-exe getr.zig --library c --libc-include-dir /usr/local/Cellar/musl-cross/0.9.7_1/libexec/x86_64-linux-musl/include
const std = @import("std");
const c = @cImport({
@cInclude("sys/resource.h");
@cInclude("spawn.h");
});
const warn = std.debug.warn;
const report_time =
\\User time : {} s, {} us
@jrfondren
jrfondren / bits_types_struct_rusage_h.ads
Created November 11, 2018 02:51
sys/resource.h 's struct-of-unions completely foils gcc & zig header analysis
-- gcc -c -fdump-ada-spec "getr.h" -C # where getr.h is #include <sys/resource.h>
...
pragma Convention (C_Pass_By_Copy, anon1055_anon1068_union);
pragma Unchecked_Union (anon1055_anon1068_union);type anon1055_anon1069_union (discr : unsigned := 0) is record
case discr is
when 0 =>
ru_nivcsw : aliased long; -- /usr/include/x86_64-linux-gnu/bits/resource.h:281
when others =>
uu_ru_nivcsw_word : aliased x86_64_linux_gnu_bits_types_h.uu_syscall_slong_t; -- /usr/include/x86_64-linux-gnu/bits/resource.h:282
end case;
@jrfondren
jrfondren / dns1.zig
Last active November 12, 2018 10:40
trivial DNS
// zig build-exe dns1.zig --library c
const c = @cImport({
@cInclude("netdb.h");
@cInclude("stdio.h");
});
pub fn main() void {
if (c.gethostbyname(c"irc.freenode.net")) |host| {
if (host[0].h_addr_list) |res| {
if (res[0]) |first| {
@jrfondren
jrfondren / concatarr.zig
Created November 14, 2018 03:04
concatenating arrays at compile-time
const std = @import("std");
const warn = std.debug.warn;
pub fn main() void {
comptime var x1 = []f64{1, 2, 3};
comptime var x2 = x1 ++ x1; // error to assign x1 here
comptime var x3 = ([]f64{1, 2, 3})[0..];
x3 = x3 ++ x3; // no error
@jrfondren
jrfondren / concatarr.zig
Created November 14, 2018 04:02
more array concats
// from some C example with DEFINEs
const std = @import("std");
fn SQ(x:f64) f64 { return x*x; }
fn M0(x:f64,y:f64) u8 { if (SQ(x) + SQ(y)<4) { return 0; } else { return 0xe0; } }
fn M1(x:f64,y:f64,x0:f64,y0:f64) u8 { return if (SQ(x)+SQ(y)<4) M0(SQ(x)-SQ(y)+x0,2*(x)*(y)+y0) else 0xc0; }
fn M2(x:f64,y:f64,x0:f64,y0:f64) u8 { return if (SQ(x)+SQ(y)<4) M1(SQ(x)-SQ(y)+x0,2*(x)*(y)+y0,x0,y0) else 0xa0; }
fn M3(x:f64,y:f64,x0:f64,y0:f64) u8 { return if (SQ(x)+SQ(y)<4) M2(SQ(x)-SQ(y)+x0,2*(x)*(y)+y0,x0,y0) else 0x80; }
fn M4(x:f64,y:f64,x0:f64,y0:f64) u8 { return if (SQ(x)+SQ(y)<4) M3(SQ(x)-SQ(y)+x0,2*(x)*(y)+y0,x0,y0) else 0x60; }
diff -ru apr-1.4.8/threadproc/unix/proc.c apr-1.4.8.hack/threadproc/unix/proc.c
--- apr-1.4.8/threadproc/unix/proc.c 2011-11-07 12:33:35.000000000 -0600
+++ apr-1.4.8.hack/threadproc/unix/proc.c 2019-03-11 13:23:52.356000000 -0500
@@ -350,6 +350,8 @@
{
int i;
const char * const empty_envp[] = {NULL};
+ struct rlimit limits;
+ int is_suphp = 0;
@jrfondren
jrfondren / topsender.nim
Last active April 19, 2019 08:51
CountTable.sort
# release performance against 400MB log: 2.5s
import re, tables, strformat, sequtils, algorithm
var emailcounts = initCountTable[string]()
let re_sender = re"^(?:\S+ ){3,4}<= ([^@]+@(\S+))"
for line in "exim_mainlog".lines:
if line =~ re_sender:
emailcounts.inc matches[0]
@jrfondren
jrfondren / gist:c2bc601f28a5c9349bdab52aa3270354
Created April 20, 2019 09:39
code samples sized for nim-lang.org's sample code
# DNS query
import nativesockets
iterator items(ai: ptr AddrInfo): string =
var current = ai
while current != nil:
yield current.aiAddr.getAddrString
current = current.aiNext
let ips = getAddrInfo("www.nim-lang.org",