Skip to content

Instantly share code, notes, and snippets.

@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)
{
@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 / 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 / SMHasher_pearson_hash_tests_output.txt
Last active January 22, 2018 02:00
SMHasher test results for Clayton's hash function
iolanthe:smhasher dbremner$ cd build
iolanthe:build dbremner$ ./SMHasher pearson_hash
-------------------------------------------------------------------------------
--- Testing pearson_hash "http://calypsosfarewell.blogspot.com/2018/01/toy-code-hand-crafting-good-hash.html"
[[[ Sanity Tests ]]]
Verification value 0x5AA86F6C : PASS
Running sanity check 1 ..........PASS
Running AppendedZeroesTest..........PASS
@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 / 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
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 / 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;
#include<limits.h>
#include<stdlib.h>
#include<stdio.h>
int f1(int num)
{
const int result = abs(num) - num;
return result;
}
@dbremner
dbremner / gist:e7c9e6891b1c230f08d39b38fdf9fb9a
Created March 25, 2019 01:22
Deleting Old Time Machine backups
Problem:
Time Machine backups accumulate until they fill the drive.
Solution:
1. Run "tmutil listbackups" from a shell and pipe the output to a file.
2. Use TextEdit pattern-based search and replace to quote each line of the file.
3. Select a subset of the file with a buffer of several years.
4. Open a terminal and run caffeinate to keep the computer from sleeping.
5. Open an interactive root shell with "sudo -i"
6. Use the following command to delete old backups.