Skip to content

Instantly share code, notes, and snippets.

View jcracknell's full-sized avatar

James Cracknell jcracknell

  • GNWT Department of Justice
  • Yellowknife, NT
View GitHub Profile
@jcracknell
jcracknell / levenshtein.js
Last active December 17, 2015 00:11
Efficient Javascript implementation of Levenshtein distance.
// Compute the Levenshtein distance between `Array`-like sequences `a` and `b`; the minimum number of
// single-element insertions, deletions and substitutions necessary to transform `a` into `b`.
// Strict equality semantics are used by default to compare sequence elements; a function `equal` can
// optionally be provided to alter this behavior.
function levenshtein(a, b, equal) {
// `aii` and `bii` are used to track `ai - 1` and `bi - 1` more or less free of charge.
// `d0` and `d1` are used to store the 'previous' and 'current' rows respectively.
// `x`, `y` and `z` are used to permit the implementation of an optimized `min` using a ternary expression.
var ai, aii, ae, al, bi, bii, bl, d0, d1, dd, x, y, z;
if(a === b) return 0;
@jcracknell
jcracknell / SslRedirectionHttpHandler.cs
Last active December 17, 2015 01:29
Incredibly simple redirection to equivalent SSL-secured URI. This is intended to be configured on a dedicated site listening on port 80, and will blindly redirect all GET requests to the same URL with the `https` protocol, and `403.4 SSL Required` everything else.
using System;
using System.Web;
namespace SslRedirection {
public class SslRedirectionHttpHandler : IHttpHandler {
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context) {
if ("GET".Equals(context.Request.HttpMethod, StringComparison.Ordinal)) {
context.Response.StatusCode = 301;
@jcracknell
jcracknell / StringLiteralEncoding.cs
Last active December 18, 2015 01:09
Encode C# strings to an equivalent string literal.
using System;
using System.Text;
using System.Linq;
public static class StringLiteralEncoding {
private static readonly char[] HEX_DIGIT_LOWER = "0123456789abcdef".ToCharArray();
private static readonly char[] LITERALENCODE_ESCAPE_CHARS;
static StringLiteralEncoding() {
// Per http://msdn.microsoft.com/en-us/library/h21280bw.aspx
public static string RemoveDiacritics(this string str) {
var formD = str.Normalize(NormalizationForm.FormD);
var bp = 0;
var buffer = new char[formD.Length];
for(var i = 0; i < formD.Length; i++)
if(UnicodeCategory.NonSpacingMark != CharUnicodeInfo.GetUnicodeCategory(formD[i]))
buffer[bp++] = formD[i];
return new string(buffer, 0, bp);
@jcracknell
jcracknell / LiteralEncoding.scala
Last active December 22, 2015 05:49
LiteralEncoding.scala
object LiteralEncoding {
private val Hex = "0123456789abcdef".toArray
private val StringEscapes = {
val escapes = Map(
'\b' -> 'b', // backspace
'\t' -> 't', // tab
'\n' -> 'n', // linefeed
'\f' -> 'f', // formfeed
'\r' -> 'r', // carriage return
@jcracknell
jcracknell / ContravariantSpecificity.1.scala
Last active December 24, 2015 14:29
Examples demonstrating scala's bizzare specificity rules as applied to contravariant types.
class Animal
class Cat extends Animal
abstract class Whisperer[-A <: Animal] extends (A => Unit)
object Whisperer {
implicit def animalWhisperer: Whisperer[Animal] =
new Whisperer[Animal] { def apply(a: Animal): Unit = { println("animal") } }
implicit def catWhisperer: Whisperer[Cat] =
@jcracknell
jcracknell / JsonDialect.scala
Created December 18, 2013 23:11
Abstracting away your JSON DOM with a 'dialect' mixin.
trait JsonDialect { dialect =>
import scala.language.implicitConversions
type JValue
type JArray <: JValue
type JBoolean <: JValue
type JNull <: JValue
type JNumber <: JValue
type JObject <: JValue
type JString <: JValue
#!/bin/bash
#==========
# Uses the GIMP to set the specified DPI value on the specified image without
# changing the dimensions of the image.
#
# This is useful as Inkscape provides no means of changing the DPI of an exported
# image without altering its pixel count, and also defaults to 90dpi.
#
# Note that gimp-console must be in your $PATH.
#!/bin/bash
while true; do
ls /run/user/$UID/gvfs/* &> /dev/null
sleep 15
done
@jcracknell
jcracknell / ldapsearch.sh
Created June 12, 2014 19:27
Strip newlines from ldapsearch results
#!/bin/bash
ldapsearch "$@" | sed -n '1h; 1!H; ${ g; s/\r\?\n //g; p }'