Skip to content

Instantly share code, notes, and snippets.

@craigtp
craigtp / NotNull
Created April 22, 2014 09:58 — forked from bleroy/NotNull
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Bleroy.Helpers {
public static class NotNull {
public static TProp Get<TSource, TProp>(this TSource source, Expression<Func<TSource, TProp>> property) where TSource : class {
if (source == null) return default(TProp);
var current = property.Body;
public static class BubbleBabble
{
private static readonly string vowels = "aeiouy";
private static readonly string consonants = "bcdfghklmnprstvzx";
public static string Convert(byte[] bytes)
{
int seed = 1;
int rounds = 1 + bytes.Length / 2;
StringBuilder result = new StringBuilder();
@craigtp
craigtp / BreakIntegerToConstituentDigits.cs
Created February 2, 2015 08:37
Break an integer number into it's constituent digits.
using System;
public class Program
{
public static void Main()
{
var result = BreakToDigits(56);
}
public static int[] BreakToDigits(int startingNumber)
@craigtp
craigtp / HGAutoUpdate
Created June 18, 2015 14:42
Recurse all folders beneath the current folder for Mercurial repositories and perform a "hg pull -u" on each repo.
dir -Directory -Recurse | ? { test-path (join-path $_.FullName ".hg\hgrc") } | % { $_.FullName; Set-Location -Path $_.FullName; iex "hg pull -u" }
@craigtp
craigtp / GuidToShortGuid.cs
Created February 20, 2012 15:53
Convert a GUID to a smaller (unique) GUID
public static string GuidToShortGuid(Guid gooid)
{
string encoded = Convert.ToBase64String(gooid.ToByteArray());
encoded = encoded.Replace("/", "_").Replace("+", "-");
return encoded.Substring(0, 22);
}
@craigtp
craigtp / remove_svn.bat
Created March 13, 2012 09:44
Recursively remove all .svn folders - Windows
for /f "tokens=* delims=" %%i in ('dir /s /b /a:d *svn') do (
rd /s /q "%%i"
)
@craigtp
craigtp / FizzBuzz.cs
Last active October 8, 2015 11:18
FizzBuzz - 3 ways
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace FizzBuzz
{
class Program
{
static void Main(string[] args)
@craigtp
craigtp / NumberFunctions.cs
Last active December 20, 2015 02:59
A C# IsNumeric function, written as an extension method.
using System;
using System.Globalization;
namespace NumberFunctionUtilities
{
public static class NumberFunctions
{
public static bool IsNumeric(this object expression)
{
if (expression == null)
public static class StringExtentionMethods
{
public static string TruncateAtWord(this string input, int maxlength, bool addEllipsis)
{
if (input == null || input.Length <= maxlength) return input;
var ellipsisLengthDeduction = (addEllipsis ? 3 : 0);
var iLastSpace = input.LastIndexOf(" ", (maxlength - ellipsisLengthDeduction), StringComparison.Ordinal);
var iLength = 0;
if (iLastSpace < 0)
{
@craigtp
craigtp / RandomString.cs
Created April 29, 2016 12:16
RandomString - A class containing methods to produce some handy random string combinations.
using System;
using System.Text;
namespace RandomString
{
public class RandomString : IDisposable
{
// Can use the BetterRandom class here or just use the built-in System.Random class.
private BetterRandom random = new BetterRandom();
private const string alpha_selection = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";