Skip to content

Instantly share code, notes, and snippets.

View cameronism's full-sized avatar

Cameron Jordan cameronism

View GitHub Profile
✔ ~/.vim/bundle
$ for d in *; do pushd $d >/dev/null; git remote show -n origin | awk '/Fetch/{ print $3 }'; popd>/dev/null; done
https://github.com/mattn/emmet-vim.git
https://github.com/KabbAmine/gulp-vim.git
https://github.com/phildawes/racer.git
https://github.com/rust-lang/rust.vim.git
https://github.com/scrooloose/syntastic.git
https://github.com/leafgarland/typescript-vim.git
https://github.com/bling/vim-airline
git://github.com/altercation/vim-colors-solarized.git
@cameronism
cameronism / rfkill-toggle.sh
Created November 14, 2015 04:05
rfkill-toggle.sh
#!/bin/sh
# Usage ./rfkill-toggle.sh [IDENTIFIER]
# where IDENTIFIER is the index no. of an rfkill switch or one of:
# wifi wlan bluetooth uwb ultrawideband wimax wwan gps fm nfc
ID=`rfkill list "$1" | head -c 1 | cut -f 1`
SOFT="/sys/class/rfkill/rfkill$ID/soft"
if [ ! -f "$SOFT" ]; then
@cameronism
cameronism / DelayedTeardown.cs
Created May 20, 2015 21:52
Safe helper to defer setup until needed and teardown until after a delay
/// <summary>
/// Safe helper to defer setup until needed and teardown until after a delay
/// </summary>
/// <remarks>
/// You're on your own if setup or teardown throws or if Dispose is not called on each Acquire result
/// </remarks>
public class DelayedTeardown
{
// -1 indicates that setup needs to be run
private int _count = -1;
@cameronism
cameronism / NestedStopwatch.cs
Created March 12, 2015 03:58
Low overhead nested stopwatch for .NET
public sealed class NestedStopwatch
{
public struct Split : IDisposable
{
private NestedStopwatch _parent;
private int _index;
internal Split(NestedStopwatch sw, int index)
{
_parent = sw;
@cameronism
cameronism / SlowCloner.cs
Created December 10, 2014 00:24
Slow .NET Object Cloner
public static class SlowCloner
{
public static T Clone<T>(T val, int depth = 64)
{
return (T)CloneNonGeneric(val, typeof(T), depth);
}
static object CloneNonGeneric(object val, Type type, int depth = 64)
{
if (val == null) return null;
@cameronism
cameronism / NeedleIndex.cs
Created May 27, 2014 14:27
Needle in a haystack
// find the needle that exactly matches the specified portion of haystack
// will return -1 for count of 0
public static int NeedleIndex(byte[] haystack, int offset, int count, byte[][] needles)
{
Debug.Assert(needles.Length <= 8, "Can only search for up to 8 needles at once");
ulong mask = 0;
for (int i = 0; i < needles.Length; i++)
{
if (needles[i] != null && needles[i].Length == count)
@cameronism
cameronism / Template.cs
Last active August 29, 2015 13:57
Fast, generic, string template pre-processor for templates with variables
public static class Template
{
public struct Span<T>
{
/// <summary>If Literal is not null it should be used and Variable should be ignored</summary>
public readonly string Literal;
/// <summary>Variable should only be used if Literal is null</summary>
public readonly T Variable;
public Span(string literal, T value)
@cameronism
cameronism / uglify-gettext.js
Created December 13, 2013 00:12
Uglify files and inline keys in gettext calls
var UglifyJS = require("uglify-js");
var fs = require('fs');
var defineName = 'define',
getTextModule = 'shared/gettext',
currentGetTextName,
stringDefinitions,
placeholders,
stringKeys,
moduleName;
@cameronism
cameronism / GetFixedBufferPointer.cs
Created September 17, 2013 16:53
Generate delegate to get pointer to fixed buffer field of a struct. Use with extreme caution: http://msdn.microsoft.com/en-us/library/zycewsya.aspx
public unsafe static class FieldPointer
{
public delegate void Get<TItem, TPtr>(ref TItem item, out TPtr* ptr)
where TItem : struct;
public static Get<TItem, TPtr> Generate<TItem, TPtr>(FieldInfo field)
where TItem : struct
{
var fixedElementField = field.FieldType.GetField("FixedElementField");
@cameronism
cameronism / QuickAsciiReader.cs
Last active November 19, 2018 19:10
High speed TextReader-like object. Almost 3x faster than StreamReader + MemoryStream
/// <summary>
/// High speed TextReader-like object, wraps a byte array directly rather than a Stream
/// Almost 3x faster than StreamReader + MemoryStream in my tests
/// </summary>
public struct QuickAsciiReader
{
private readonly byte[] _Array;
private int _Index;
private readonly int _End;