Skip to content

Instantly share code, notes, and snippets.

View geraintluff's full-sized avatar

Geraint geraintluff

View GitHub Profile
@geraintluff
geraintluff / json-simplified.js
Created February 20, 2024 10:26
Simple RegEx for slightly prettier JSON-ish output (unquoted keys, single-item objects/arrays on one line)
JSON.simplified = v => {
return JSON.stringify(v, null, '\t')
.replace(/(\n\t*)"([a-z][a-z0-9_]*)"/ig, '$1$2')
.replace(/(\n[\sa-z0-9_:]+)\{\n\t*(.*)\n\t+\}/ig, '$1{$2}')
.replace(/(\n[\sa-z0-9_:]+)\[\n\t*(.*)\n\t+\]/ig, '$1[$2]');
};
JSON.parseSimplified = t => {
return JSON.parse(t
.replace(/(\n[\sa-z0-9_:]+)([\[\{])([^\n])/ig, '$1$2\n$3')
.replace(/(\n\t*)([a-z][a-z0-9_]*):/ig, '$1"$2":')
@geraintluff
geraintluff / csv-writer.h
Last active January 25, 2022 13:56
Example of outputting CSV from C++, and picking it up with Python (Matplotlib)
#ifndef UTIL_CSV_WRITER
#define UTIL_CSV_WRITER
/* Optional helper class for outputting CSV data. Use like:
CsvWriter csv("out"); // no .csv suffix
// write individual values
csv.write("foo", "bar");
csv.line();
@geraintluff
geraintluff / unit-range-reciprocal.hpp
Last active June 6, 2022 10:56
A map from [0-1] to an arbitrary range, specified using low/mid/high values. Pulled out from some Signalsmith Audio internal code.
class UnitRangeMapReciprocal {
double vMin, vTopFactor, vBottomFactor;
public:
UnitRangeMapReciprocal() : UnitRangeMapReciprocal(0, 0.5, 1) {}
UnitRangeMapReciprocal(double min, double mid, double max) {
vMin = min;
double k = (mid - min)/(max - mid);
vTopFactor = max*k - min;
vBottomFactor = k - 1;
}
{
"https://twitter.com/every_peano/status/590740112625008641": {
"exact": 0
},
"https://twitter.com/every_peano/status/1058469466714132480": {
"relative": "https://twitter.com/every_peano/status/592203098388566016",
"N": 30900
},
"https://twitter.com/every_peano/status/1057001290117595139": {
"relative": "https://twitter.com/every_peano/status/1058469466714132480",
@geraintluff
geraintluff / ffmpeg-install.sh
Created March 30, 2016 10:55 — forked from clayton/ffmpeg-install.sh
Install FFMPEG on OS X with HomeBrew to convert Mp4 to WebM
# Installation
brew install ffmpeg --with-vpx --with-vorbis --with-libvorbis --with-vpx --with-vorbis --with-theora --with-libogg --with-libvorbis --with-gpl --with-version3 --with-nonfree --with-postproc --with-libaacplus --with-libass --with-libcelt --with-libfaac --with-libfdk-aac --with-libfreetype --with-libmp3lame --with-libopencore-amrnb --with-libopencore-amrwb --with-libopenjpeg --with-openssl --with-libopus --with-libschroedinger --with-libspeex --with-libtheora --with-libvo-aacenc --with-libvorbis --with-libvpx --with-libx264 --with-libxvid
# Easy Peasy
ffmpeg -i video.mp4 video.webm
@geraintluff
geraintluff / base64-web-safe.js
Created October 9, 2015 15:36
Convert base64 to and from web-safe variant
// Convert from normal to web-safe, strip trailing "="s
function webSafe64(base64) {
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
// Convert from web-safe to normal, add trailing "="s
function normal64(base64) {
return base64.replace(/\-/g, '+').replace(/_/g, '/') + '=='.substring(0, (3*base64.length)%4);
}
@geraintluff
geraintluff / README.md
Last active August 29, 2015 14:22
Your Unix machine is awaited in Valhalla - witness!

The shutdown messages on the Mac/Linux command line are baked into the /sbin/shutdown executable.

This replaces the messages with suitable Mad Max: Fury Road quotes. You can make your own, but since you're patching a binary file the replacements MUST, I repeat MUST be exactly the same number of characters as the original, or your computer will not ride eternal, shiny and chrome.

The two versions below are for a Ubuntu Linux variant, and Mac OS X Yosemite (both versions Work For Me on at least one machine).

@geraintluff
geraintluff / index.html
Created April 28, 2015 17:56
Page to generate self-decrypting web pages
<style>
body {
margin: 0;
padding: 0;
}
a {
display: block;
}
</style>
<div id="drop" style="width: 100%; height: 69%; margin: 0; padding-top: 30%; text-align: center; font-family: Tahoma, sans-serif;">
@geraintluff
geraintluff / data-url.txt
Created December 31, 2014 11:59
Small single HTML file that securely loads libraries (SHA256-verified). User can then drag files onto the page and they appear PGP-encrypted in a DropBox account.
data:text/html,%3Cbody%3E%3Cscript%3Efunction%20sha256(a)%7Bfunction%20b(a,b)%7Breturn%20a%3E%3E%3Eb%7Ca%3C%3C32-b%7Dfor(var%20c,d,e=Math.pow,f=e(2,32),g=%22length%22,h=%22%22,i=%5B%5D,j=8*a%5Bg%5D,k=%5B%5D,l=%5B%5D,m=0,n=%7B%7D,o=2;64%3Em;o++)if(!n%5Bo%5D)%7Bfor(c=0;313%3Ec;c+=o)n%5Bc%5D=o;k%5Bm%5D=e(o,.5)*f%7C0,l%5Bm++%5D=e(o,1/3)*f%7C0%7Dfor(a+=%22%5Cx80%22;a%5Bg%5D%2564-56;)a+=%22%5Cx00%22;for(c=0;c%3Ca%5Bg%5D;c++)%7Bif(d=a.charCodeAt(c),d%3E%3E8)return;i%5Bc%3E%3E2%5D%7C=d%3C%3C(3-c)%254*8%7Dfor(i%5Bi%5Bg%5D%5D=j/f%7C0,i%5Bi%5Bg%5D%5D=j,d=0;d%3Ci%5Bg%5D;)%7Bvar%20p=i.slice(d,d+=16),q=k;for(k=k.slice(0,8),c=0;64%3Ec;c++)%7Bvar%20r=p%5Bc-15%5D,s=p%5Bc-2%5D,t=k%5B0%5D,u=k%5B4%5D,v=k%5B7%5D+(b(u,6)%5Eb(u,11)%5Eb(u,25))+(u&k%5B5%5D%5E~u&k%5B6%5D)+l%5Bc%5D+(p%5Bc%5D=16%3Ec?p%5Bc%5D:p%5Bc-16%5D+(b(r,7)%5Eb(r,18)%5Er%3E%3E%3E3)+p%5Bc-7%5D+(b(s,17)%5Eb(s,19)%5Es%3E%3E%3E10)%7C0),w=(b(t,2)%5Eb(t,13)%5Eb(t,22))+(t&k%5B1%5D%5Et&k%5B2%5D%5Ek%5B1%5D&k%5B2%5D);k=%5Bv+w%7C0%5D.concat(k),k%5B4%5D=k%5B4%5D+v%7C0%7Dfor(c=0
@geraintluff
geraintluff / README.md
Last active August 29, 2015 14:10
AMD define() and require() in 550 bytes

Asynchronous Module Definitions, in 550 bytes

This provides implementations of Asynchronous Module Definition's define() and require() functions. The minified code comes out around 550 bytes.

This enables module definitions, but it doesn't actually fetch any modules that are missing. If you want to automatically fetch modules, see "Extending" below.

The "CommonJS"-style hacks are not supported (e.g. special behaviour for dependencies "module", "require" or "exports").

Usage