Skip to content

Instantly share code, notes, and snippets.

View jhurliman's full-sized avatar
🐨

John Hurliman jhurliman

🐨
View GitHub Profile
@jhurliman
jhurliman / compileconcatenation.m
Created June 28, 2014 08:04
Compile-time string concatenation in Objective-C
#ifdef DEBUG
#define kAPIEndpointHost @"http://example.dev"
#else
#define kAPIEndpointHost @"http://www.example.com"
#endif
#define kAPIEndpointLatest (kAPIEndpointHost @"/api/latest_content")
#define kAPIEndpointMostPopular (kAPIEndpointHost @"/api/most_popular")
@jhurliman
jhurliman / const.c
Created June 29, 2014 05:27
Quick explanation of how to read const declarations in C-based languages
const char *s; // read as "s is a pointer to a char that is constant"
char c;
char *const t = &c; // read as "t is a constant pointer to a char"
@jhurliman
jhurliman / objc-pre-post-loops.asm
Last active August 29, 2015 14:04
LLVM does not care about your C optimizations
## for (int i = 0; i < 10; i++) { }
LBL1:
cmpl $10, -4(%ebp) ## -4(%ebp) is the stack address of i. Check if it is less than 10
jge LBL3 ## If the previous compare had a greater than or equal to result, exit the loop
jmp LBL2 ## Otherwise, jump to the next address (this instruction will get optimized away)
LBL2:
movl -4(%ebp), %eax ## Load i into the EAX register (where the arithmetic happens!)
addl $1, %eax ## Add 1 to the EAX register
movl %eax, -4(%ebp) ## Copy the EAX register back on to the stack at the address for i.
@jhurliman
jhurliman / UIViewController+SegueData.m
Last active August 29, 2015 14:07
UIViewController+SegueData.m
//
// UIViewController+SegueData.h
// SegueDataPassing - Helper method for performing a segue and passing data to
// the destination view controller.
//
// Created by John Hurliman on 10/21/14.
// Copyright (c) 2014 John Hurliman. Released under the MIT license.
// http://opensource.org/licenses/MIT
//
// List all font families and font names in iOS
for (NSString *familyName in UIFont.familyNames) {
NSLog(@"%@", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(@" %@", fontName);
}
}
@jhurliman
jhurliman / rgb2lab.fsh
Created December 29, 2014 23:53
rgb2lab.fsh
varying vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
vec3 rgb2xyz(vec3 c) {
vec3 tmp;
tmp.x = (c.r > 0.04045) ? pow( (c.r + 0.055) / 1.055, 2.4) : c.r / 12.92;
tmp.y = (c.g > 0.04045) ? pow( (c.g + 0.055) / 1.055, 2.4) : c.g / 12.92;
tmp.z = (c.b > 0.04045) ? pow( (c.b + 0.055) / 1.055, 2.4) : c.b / 12.92;
@jhurliman
jhurliman / lab2rgb.fsh
Created December 29, 2014 23:55
lab2rgb.fsh
varying vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
vec3 lab2xyz(vec3 c) {
float fy = (c.x + 16.0) / 116.0;
float fx = c.y / 500.0 + fy;
float fz = fy - c.z / 200.0;
return vec3( 95.047 * ((fx > 0.206897) ? fx * fx * fx : (fx - 16.0 / 116.0) / 7.787),
@jhurliman
jhurliman / firebaseID.js
Created January 5, 2015 22:33
Generate Firebase IDs using the same approach as Firebase.push()
var ALPHABET = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
function generateFirebaseID() {
var timestamp = +new Date();
var chars = Array(20);
for (var i = 7; 0 <= i; i--) {
chars[i] = ALPHABET.charAt(timestamp % 64);
timestamp = Math.floor(timestamp / 64);
}
@jhurliman
jhurliman / isIPhone6OrNewer.m
Created February 7, 2015 01:16
isIPhone6OrNewer.m
#include <sys/utsname.h>
+ (BOOL)isIPhone6OrNewer
{
static int major = -1;
if (major == -1) {
struct utsname unameData;
if (0 != uname(&unameData)) {
major = 0;
@jhurliman
jhurliman / isValidEmail.m
Created March 16, 2015 20:59
Email Validation in Objective-C
+ (BOOL)isValidEmail:(NSString *)str
{
NSString *emailRegex = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:str];
}