Skip to content

Instantly share code, notes, and snippets.

View silentbicycle's full-sized avatar

Scott Vokes silentbicycle

  • Fastly (@fastly)
  • Grand Rapids, MI
View GitHub Profile
@silentbicycle
silentbicycle / gist:3176678
Created July 25, 2012 15:10
Generate appropriate input for testing run-length encoding
-- Generate a random array of bytes, with some redundancy (via sorting).
function gen_int_array(limit)
limit = limit or 10000
local ri = lunatest.random_int
local length = ri(1, limit)
local ints = {}
for i=1,length do
-- keep them small, to increase duplication
ints[i] = ri(0, 2^8 - 1)
end
@silentbicycle
silentbicycle / gist:3176689
Created July 25, 2012 15:12
run-length encoding
-- Use literal zero for escape before compression data
local esc = 0
-- Run-length-encode an array of ints.
function encode(ints)
local res, i, ct = {}, 1, 1 -- result, index, count
while i <= #ints do
local v = ints[i]
while ints[i+1] == v do
ct = ct + 1
@silentbicycle
silentbicycle / shift.py
Created January 17, 2015 22:07
shift-and and shift-or in python
# shift-and / shift-or
def to_bin(n, width=32):
"Pad a binary number to WIDTH bits wide"
s = bin(n).replace("0b", "")
return (("%0" + str(width) + "d") % int(s))
# pg 20
def shift_and(pattern, text, trace=False):
m = len(pattern)
@silentbicycle
silentbicycle / space_padness.c
Last active April 19, 2021 17:33
uninitialized memory caused by struct padding
$ cat space_padness.c
/*
* Copyright (c) 2016 Scott Vokes <vokes.s@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
@silentbicycle
silentbicycle / handwarmer.c
Created January 2, 2014 17:53
handwarmer.c
/*
* Copyright (c) 2014 Scott Vokes <vokes.s@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
@silentbicycle
silentbicycle / array.lua
Created May 12, 2013 16:11
quick draft of an array/vector library for lua (mostly as an example of metatables)
local NONE = setmetatable({}, {['__tostring'] = function() return "NONE" end})
local Array = {
['__newindex'] = function(a, k, v)
if type(k) ~= "number" then error("bad index") end
if k < 1 then error("out of bounds") end
if a._lim ~= NONE and k <= a._lim then
a._v[k] = v
elseif a._lim == NONE then
if k > a._len then
@silentbicycle
silentbicycle / linear-time-indexing-sort.c
Last active January 3, 2016 01:29
simple linear-time sorting algorithm, using a flattened linked-list index
/*
* Copyright (c) 2014 Scott Vokes <vokes.s@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
@silentbicycle
silentbicycle / gist:7853896
Last active December 30, 2015 16:19
Setlist for The Mountain Goats at the Ladies Literary Club, Grand Rapids, MI 2009-11-04
Setlist for The Mountain Goats at the Ladies Literary Club,
Grand Rapids, MI, 2009-11-04
https://dl.dropboxusercontent.com/u/76615635/tmg-2009-11-04-GRMI-LLC.mp3
Handball
Old College Try
Psalms 40:2
Has Thou Considered the Tetrapod
Genesis 3:23
Genesis 30:3
@silentbicycle
silentbicycle / pirate.cow
Created September 9, 2013 22:02
Pirate cow, for cowsay(1). Goes in /usr/local/share/cows/ or wherever your cowsay install puts it.
$eye = chop($eyes);
$the_cow = <<"EOC";
$thoughts A__^
$thoughts ($eye#)\\_______
(__)\\ )\\/\\
$tongue ||----w |
J| |V
EOC
@silentbicycle
silentbicycle / rfcify
Created August 12, 2013 18:47
If you MUST sound like an IETF RFC (using cat is RECOMMENDED)
#!/bin/sh
sed -e "s/must/MUST/" \
-e "s/must not/MUST NOT/" \
-e "s/required/REQUIRED/" \
-e "s/shall/SHALL/" \
-e "s/shall not/SHALL NOT/" \
-e "s/should/SHOULD/" \
-e "s/should not/SHOULD NOT/" \
-e "s/recommended/RECOMMENDED/" \
-e "s/may/MAY/" \