Skip to content

Instantly share code, notes, and snippets.

View indragiek's full-sized avatar

Indragie Karunaratne indragiek

View GitHub Profile
@Catfish-Man
Catfish-Man / sethack.m
Created March 11, 2016 06:21
Demonstrating the trick of using stack-allocated mimics and sets for lookup tables instead of heap allocated keys and dictionaries
// Compile with clang -framework Foundation sethack.m
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
/*
CFHashBytes from http://www.opensource.apple.com/source/CF/CF-1153.18/CFUtilities.c
*/
#define ELF_STEP(B) T1 = (H << 4) + B; T2 = T1 & 0xF0000000; if (T2) T1 ^= (T2 >> 24); T1 &= (~T2); H = T1;
public final class StaticCStringArray {
private let storage: AnyForwardCollection<StaticString>
private lazy var pointers: ContiguousArray<UnsafeMutablePointer<Int8>> = {
ContiguousArray(self.storage.lazy.map { UnsafeMutablePointer($0.utf8Start) })
}()
init<Collection: CollectionType where Collection.Generator.Element == StaticString>(_ strings: Collection) {
self.storage = AnyForwardCollection(strings)
@shepting
shepting / NodeTraversal.swift
Last active November 24, 2015 22:55
Print out a tree of nodes with multiple children each.
//
// main.swift
// NodeTraversal
//
// Created by Steven Hepting on 11/16/15.
// Copyright © 2015 Steven Hepting. All rights reserved.
// Algorithm from http://bit.ly/1kSdSvc
//
import Foundation
//
// SimpleScrollingStack.swift
// A super-simple demo of a scrolling UIStackView in iOS 9
//
// Created by Paul Hudson on 10/06/2015.
// Learn Swift at www.hackingwithswift.com
// @twostraws
//
import UIKit
@JadenGeller
JadenGeller / Type-Level Assertions.swift
Last active March 9, 2018 03:06
Type-Level Assertions (or, almost-dependent types)
/*
* We've defined "traits" by which we can type an integer that are characteristic of its value.
* These traits can even be subtraits of other traits (like both positive and negative being nonzero).
*
* We can use these traits in the type signatures of functions to indicate what trait will be returned
* as a function of the passed-in traits.
*
* Even cooler, we can specify properties of the traits such that we can runtime-verify the correctness
* of these labels (in case a function was improperly annotated, for example).
*/
Your goals are to reduce the number of things that you have to keep in your head at any given moment, and to rely as little as possible on your own ability to consistently do things right.
If you make a thing immutable ('let' in swift), you never have to think about what happens if it changes, or what other parts of the code you'll effect if you change it.
If you split complex functions into several smaller functions that only interact by passing arguments or getting return values, then you limit the amount of code you need to consider when hunting for a bug, and you can test each small piece separately.
If you understand what things must be true in your code (aka invariants, for example "a person's age must be greater than 0"), and either provide no function that can cause them to be untrue, or check and crash immediately when they're untrue, then you don't have to debug issues caused by incorrect assumptions.
If you remove possibilities (for example, Swift removes the possibility of things being nil unless
@zwaldowski
zwaldowski / TextRanges.swift
Last active January 28, 2017 09:50
Proper bridging for NSRegularExpression; Swift 1.2
import Foundation
// MARK: String
extension String {
func convert(#index: Int) -> Index? {
let utfIndex = advance(utf16.startIndex, index, utf16.endIndex)
return utfIndex.samePositionIn(self)
}
#!/bin/bash
target_name="foo" # Remember to set your name here
target_email="foo@example.com" # Set the new email here
git filter-branch --commit-filter 'if [ "$GIT_AUTHOR_NAME" = $target_name ];
then export GIT_AUTHOR_EMAIL=$target_email;
fi; git commit-tree "$@"'
anonymous
anonymous / NSStringVariations.m
Created February 18, 2015 08:32
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
void (^benchmark)(const char *str) = ^(const char *str) {
const long count = 10000000;
NSDate *start = [NSDate date];
for (long i = 0; i < count; i++) {
(void)[[NSString alloc] initWithUTF8String:str];
}
@steipete
steipete / PSPDFEnvironment.m
Last active March 5, 2024 09:15
Example for DISPATCH_SOURCE_TYPE_MEMORYPRESSURE (Mac only, since 10.9) ... Since we share code between iOS and mac, I'm trying to be a good system citizen and reimplement the equivalent of UIApplicationDidReceiveMemoryWarningNotification on the Mac.
NSString *const PSPDFApplicationDidReceiveMemoryWarningNotification = @"PSPDFApplicationDidReceiveMemoryWarningNotification";
// Test with sudo memory_pressure -l critical. Don't forget to re-set afterwards!
__attribute__((constructor)) static void PSPDFInstallLowMemoryNotificationWarningMac(void) {
static dispatch_source_t source;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
source = dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0, DISPATCH_MEMORYPRESSURE_WARN|DISPATCH_MEMORYPRESSURE_CRITICAL, dispatch_get_main_queue());
dispatch_source_set_event_handler(source, ^{
dispatch_source_memorypressure_flags_t pressureLevel = dispatch_source_get_data(source);