Skip to content

Instantly share code, notes, and snippets.

View ufcpp's full-sized avatar

Nobuyuki Iwanaga ufcpp

View GitHub Profile
using System.Buffers.Text;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
var x = 0x12345678;
Console.WriteLine($"{x.Hex()}");
public static class Format
{
public static AsciiBuffer8 Hex(this int x)
using System.Buffers.Binary;
var x = 0x12345678;
Console.WriteLine($"{x:X}");
Console.WriteLine($"{x.Little()}");
static class LittleEndianExtensions
{
public static LittleEndianInt32 Little(this int x) => new(x);
@ufcpp
ufcpp / Comparison.cs
Created March 11, 2023 14:59
A<B, C>(D)
class Program
{
static void X(bool x) { }
static void X(bool x, bool y) { }
static bool A<T1, T2>(int x) => true;
class B { }
class C { }
static void Main()
{
@ufcpp
ufcpp / ConditionalNestedInterpolation.cs
Created January 14, 2023 01:45
IfTrue/IfFalse interpolated string handler
using System.Globalization;
using System.Runtime.CompilerServices;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
static string m(bool condition) => string.Create(
CultureInfo.GetCultureInfo("fr-fr"),
$"direct: {1.5} or {1:c}, nested: {If(condition, $"{1.5}", $"{1:C}")}");
Console.WriteLine(m(true));
@ufcpp
ufcpp / IDefaultSpanFormattable.cs
Created January 7, 2023 14:56
ISpanFormattable default implementation of IFormattable.ToString
using System.Buffers;
public interface IDefaultSpanFormattable : ISpanFormattable
{
string IFormattable.ToString(string? format, IFormatProvider? formatProvider)
{
var buffer = (stackalloc char[512]);
if (TryFormat(buffer, out var charsWritten, format, formatProvider))
{
return new string(buffer[..charsWritten]);
@ufcpp
ufcpp / RefLinkedList.cs
Created December 12, 2022 15:32
ref linked list
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
var nodes = new Node<string>[]
{
new("a0", 2),
new("a1", 5),
new("a2", 1),
new("a3", -2),
new("a4", 3),
@ufcpp
ufcpp / tt2rawstring.cs
Last active December 7, 2022 13:28
Convert T4 to StringBuilder.Append($$""" """);
using System.Text.RegularExpressions;
using System.Windows;
class Program
{
[STAThread]
static void Main()
{
var x = Clipboard.GetText();
@ufcpp
ufcpp / StringComparer.cs
Created December 7, 2022 06:05
Comparer<string>.Default… お前…
using System.Globalization;
var items = new[]
{
"ABc",
"Abc",
@"A\c",
};
// ASCII コード順。
@ufcpp
ufcpp / MixedSpaces.cs
Last active December 1, 2022 13:32
mixed-space C# source code
var a = new[] {
0,
1,
2,
3,
    4,
    5,
    6,
    7,
    8,
@ufcpp
ufcpp / CycleDetector.cs
Created November 30, 2022 06:01
Tarjan's cycle detection implemeted in C#
// see https://stackoverflow.com/questions/6643076/tarjan-cycle-detection-help-c-sharp
// tests simple model presented on https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
using System.Runtime.InteropServices;
var cycles = CycleDetector.Detect(
(1, 2),
(2, 3),
(3, 1),
(4, 3),
(4, 5),