Skip to content

Instantly share code, notes, and snippets.

@binarycow
binarycow / all.json
Last active November 8, 2022 18:28
yang test
{
"alpha:username": "admin",
"alpha:password": "password",
"alpha:encryption-type": "Cisco-Type7"
}
@binarycow
binarycow / Ipv4Address.cs
Created August 26, 2022 14:39
Ipv4Address Parsing
/*
IPv4 addresses are 32-bit unsigned integers, that are commonly
represented in "dotted decimal notation".
Dotted decimal notation takes each 8-bit byte of the IP address,
displays them in base 10, and separates them with a decimal point.
10.100.30.52
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Bridge">
<html>
<body>
<h2><xsl:value-of select="Name"/></h2>
<h4>Bridge Properties</h4>
<ul>
<li>Number of Spans: <xsl:value-of select="NumSpans"/></li>
public readonly struct OneVarInterpolationPoint
: IComparable<OneVarInterpolationPoint>,
IComparable<decimal>,
IComparable,
IEquatable<OneVarInterpolationPoint>,
IEquatable<decimal>
{
public OneVarInterpolationPoint(decimal var, decimal result)
{
@binarycow
binarycow / naiveparse.cs
Created July 19, 2022 15:53
PhoneNumber Example
public static PhoneNumber Parse(string text)
{
var match = Regex.Match(text, @"^\(((?<areaCode>\d{3})\)\s*)?(?<exchange>\d{3})-(?<number>\d{4})$");
var areaCode = string.IsNullOrWhiteSpace(match.Groups["areaCode"].Value)
? null
: (int?)int.Parse(match.Groups["areaCode"].Value);
var exchange = int.Parse(match.Groups["exchange"].Value);
var number = int.Parse(match.Groups["number"].Value);
if (areaCode is < 0 or > 999)
throw new Exception("Area code is invalid");
@binarycow
binarycow / BitVector.cs
Last active June 26, 2022 17:56
Generic BitVector
using System.Diagnostics.CodeAnalysis;
public struct BitVector<T>
: IBitVector<BitVector<T>, T>,
IEquatable<T>, IEqualityOperators<BitVector<T>, T>
where T : IEquatable<T>,
IBitwiseOperators<T, T, T>,
IShiftOperators<T, T>,
IMinMaxValue<T>,
IBinaryInteger<T>,
@binarycow
binarycow / CustomButton.cs
Created June 25, 2022 17:26
Custom Button that handles CommandParameter CanExecute issue
using System.Windows;
using System.Windows.Controls;
/// <remarks>
/// Handles issue seen here:
/// https://stackoverflow.com/a/24669638/29249
/// </remarks>
public class CustomButton : Button
{
static CustomButton()
@binarycow
binarycow / CustomCommand.cs
Created June 25, 2022 17:18
Custom RelayCommand<T> that accepts null values for value types
using System;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input; // https://www.nuget.org/packages/CommunityToolkit.Mvvm
public class CustomCommand<T> : IRelayCommand<T>
{
private readonly Action<T?> execute;
private readonly Predicate<T?>? canExecute;
public CustomCommand(Action<T?> execute, Predicate<T?>? canExecute = null)
@binarycow
binarycow / MonogameTest.fsproj
Created January 7, 2022 19:52
F# Monogame sample
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<PublishReadyToRun>false</PublishReadyToRun>
<TieredCompilation>false</TieredCompilation>
<Nullable>enable</Nullable>
<WarnOn>3390;$(WarnOn)</WarnOn>
<LangVersion>preview</LangVersion>
public sealed class RelativeFontSizeHelper
{
public static readonly DependencyProperty RelativeFontSizeProperty = DependencyProperty.RegisterAttached(
"RelativeFontSize",
typeof(double),
typeof(RelativeFontSizeHelper),
new ((double)0, RelativeFontSizeChanged)
);
public static double GetRelativeFontSize(TextBlock textBlock)