Skip to content

Instantly share code, notes, and snippets.

Avatar

Nobuyuki Iwanaga ufcpp

View GitHub Profile
@ufcpp
ufcpp / MixedSpaces.cs
Last active Dec 1, 2022
mixed-space C# source code
View MixedSpaces.cs
var a = new[] {
0,
1,
2,
3,
    4,
    5,
    6,
    7,
    8,
@ufcpp
ufcpp / CycleDetector.cs
Created Nov 30, 2022
Tarjan's cycle detection implemeted in C#
View CycleDetector.cs
// 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),
View Unicode9OrLater.cs
// 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
View ParseOrNull.cs
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;
}
@ufcpp
ufcpp / stackalloc.M.cs
Created Oct 17, 2022
(stackalloc) + extension methods
View stackalloc.M.cs
// u8 リテラルの型は ReadOnlySpan<byte> なので、ROS に対する拡張メソッド直呼び可能。
""u8.R();
// それでふと思ったけども、stackalloc も行ける?
// 式の途中で stackalloc を書くと Span になる。
A.S(stackalloc byte[0]);
// この場合(拡張メソッドじゃなくて、静的メソッド呼びの場合)、 ReadOnlySpan への変換も働く。
A.R(stackalloc byte[0]);
@ufcpp
ufcpp / FixedArray.cs
Created Oct 12, 2022
.NET 7 RC 2 でやっとまっとうに動いた
View FixedArray.cs
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Runtime.InteropServices;
var a2 = new Array2<int>();
for (int i = 0; i < 2; i++) a2[i] = i + 1;
Console.WriteLine(sum(a2.AsReadOnlySpan()));
var a3 = new Array3<int>();
for (int i = 0; i < 3; i++) a3[i] = i + 1;
@ufcpp
ufcpp / DependencyAnalyzer.cs
Created Oct 7, 2022
GetProjectDependencyGraph を mermaid 記法出力
View DependencyAnalyzer.cs
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
class DependencyAnalyzer
{
public static async ValueTask WriteMermaidGraph(string path)
{
MSBuildLocator.RegisterDefaults();
var workspace = MSBuildWorkspace.Create();
@ufcpp
ufcpp / MixedSpaces.cs
Created Sep 23, 2022
Mixed space characters
View MixedSpaces.cs
_ = """
            All whitespace characters are the same as the closing line.
            U+20, U+2000, U+2001, U+2002, U+2003, U+2004, U+2005, U+2006, U+2007, U+2008, U+2009, U+200A, U+3000
            Please insert a new line here:
            """;
@ufcpp
ufcpp / ShouldError.cs
Last active Nov 8, 2022
Ref Fields bug in VisualStudio 17.4 Preview 2
View ShouldError.cs
ref var r = ref m();
static ref int m()
{
int i = 0;
return ref m1(ref i).Item1;
static Ref m1(scoped ref int i) => new(ref i); // A scoped ref parameter passed to new Ref() as unscoped ref.
}
#if false
@ufcpp
ufcpp / CovariantArray.cs
Created Sep 12, 2022
久々にミスった。
View CovariantArray.cs
// ↓.NET 的に許されていはいるものの、 items[0] = new Base(); が例外を起こすので今となってはあんまり使いたくない機能。
// 意図的に使うことはめったにないものの…
Base[] items1 = new Derived[1];
// ↓これはミスってやっちゃうことが…
// (左辺からの型推論はせず、初期化側から推論して Derived[] になる。)
Base[] items2 = new[] { new Derived() };
// items2[0] = new Base(); が問題を起こすのはまあ、有名として、
// new Span<T>(), AsSpan<T>() は呼んだ時点で例外らしい。