Skip to content

Instantly share code, notes, and snippets.


Micha Mettke vurtun

View GitHub Profile
View API

API Design: Builder APIs (October-2020)

Some time has past (three years!) since I last wrote about API specifically about coroutines style APIs so I thought why not write another one about a different API type I encounter relatively often. The builder API.

Now first let me take a step back and put this into 20,000 feet view on where builder APIs are located in the grant scheme. In general everything in computing is separated into input, processing and finally output. In its most basic form I am currently typing on my keyboard. All pressed keys are processed from the OS up to the browser I am writing this in and finally rendered and displayed on the screen as output. Of course this example is very user centric

View hash.c
#include <emmintrin.h>
#include <wmmintrin.h>
static const unsigned char hash_default_seed[16] = {
0x32, 0x43, 0xF6, 0xA8, 0x88, 0x5A, 0x30, 0x8D,
0x31, 0x31, 0x98, 0xA2, 0xE0, 0x37, 0x07, 0x34};
#define hash128eq(v0, v1) (_mm_movemask_epi8(_mm_cmpeq_epi8(v0, v1)) == 0xffff)
View lay.c
// gui_lay_row(ctx, 3, (int[]) { 30, -90, -1 }, 0);
/* Layout */
enum gui_lay_dir {
struct gui_lay_sol {
int fix_siz;
int fix_cnt;
View ui.c
struct gui_bnd {int min, mid, max, ext;};
struct gui_box {
struct gui_bnd x;
struct gui_bnd y;
#define gui_box(x,y,w,h) (struct gui_box){{x,x+(w>>1),x+w,w},{y,y+(h>>1),y+h,h}}
#define gui_min_max(a,b) (struct gui_bnd){a,a+((b-a)>>1),b,b-a}
#define gui_min_ext(m,e) (struct gui_bnd){m,m+(e>>1),m+e,e}
#define gui_max_ext(m,e) (struct gui_bnd){m-e,m-(e>>1),m,e}
#define gui_mid_min(c,m) (struct gui_bnd){m,c,m+c-m,(m-c)<<1}
View prng.c
static unsigned
wang_hash(unsigned u)
u = ((u ^ 61) ^ (u >> 16)) * 9;
u = (u ^ (u >> 4)) * 0x27d4eb2d;
return u ^ (u >> 15);
static unsigned
rand_lcg(unsigned *rng)
View utf.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
static const char*
utf_dec(unsigned *dst, const char *p, const char *e)
View fuzzy_match.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
static int
str_fzy(const char *str, const char *ptn)
const char *pat = ptn;
vurtun / zoom.c
Last active Jan 7, 2020
Zoom to point
View zoom.c
static float
scalbnf(float x, int n)
union {float f; unsigned i;} u;
float y = x;
if (n > 127) {
y *= 0x1p127f;
n -= 127;
if (n > 127) {
y *= 0x1p127f;
vurtun / x11_clipboard.c
Last active Dec 15, 2019
X11 clipboard
View x11_clipboard.c
static char*
sys_clip_get(struct sys *s, Atom selection, Atom target)
struct sys_x11 *x11 = s->platform;
/* blocking wait for clipboard data */
XEvent notify;
XConvertSelection(x11->dpy, selection, target, selection, x11->helper, CurrentTime);
while (!XCheckTypedWindowEvent(x11->dpy, x11->helper, SelectionNotify, &notify)) {
vurtun / math.c
Last active Jul 17, 2020
Linear Algebra
View math.c
#include <math.h>
#define op(r,e,a,p,b,i,s) ((r) e (a) p ((b) i (s)))
#define lerp(r,a,b,t) ((r)=(a)+(t)*((b)-(a)))
#define rad(x) ((x)*3.141592653f/180.0f)
#define op3(r,e,a,p,b,i,s)\
do{op((r)[0],e,(a)[0],p,(b)[0],i,s), op((r)[1],e,(a)[1],p,(b)[1],i,s),\
op((r)[2],e,(a)[2],p,(b)[2],i,s);} while(0)
#define op3s(r,e,a,p,s)\
do{op((r)[0],e,(0),+,(a)[0],p,s), op((r)[1],e,(0),+,(a)[1],p,s),\
You can’t perform that action at this time.