Skip to content

Instantly share code, notes, and snippets.

@gregmac
Created February 3, 2022 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregmac/b2c13fc4381166a13f6acac26f75ea25 to your computer and use it in GitHub Desktop.
Save gregmac/b2c13fc4381166a13f6acac26f75ea25 to your computer and use it in GitHub Desktop.
LINQPad Extensions
public static class MyExtensions
{
// Write custom extension methods here. They will be available to all queries.
/// <summary>Dump a string using wrapping</summary>
public static string DumpWrapped(this string value, string title = null, int width = 120)
{
string.Join("\n", value.Select((x, i) => i)
.Where(i => i % width == 0)
.Select(i => value.Substring(i, value.Length - i >= width ? width : value.Length - i)))
.Dump(title);
return value;
}
/// <summary>Dump a JSON string as formatted JSON</summary>
public static string DumpJson(this string value, string title = null)
{
Newtonsoft.Json.JsonConvert.SerializeObject(
Newtonsoft.Json.JsonConvert.DeserializeObject(value),
Newtonsoft.Json.Formatting.Indented)
.Dump(title);
return value;
}
/// <summary>Dump hex and binary values</summary>
public static byte DumpBinary(this byte value, string description = null)
{
DumpBinary(value, value.ToString("X"), Convert.ToString(value, 2));
return value;
}
/// <summary>Dump hex and binary values</summary>
public static short DumpBinary(this short value, string description = null)
{
DumpBinary(value, value.ToString("X"), Convert.ToString(value, 2));
return value;
}
/// <summary>Dump hex and binary values</summary>
public static int DumpBinary(this int value, string description = null)
{
DumpBinary(value, value.ToString("X"), Convert.ToString(value, 2));
return value;
}
/// <summary>Dump hex and binary values</summary>
public static long DumpBinary(this long value, string description = null)
{
DumpBinary(value, value.ToString("X"), Convert.ToString(value, 2));
return value;
}
/// <summary>Dump hex and binary values</summary>
public static sbyte DumpBinary(this sbyte value, string description = null)
{
DumpBinary(value, value.ToString("X"), Convert.ToString(value, 2));
return value;
}
/// <summary>Dump hex and binary values</summary>
public static float DumpBinary(this float value, string description = null)
{
DumpBinary(value, value.ToString("X"), Convert.ToString(value));
return value;
}
/// <summary>Dump hex and binary values</summary>
public static ushort DumpBinary(this ushort value, string description = null)
{
DumpBinary(value, value.ToString("X"), Convert.ToString(value, 2));
return value;
}
/// <summary>Dump hex and binary values</summary>
public static uint DumpBinary(this uint value, string description = null)
{
DumpBinary(value, value.ToString("X"), Convert.ToString(value, 2));
return value;
}
/// <summary>Dump hex and binary values</summary>
public static ulong DumpBinary(this ulong value, string description = null)
{
DumpBinary(value, value.ToString("X"), Convert.ToString((long)value, 2));
return value;
}
/// <summary>Dump hex and binary values</summary>
private static void DumpBinary(object value, string hexString, string binaryString, string description = null)
{
new
{
Value = value,
Type = value.GetType(),
Hex = GroupDigits(hexString, 2),
Binary = GroupDigits(binaryString, 4),
}.Dump(description);
}
/// <summary>Insert separator every digitsPerGroup characters, with padding</summary>
private static string GroupDigits(string value, byte digitsPerGroup, string separator = " ", char leftPadChar = '0')
{
var padding = value.Length % digitsPerGroup;
return string.Join(separator,
(value
.Select((digit, idx) => new { idx, digit })
.GroupBy(x => (x.idx + digitsPerGroup - padding) / digitsPerGroup)
.Select(g => string.Concat(g.Select(x => x.digit)).PadLeft(digitsPerGroup, leftPadChar))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment