Skip to content

Instantly share code, notes, and snippets.

(•_•) ( •_•)>⌐■-■ (⌐■_■)

James Jackson-South JimBobSquarePants

(•_•) ( •_•)>⌐■-■ (⌐■_■)
View GitHub Profile
JimBobSquarePants / Attempt{T}.cs
Created January 14, 2023 11:15
A nullable aware Attempt type for wrapping nullable async responses.
View Attempt{T}.cs
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// A wrapper for nullable values that correctly handles the return type based on the result.
/// </summary>
/// <typeparam name="T">The type of nullable value.</typeparam>
public readonly struct Attempt<T>
/// <summary>
/// Gets a value indicating whether the attempted return was successful.
JimBobSquarePants / ByteMemoryManager.cs
Created September 20, 2022 02:25
MemoryManager<T> implementation that uses an underlying Memory<byte> source.
View ByteMemoryManager.cs
/// <summary>
/// A custom <see cref="MemoryManager{T}"/> that can wrap <see cref="Memory{T}"/> of <see cref="byte"/> instances
/// and cast them to be <see cref="Memory{T}"/> for any arbitrary unmanaged <typeparamref name="T"/> value type.
/// </summary>
/// <typeparam name="T">The value type to use when casting the wrapped <see cref="Memory{T}"/> instance.</typeparam>
internal sealed class ByteMemoryManager<T> : MemoryManager<T>
where T : struct
/// <summary>
/// The wrapped <see cref="Memory{T}"/> of <see cref="byte"/> instance.
JimBobSquarePants /
Last active April 6, 2022 15:44
Skia to System.Numerics Port Question
JimBobSquarePants / ToSplitTitleString.cs
Created October 13, 2021 13:45
Add spaces to string based upon the position of Uppercase letters
View ToSplitTitleString.cs
/// <summary>
/// Returns the input string with spaces inserted at each uppercase character.
/// </summary>
/// <param name="input">The input string to split.</param>
/// <returns>The <see cref="string"/>.</returns>
public static string ToSplitTitleString(string input)
int spaces = 0;
Rune whitespace = new(0x0020);
Rune previous = default;
JimBobSquarePants / ColorDistanceCache.cs
Created June 5, 2021 13:27
A naïve cache for holding color distances. Needs thread safety!
View ColorDistanceCache.cs
/// <summary>
/// A cache for storing color distance matching results.
/// </summary>
/// <remarks>
/// The cache is limited to 646866 entries at 0.62MB.
/// </remarks>
private struct ColorDistanceCache
private const int IndexBits = 5;
private const int IndexAlphaBits = 3;
JimBobSquarePants / TestOutput.txt
Created February 2, 2021 17:39
Failing Avalonia LineBreakEnumerator Test Output.
View TestOutput.txt
Test Name: Avalonia.Visuals.UnitTests.Media.TextFormatting.LineBreakerTests.ICUTests
Test Outcome: Passed
Result StandardOutput:
Line Breaker Tests
Failed test on line 49
Code Points: 35 65532
Expected Breaks: 1 2
Actual Breaks: 2
Char Props: Alphabetic ContingentBreak
JimBobSquarePants / LineBreakerTests.cs
Last active February 2, 2021 06:20
Failing Avalonia LineBreaker tests
View LineBreakerTests.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Avalonia.Media.TextFormatting.Unicode;
using Avalonia.Utilities;
using Xunit;
JimBobSquarePants / ScopedServiceRunner.cs
Created November 10, 2020 21:46
Allows the controlled running of scoped services with automatic cleanup.
View ScopedServiceRunner.cs
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace ThingsWhatImNotProudOf
/// <summary>
/// Allows the manufacture and running of items within a smaller lifetime scope than the parent.
/// To be used sparingly when unable to inject a service within the required scope.
/// </summary>
JimBobSquarePants / intersection.cs
Last active June 24, 2020 11:36
Calculating intersections between two segments.
View intersection.cs
void Main()
var tl = new Vector2(0, 0);
var br = new Vector2(5, 3);
var tr = new Vector2(5, 0);
var bl = new Vector2(0, 3);
var tlbr = new Segment(tl, br);
var trbl = new Segment(tr, bl);
View ESQuery.cs
// Create a query where the result must match or contain the given query
// and the subset of properties and filters.
q => q.Bool(
b => b.Must(
mu =>
m => m.Fields(propertyExpressions) // Search within these properties or all.
.Query(searchTerm) // For this query
.Operator(Operator.Or))) // In any of the properties.