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 / 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;
@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.
@dbremner
dbremner / crontab_changes.txt
Last active June 24, 2019 21:07
disable SACK on Ubuntu 16.04 LTS
#Ubuntu 16.04LTS does not apply network settings after boot.
#See https://bugs.launchpad.net/ubuntu/+source/procps/+bug/50093
#Work around this issue with a cron job to disable SACK
@reboot /bin/sleep 5 && /sbin/sysctl -w net.ipv4.tcp_sack=0
@dbremner
dbremner / ListModels.Java
Last active July 4, 2019 17:01
Iterable from Swing ListModel
import org.jetbrains.annotations.NotNull;
import javax.swing.ListModel;
import java.util.AbstractList;
public enum ListModels
{
;
public static @NotNull <E> Iterable<E> asIterable(final @NotNull ListModel<E> model)