Skip to content

Instantly share code, notes, and snippets.

using System;
using System.Linq;
using System.Collections.Generic;
namespace ConsoleApplication
{
public class Program
{
private static readonly Char[] baseDigits = "abcdefghijklmnopqrstuvwxyz0123456789.?-+#".ToCharArray();
@NickStrupat
NickStrupat / ReinterpretCast.cs
Created February 8, 2017 02:44
ReinterpretCast
static unsafe TDest ReinterpretCast<TSource, TDest>(TSource source)
{
var sourceRef = __makeref(source);
var dest = default(TDest);
var destRef = __makeref(dest);
*(IntPtr*)&destRef = *(IntPtr*)&sourceRef;
return __refvalue(destRef, TDest);
}
@NickStrupat
NickStrupat / byte array equals.cs
Last active February 13, 2017 04:06
Pretty fast byte array element equality
private static Boolean BitwiseEquals(Byte[] a, Byte[] b) {
const Int32 _64BitIntPtrSize = 8;
const Int32 _32BitIntPtrSize = 4;
if (ReferenceEquals(a, b))
return true;
if (a.Length != b.Length)
return false;
var i = 0;
var unionA = new Union { Bytes = a };
var unionB = new Union { Bytes = b };
@NickStrupat
NickStrupat / FastString.cs
Last active May 8, 2017 21:54
A string-wrapping struct which eagerly caches the hash code and length, and does a fast (aka unsafe :P) bitwise (ordinal) equality comparison for .Equals()
using System;
using System.Collections.Generic;
namespace GeneralTesting.NETCore {
public struct FastString : IEquatable<FastString>
{
public readonly String String;
public readonly Int32 Length;
public readonly Int32 HashCode;
/// <summary>
/// Sync roots must be in the same order to prevent deadlocks
/// </summary>
public struct MultiLock : IDisposable
{
private readonly object[] _syncRoots;
private readonly bool[] _locksTaken;
public MultiLock(object[] syncRoots) : this()
{
@NickStrupat
NickStrupat / MarshalEx.cs
Created November 20, 2017 18:24
Aligned marshal alloc/free methods for .NET Standard
using System;
using System.Runtime.InteropServices;
namespace NickStrupat {
public static class MarshalEx
{
private static readonly Boolean IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
private static readonly Boolean IsMacOS = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
private static readonly Boolean IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
public static class Extensions
{
public static IList<TResult> Select<TSource, TResult>(this IList<TSource> source, Func<TSource, TResult> selector) => new IListProjection<TSource, TResult>(source, selector);
private class IListProjection<TSource, TResult> : IList<TResult>
{
private readonly IList<TSource> source;
private readonly Func<TSource, TResult> selector;
public IListProjection(IList<TSource> source, Func<TSource, TResult> selector)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://json-schema.org/draft-07/schema#",
"title": "Rosetta Configuration",
"type": "object",
"properties": {
"Options": {
"type": "object",
"additionalProperties": {}
},
Enum OSType {
Windows
Linux
}
function Ensure-Docker-Container-Engine-Mode([OSType] $osType) {
[OSType] $currentOSType = docker info --format "{{.OSType}}"
Write-Output "Current engine: $($currentOSType)"
Write-Output "Requested engine: $($osType)"
if ($currentOSType -eq $osType) {
@NickStrupat
NickStrupat / docker-run.ps1
Created July 5, 2018 06:05
Pass domain and user names into docker container
docker run -it -e DOCKER_HOST_USER_DOMAIN_NAME=$([System.Environment]::UserDomainName) -e DOCKER_HOST_USER_NAME=$([System.Environment]::UserName) mcr.microsoft.com/powershell