Skip to content

Instantly share code, notes, and snippets.

View ufcpp's full-sized avatar

Nobuyuki Iwanaga ufcpp

View GitHub Profile
@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),
// Unicode 8.0
int ᏸ = 8; // U+13F8, Cherokee Small Letter Ye
// Unicode 9.0
int Ɪ = 9; // U+A7AE, Small Captital I
// Unicode 10.0
int ৼ = 10; // U+09FC, Bengali Letter Vedic Anusvara
// Unicode 11.0
@ufcpp
ufcpp / ParseOrNull.cs
Created October 17, 2022 01:40
ParseOrNull
Console.WriteLine("123".ParseOrNull<int>());
Console.WriteLine("1a3".ParseOrNull<int>());
static class Ex
{
public static T? ParseOrNull<T>(this string? s, IFormatProvider? provider = null)
where T : struct, IParsable<T>
=> T.TryParse(s, provider, out var value) ? value : null;
}