Skip to content

Instantly share code, notes, and snippets.

View simonwhitaker's full-sized avatar

Simon Whitaker simonwhitaker

  • Oxfordshire, UK
View GitHub Profile
@simonwhitaker
simonwhitaker / enumeration-benchmark.m
Created July 2, 2013 06:30
A simple benchmarking experiment to compare enumeration techniques in Objective-C. My results are in the comments.
#import <Foundation/Foundation.h>
void timeThis(NSString *label, NSUInteger iterations, void(^block)()) {
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
for (NSUInteger i = 0; i < iterations; i++) {
block();
}
CFAbsoluteTime interval = CFAbsoluteTimeGetCurrent() - start;
printf("%s: %lu iterations, %.3fs\n", [label UTF8String], iterations, interval);
@simonwhitaker
simonwhitaker / git-is-ancestor
Last active April 5, 2022 08:01
A script to determine whether one git commit is the ancestor of another
#!/bin/bash
#
# git-is-ancestor, by Simon Whitaker
#
# Suggested usage
#
# Store this file somewhere, make it executable, then alias
# it to git is-ancestor by putting this in your $HOME/.gitconfig:
#
# [alias]
#import <Foundation/Foundation.h>
typedef NS_OPTIONS(uint32_t, SomeOption) {
SomeOptionNone = 0,
SomeOptionFoo = 1 << 0,
SomeOptionBar = 1 << 1,
};
int main(int argc, char *argv[]) {
@autoreleasepool {
$ cat test.c
#import <stdio.h>
#define NYBBLE 4
int main(int argc, const char * argv[]) {
unsigned long t = (unsigned long)argc;
t = (t << NYBBLE) | (t >> (sizeof(t) * 8 - NYBBLE));
printf("t is now 0x%lx\n", t);
@simonwhitaker
simonwhitaker / swmath.m
Last active January 1, 2016 22:58
Architecture-independent floor() function
/*
* Calling math.h functions like `floor` and `floorf` on CGFloat variables
* becomes problematic when compiling the same code for both 32-bit and
* 64-bit platforms, since CGFloat is double on 64-bit, float on 32-bit.
* Hence, if you compile with -Wconversion set, `floorf` will give a warning
* on 64-bit, while `floor` gives a warning on 32-bit.
*
* Here's a suggested implementation of an architecture-independent `floor`
* function, written using plain old function overloading which is enabled
* using the `__attribute__((overloadable))` Clang language extension.
@simonwhitaker
simonwhitaker / one-simple-block-trick.markdown
Last active August 29, 2015 13:57
Wrapping a method implementation

Here's a nice trick for wrapping a method's implementation in a block, for example for logging the return value.

Have you ever written a method with a load of conditionals and return statements...

- (int)statusCode {
    if (foo) {
        return 1;
    }
    if (bar) {
#!/usr/bin/env python
#
# git-flip-flop: checks out two or more commits in a periodic cycle
#
# Save on your $PATH as git-flip-flop
#
# EXAMPLES
#
# Check out abc1234, then def5678, then abc1234, etc, with
# the default 2s between each checkout
@simonwhitaker
simonwhitaker / decorate-sort-undecorate.swift
Last active August 29, 2015 14:02
A demonstration of how you might use the decorate-sort-undecorate pattern in Swift, using a handful of cool Swift language features.
/*
Decorate-Sort-Undecorate in Swift
In Swift, you get the length of a String by calling the global function
countElements(). However, The Swift Programming Language states:
> The length of a string cannot be calculated without iterating through
> the string to consider each of its characters in turn. If you are
> working with particularly long string values, be aware that the
> countElements function must iterate over the characters within a string

I was intrigued by this example from The Swift Programming Guide's Extensions section:

extension Double {
    var km: Double { return self * 1_000.0 }
    var m: Double { return self }
    var cm: Double { return self / 100.0 }
    var mm: Double { return self / 1_000.0 }
    var ft: Double { return self / 3.28084 }
}
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSDateFormatter *goodDateFormatter = [[NSDateFormatter alloc] init];
goodDateFormatter.dateFormat = @"dd-MM-yyyy HH:mm:ss.SSS";
NSDateFormatter *badDateFormatter = [[NSDateFormatter alloc] init];
badDateFormatter.dateFormat = @"dd-MM-YYYY HH:mm:ss.SSS";