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
def urldecode:
def unhex:
if 48 <= . and . <= 57 then . - 48 elif 65 <= . and . <= 70 then . - 55 else . - 87 end;
def bytes:
def loop($i):
if $i >= length then empty else 16 * (.[$i+1] | unhex) + (.[$i+2] | unhex), loop($i+3) end;
[loop(0)];
def codepoints:
@jcracknell
jcracknell / dnsmasq.conf
Created December 23, 2023 15:18
FreshTomato dnsmasq configuration
# Use a much larger DNS cache size than the default of 150 entries, which is
# ludicrously small
cache-size=4096
# Use stale cache entries (up to 1 day out of date). Using stale cache data triggers
# a background update of the cached entry, but responds immediately.
use-stale-cache
# Query all configured DNS servers simultaneously, using the first response.
# Reduces response times for uncached DNS lookups.
@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 }'
@jcracknell
jcracknell / levenshtein.sh
Last active July 31, 2022 01:06
levenshtein.sh
#!/bin/bash
function levenshtein() { a="$1"; b="$2";
if [ "$a" == "$b" ]; then echo 0; return 0; fi
if [ 0 -eq ${#a} ]; then echo ${#b}; return 0; fi
if [ 0 -eq ${#b} ]; then echo ${#a}; return 0; fi
d0=(0)
d1=()
for i in $(seq 1 ${#b}); do d0[$i]=$i; done
@jcracknell
jcracknell / numerals.sql
Created December 20, 2021 20:48
Recursive CTE for alpha/roman numeral encoding
-- Encodes the provided numeral value using alpha numerals.
create or alter function encode_alpha(@numeral int, @a char(1)) returns varchar(32)
with returns null on null input as
begin
declare @result varchar(32);
with [alphabet] ([Value], [Char]) as (
select [n].[Value], char(ascii(@a) + [n].[Value] - 1)
from (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26)) as [n] ([Value])
),
@jcracknell
jcracknell / CSV.bas
Created September 12, 2018 17:19
CSV.bas
' Rudimentary CSV parser in VBA. Wheee.
Option Explicit
Public Function IsEmptyRecord(ByRef record() As String)
IsEmptyRecord = UBound(record) = 0 And record(0) = ""
End Function
' Reads a CSV record from the provided TextStream, assigning the fields
' to the provided array reference. Returns True if a record is read from
' the TextStream, or False if the end of the stream has been reached.
@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
@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 / 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
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);