Skip to content

Instantly share code, notes, and snippets.

View mayoff's full-sized avatar
😷
status messages are fun

Rob Mayoff mayoff

😷
status messages are fun
View GitHub Profile
@mayoff
mayoff / parser.c
Last active December 11, 2017 15:48
recursive descent parser for a very simple tree grammar; see http://stackoverflow.com/questions/14085626
#include <stdio.h>
#include <stdlib.h>
typedef struct BinaryTree {
char info;
struct BinaryTree *left, *right, *father;
} BinaryTree;
typedef struct {
BinaryTree *node;
static CGPathRef createClosedPathWithPoints(const CGPoint *points, size_t count) {
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddLines(path, NULL, points, count);
CGPathCloseSubpath(path);
return path;
}
static CGRect integralFrameForPath(CGPathRef path) {
CGRect frame = CGPathGetBoundingBox(path);
return CGRectIntegral(frame);
@mayoff
mayoff / makeAnimatedGif.m
Created February 16, 2013 23:00
Example of creating an animated GIF on iOS, with no 3rd-party code required. This should also be easy to port to OS X.
#import <UIKit/UIKit.h>
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
static UIImage *frameImage(CGSize size, CGFloat radians) {
UIGraphicsBeginImageContextWithOptions(size, YES, 1); {
[[UIColor whiteColor] setFill];
UIRectFill(CGRectInfinite);
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(gc, size.width / 2, size.height / 2);
@mayoff
mayoff / longDecimalToHex.mm
Created March 13, 2013 21:30
Test program that converts an arbitrary-precision decimal digit string to a hex digit string.
#import <Foundation/Foundation.h>
#import <vector>
// index 0 is the least significant digit
typedef std::vector<uint16_t> BigInt;
static void insertDecimalDigit(BigInt &b, uint16_t decimalDigit) {
uint32_t carry = decimalDigit;
for (size_t i = 0; i < b.size(); ++i) {
uint32_t product = b[i] * (uint32_t)10 + carry;
@mayoff
mayoff / drawArc.m
Created April 7, 2013 22:07
Use Core Graphics to draw an arc with a gradient fill, a stroked outline, and a shadow. http://stackoverflow.com/questions/15866179/draw-segments-from-a-circle-or-donut
static UIImage *imageWithSize(CGSize size) {
static CGFloat const kThickness = 20;
static CGFloat const kLineWidth = 1;
static CGFloat const kShadowWidth = 8;
UIGraphicsBeginImageContextWithOptions(size, NO, 0); {
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextAddArc(gc, size.width / 2, size.height / 2,
(size.width - kThickness - kLineWidth) / 2,
-M_PI / 4, -3 * M_PI / 4, YES);
@mayoff
mayoff / open.sh
Last active April 5, 2018 23:46
bash completion for the `-a` and `-b` flags of the Mac OS X `open` command
#
# This is a set of bash function definitions, and a bash command, that
# set up bash completion for the Mac OS X "open" command.
#
# HOW TO USE
#
# Add this command to your .bashrc:
#
# . open.sh
#
@mayoff
mayoff / ViewController.m
Created June 14, 2013 23:53
Example of using auto layout to make a container view as tall as its tallest subview, but no taller. For http://stackoverflow.com/q/17117799/77567 .
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController {
UIView *containerView;
NSArray *subviews;
@mayoff
mayoff / .lldbinit
Created June 18, 2013 04:58
An lldb command that prints the description of the exception being raised, when run while the target is stopped at the first instruction of objc_exception_throw. Many thanks to Enrico Granata and Sean Callanan for basically writing this to my specifications at WWDC 2013.
command script import ~/Library/lldb/sniff_objc_exception_throw.py
@mayoff
mayoff / .lldbinit
Created July 12, 2013 05:05
My .lldbinit for Cocoa Coders
# command alias rd expression -o -- [[UIApp keyWindow] recursiveDescription]
command regex rd 's/^[[:space:]]*$/po [[UIApp keyWindow] recursiveDescription]/' 's/^(.+)$/po [%1 recursiveDescription]/'
command regex rsd 's/.*/po [UIApp Rob_recursiveSceneDescription]/'
command script import ~/.../lldb/sniff_objc_exception_throw.py
command regex hcons 's/^(.+)$/po [(id)%1 constraintsAffectingLayoutForAxis:0]/'
command regex vcons 's/^(.+)$/po [(id)%1 constraintsAffectingLayoutForAxis:1]/'
@mayoff
mayoff / GenericPrepareForSegue.m
Created August 10, 2013 05:52
This is a generic implementation of prepareForSegue:sender: that calls a segue-specific method based on the segue identifier. There's also a similar implementation of shouldPerformSegueWithIdentifier:sender:.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSString *identifier = segue.identifier;
if (identifier.length == 0)
return;
NSString *selectorString = [NSString stringWithFormat:@"prepareFor%@Segue:sender:", identifier];
SEL selector = NSSelectorFromString(selectorString);
if (!selector || ![self respondsToSelector:selector])
return;
void (*method)(id, SEL, id, id) = (void (*)(id, SEL, id, id))[self methodForSelector:selector];
method(self, selector, segue, sender);