Skip to content

Instantly share code, notes, and snippets.

@dbremner
dbremner / Program.cs
Last active March 11, 2019 16:54
BufferedStream vs NetworkStream benchmark
// Steven Toub wrote this in https://github.com/dotnet/corefx/issues/1793#issuecomment-151584410
// I have made only minor changes.
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
var a = BitConverter.Int64BitsToDouble(BitConverter.ToInt64(BitConverter.GetBytes(0xFFF8000000000000), 0));
var b = BitConverter.Int64BitsToDouble(BitConverter.ToInt64(BitConverter.GetBytes(0xFFF8000000000001), 0));
Console.WriteLine(a == b);
Console.WriteLine(a.Equals(b));
Console.WriteLine(a.Equals((object)b));
@dbremner
dbremner / usual_nos.h
Last active July 12, 2023 23:27
defines to disable or enable portions of windows.h
#define WIN32_LEAN_AND_MEAN
#define NOGDICAPMASKS
#define NOVIRTUALKEYCODES
#define NOWINMESSAGES
#define NOWINSTYLES
#define NOSYSMETRICS
#define NOMENUS
#define NOICONS
#define NOKEYSTATES
#define NOSYSCOMMANDS
@dbremner
dbremner / main.c
Last active August 11, 2023 18:12
Memory Safety in Rust : A Safer C Implementation
#include "vec.h"
int main(void) {
Vec* vec = vec_new();
//vec is a pointer to a struct of an unknown type
//so the compiler will not allow bugs 6 and 7 to occur.
vec_push(vec, 107);
vec_push(vec, 110);
vec_free(vec);
}
@dbremner
dbremner / handle_traits.h
Created July 15, 2017 05:19
base class for Microsoft::WRL::Wrappers::HandleT
#pragma once
template<class T, T InvalidValue = nullptr>
struct HandleTraitsBase
{
using Type = T;
inline static Type GetInvalidValue() noexcept
{
return InvalidValue;
}
@dbremner
dbremner / handle_traits_base.hpp
Created July 15, 2017 05:04
base class for KennyKerr::unique_handle (https://dx.codeplex.com)
template<class T, T invalid_value=nullptr>
struct handle_traits_base
{
using pointer = T;
inline static pointer invalid() throw()
{
return invalid_value;
}
};
@dbremner
dbremner / tryparse_annotations.cs
Last active July 9, 2017 22:15
using ReSharper annotations for a TryParse workalike
//taken from https://resharper-support.jetbrains.com/hc/en-us/community/posts/206650785-Can-I-handle-not-initialized-warnings-with-Jetbrains-Annotations-
[ContractAnnotation("=> false, target:null; => true, target:notnull")]
private bool TryFindWorkbook([NotNull] Excel.Workbooks workbooks, [CanBeNull] out Excel.Workbook target)
{
Requires.NotNull(workbooks, nameof(workbooks));
foreach (Excel.Workbook workbook in workbooks)
{
if (workbook.Name == cellLocation.WorkBookName)
{