Skip to content

Instantly share code, notes, and snippets.

View z-rui's full-sized avatar

张睿 Zhang Rui z-rui

  • Los Gatos, CA
  • 05:05 (UTC -07:00)
View GitHub Profile
@z-rui
z-rui / bf.awk
Created September 19, 2018 12:19
Brainf*** interpreter in AWK
BEGIN {
FS = ""
AMOD = 65536
VMOD = 256
pc = 0
for (ch = 0; ch < VMOD; ch++)
ORD[sprintf("%c", ch)] = ch
}
{
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<alias>
<family>sans-serif</family>
<prefer>
<family>Source Sans Pro</family>
<family>Source Han Sans SC</family>
</prefer>
</alias>
@z-rui
z-rui / hash.c
Created November 2, 2018 19:59
Open addressing, linear probing hash table
#include "hash.h"
#include <assert.h>
void **hash_find(void **base, size_t n, void *key,
size_t hash, int (*equal)(const void *, const void *))
{
void **p, **q, **end;
p = q = base + hash % n;
end = base + n;
@z-rui
z-rui / .tmux.conf
Created November 17, 2018 18:35
Tmux configuration
unbind C-b
bind-key C-a send-prefix
set -g prefix C-a
set -g mouse on
set -g set-titles on
set -g status off
set -g escape-time 100
setw -g mode-keys vi
@z-rui
z-rui / makepdf.sh
Created July 14, 2019 04:16
Make a 16-page booklet
#!/bin/bash
size="3300x2550" # 11" x 8"
# size="3508x2480" # 297mm x 210mm
cvtopts="+append -adaptive-resize $size -gravity center -extent $size -set density 300 -unsharp 0x5+0.3+0"
if [[ ! -z $1 ]]; then
N=$1
elif [[ $PWD =~ FILE([0-9]+) ]]; then
@z-rui
z-rui / eexp.c
Created July 14, 2019 06:39
Expand "2[ab3[c]]" into "abcccabccc"
char*f(char*s){char*t;int n;for(t=s;*t;t++)if(*t==']')break;
else if(*t>>4==3){for(n=*(s=t++)-48;*t>>4==3;n=n*10+(*t++-48));
if(*t=='[')for(s=t+1;n--;t=f(s));else for(t--;s<=t;putchar(*s++));}
else putchar(*t);return t;}int main(int c,char**v){f(v[1]);}
@z-rui
z-rui / verb.tex
Created July 21, 2019 04:50
TeX verbatim typesetting
% Verbatim typesetting
\catcode`@=11
\def\uncatcodespecials{\begingroup
\def\do##1{\catcode`\noexpand##1=12 }%
\edef\next{\endgroup\dospecials}\next}
\let\verbfont=\tentt
\def\nohyphenation{\hyphenchar\font\m@ne}
@z-rui
z-rui / kmp.py
Created October 29, 2019 05:47
Knuth–Morris–Pratt
#!/usr/bin/python3
def make_pi(p):
n = len(p)
pi = [0] * n
i = 0
for j in range(1, n):
while i > 0 and p[i] != p[j]:
i = pi[i-1]
if p[i] == p[j]:
@z-rui
z-rui / parsegen.tex
Created March 15, 2020 01:10
Parse Generator
\input luaotfload.sty
\input luatypezh
\input zr/verb
\font\tenmib=cmmib10 \font\sevenmib=cmmib7 \font\fivemib=cmmib5
\baselineskip=14pt \everydisplay{\baselineskip=12pt\relax}
\verbatimindent=2em
% % %
#include <stdio.h>
#include <stdlib.h>
static void
hanoi(unsigned char n)
{
void *s[32];
char p[3] = "ABC", t;
if (n >= 32) n = 31;