Skip to content

Instantly share code, notes, and snippets.

View lawrencelomax's full-sized avatar

Lawrence Lomax lawrencelomax

View GitHub Profile
@lawrencelomax
lawrencelomax / scanner.swift
Created July 10, 2014 16:47
Word Scanner in Swift
// Playground - noun: a place where people can play
import Cocoa
let str = "Foo Bar bannana' basa fist ada-2@🚅 afa2-ff 👍🎭-asd👍 adsa😃da\ndas🎉d"
let whitespaceSet = NSCharacterSet.whitespaceAndNewlineCharacterSet()
enum ScanResult<T> {
case Append(T)
@lawrencelomax
lawrencelomax / gist:00ea2c00c9b6ca5bb4ab
Created September 5, 2014 11:30
Return Early vs Nested Imperative Parsing in Swift
static func decodeImperative<X: XMLParsableType>(xml: X) -> Result<Animal> {
if let type = XMLParser.parseChildText("type")(xml: xml).toOptional() {
if let name = XMLParser.parseChildText("name")(xml: xml).toOptional() {
if let urlString = XMLParser.parseChildText("url")(xml: xml).toOptional() {
return Result.value(self(type: type, name: name, url: NSURL.URLWithString(urlString)) )
}
return Result.Error(decodeError("Could not parse 'urlString' as a String"))
}
return Result.Error(decodeError("Could not parse 'name' as a String"))
}
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd;
@lawrencelomax
lawrencelomax / gist:b7a357d1d35a305d531d
Created June 22, 2015 14:21
Fun with Interposing
#import <Foundation/Foundation.h>
#import <objc/objc.h>
#import <objc/runtime.h>
#import <objc/message.h>
// Can use class-dump to get this from XCTest.framework
#import "XCTestConfiguration.h"
static void swizzleSetActiveTestConfiguration(void) {
Method method = class_getClassMethod(XCTestConfiguration.class, @selector(setActiveTestConfiguration:));
2015-09-25 10:23:18.449 xctest[80351:2903136] Port: <AVCaptureInputPort: 0x10053ce60 (AVCaptureScreenInput: 0x10053ca50) vide 0000 enabled>
2015-09-25 10:23:18.934 xctest[80351:2903717] <AVCaptureVideoDataOutput: 0x10053bcb0> Did Sample
2015-09-25 10:23:19.000 xctest[80351:2903572] <AVCaptureVideoDataOutput: 0x10053bcb0> Did Sample
2015-09-25 10:23:19.046 xctest[80351:2903572] <AVCaptureVideoDataOutput: 0x10053bcb0> Did Sample
2015-09-25 10:23:19.113 xctest[80351:2903717] <AVCaptureVideoDataOutput: 0x10053bcb0> Did Sample
2015-09-25 10:23:19.201 xctest[80351:2903717] <AVCaptureVideoDataOutput: 0x10053bcb0> Did Sample
2015-09-25 10:23:19.246 xctest[80351:2903717] <AVCaptureVideoDataOutput: 0x10053bcb0> Did Sample
2015-09-25 10:23:19.335 xctest[80351:2903572] <AVCaptureVideoDataOutput: 0x10053bcb0> Did Sample
2015-09-25 10:23:19.380 xctest[80351:2903717] <AVCaptureVideoDataOutput: 0x10053bcb0> Did Sample
2015-09-25 10:23:19.469 xctest[80351:2903572] <AVCaptureVideoDataOutput: 0x10053bcb0> Did Sample
@lawrencelomax
lawrencelomax / gist:1670617
Created January 24, 2012 15:09
Simple progressive image loading
UIView * view = [[UIView alloc] initWithFrame:CGRectMake( 0.0f, 0.0f, kAffirmationThumbnailWidth, kAffirmationThumbnailHeight )];
view.backgroundColor = [UIColor grayColor];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kAffirmationThumbnailWidth, kAffirmationThumbnailHeight)];
imageView.backgroundColor = [UIColor clearColor];
imageView.alpha = 1.0;
[view addSubview:imageView];
// Background getting of small image and loading into memory
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
NSString * imagePathSmall = [NSString stringWithFormat:@"%@_small.jpg",[[affirmation imagePath] stringByDeletingPathExtension]];
@lawrencelomax
lawrencelomax / gist:532d23905a27f97754b7
Created October 13, 2015 09:32
Xcode7 XCTest Launch
Oct 13 08:41:44 lawrencelom-mbp syncdefaultsd[10129]: (Note ) SYDAlwaysOnAccount: no account (null)
Oct 13 08:41:44 lawrencelom-mbp CoreSimulatorBridge[9632]: Requesting launch of com.example.apple-samplecode.TableSearch with options: {
arguments = (
"-NSTreatUnknownArgumentsAsOpen",
NO,
"-ApplePersistenceIgnoreState",
YES
);
environment = {
AppTargetLocation = "/Users/lawrencelomax/Library/Developer/Xcode/DerivedData/TableSearch-avrosnidtpyztnerixabsyhzbfvt/Build/Products/Debug-iphoneos/TableSearch.app/TableSearch";
@lawrencelomax
lawrencelomax / gist:4617449
Last active December 11, 2015 14:58
Variadic Macros for Pushing and Popping GL State
extern void bilContext_glClientState(dispatch_block_t contextBlock, GLenum clientState, ... )
{
va_list arguments;
GLenum value;
va_start(arguments, clientState);
for (value = clientState; value > 0; value = va_arg(arguments, GLenum))
{
glEnableClientState(value);
}
@lawrencelomax
lawrencelomax / gist:4704470
Last active December 12, 2015 03:08
Enumeration of NULL terminated Varargs
#define ENUMERATE_NULL_TERMINATED_VARARGS(firstArgument, type, argumentName, operation) \
{ \
va_list arguments; \
va_start(arguments, firstArgument); \
type argumentName = firstArgument; \
while (argumentName) \
{ \
{ \
operation \
} \
@lawrencelomax
lawrencelomax / gist:4711119
Last active December 12, 2015 03:58
ARC Retain/Release overhead in tight loops
/**
An inline (read: no overhead) function that will perform the physics increment for a given particle
@return A boolean for whether the particle has expired and needs to be removed
*/
extern inline BOOL updateParticle(BILParticleAtomConventional * atom, BILParticleModel * model, NSTimeInterval deltaTime)
{
// Time to Live
atom->atom.timeToLive -= deltaTime;