Skip to content

Instantly share code, notes, and snippets.

View cxa's full-sized avatar
🦥

realazy cxa

🦥
View GitHub Profile
@rnapier
rnapier / digest.swift
Last active August 29, 2015 14:26
Associated types and type erasure for crypto digests http://stackoverflow.com/a/31748616/97337
// Just showing how to hide the underlying functions if you wanted to.
// Also makes some pieces a little less noisy
public struct DigestFunctions<Context> {
private var digester: (UnsafePointer<Void>, CC_LONG, UnsafeMutablePointer<UInt8>) -> UnsafeMutablePointer<UInt8>
private var updater: (UnsafeMutablePointer<Context>, UnsafePointer<Void>, CC_LONG) -> Int32
private var finalizer: (UnsafeMutablePointer<UInt8>, UnsafeMutablePointer<Context>) -> Int32
}
// Digests are reference types because they are stateful. Copying them may lead to confusing results.
public protocol Digest: class {
@jjgod
jjgod / fontinfo.c
Created January 12, 2011 13:03
Testing font unicode coverage info with FreeType OS/2 table, not very reliable though
#include <ft2build.h>
#include FT_FREETYPE_H
#include <freetype/tttables.h>
const char *rangeForBit(int i)
{
switch (i)
{
case 43:
@akisute
akisute / KeychainItemWrapper.h
Created April 23, 2011 05:57
KeychainItemWrapper from the sample code of GenericKeychain. Fixed a problem where the wrapper is unable to save 2 or more identifiers. http://akisute.com/2011/04/keychainitemwrapper-keychain-item.html
/*
File: KeychainItemWrapper.h
Abstract:
Objective-C wrapper for accessing a single keychain item.
Version: 1.2
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
@jjgod
jjgod / cascadelist.m
Created April 19, 2012 08:32
Use cascade list attribute to customize font fallback in Core Text
#import <ApplicationServices/ApplicationServices.h>
#import <Foundation/Foundation.h>
int main(int argc, char *argv[])
{
if (argc != 2)
return 0;
NSAutoreleasePool *pool = [NSAutoreleasePool new];
CFStringRef name = (CFStringRef) [NSString stringWithUTF8String: argv[1]];

This, is actually the wrong conclusion. The entire purpose of the “inner class” is to provide value semantics while maintaining effeciency of implementation.

Is that an opinion? Or was there a meeting when the "entire purpose" of inner classes was established and I wasn't invited? Sure, they can be used for efficiency, but they can also be used for other purposes, including inner mutation.

Just for starters, Mike Ash seems to have uncovered that inner classes are used inside the standard library to provide destructors:

Destruction can be solved by using a class, which provides deinit. The pointer can be destroyed there. class doesn't have value semantics, but we can solve this by using class for the implementation of the struct, and exposing the struct as the external interface to the array.

So to say that the "entire purpose" of inner classes is efficiency is I think plainly false; they are used for many reasons. E

@jaybaird
jaybaird / gist:6003951
Last active December 19, 2015 19:09
Binary Tree with NSArray
#import <Foundation/Foundation.h>
@interface Node : NSObject
@property (nonatomic) NSInteger identifier;
@property (nonatomic, copy) NSString *value;
+ (Node *)nodeWithIdentifier:(NSInteger)identifier value:(NSString *)value;
@end
@implementation Node
+ (Node *)nodeWithIdentifier:(NSInteger)identifier value:(NSString *)value {
(deftheme bubbleberry "Light Table like theme")
;; Based on the theme used for LightTable (see: http://www.chris-granger.com/images/lightable/main.png )
;; Source: https://gist.github.com/8065710
;; Original Source: https://gist.github.com/3027622
(custom-theme-set-variables
'bubbleberry
'(linum-format "%3i")
@peeblesjs
peeblesjs / gist:9288f79322ed3119ece4
Last active February 11, 2016 23:31
A naive "valueForKey:" operator in Swift
operator infix --> {}
func --> (instance: Any, key: String) -> Any? {
let mirror = reflect(instance)
for index in 0 ..< mirror.count {
let (childKey, childMirror) = mirror[index]
if childKey == key {
return childMirror.value
}
}
@steipete
steipete / NSData+PSPDFFoundation.m
Created July 16, 2015 08:08
After playing around with dispatch_io (https://gist.github.com/steipete/b22babbf3014e29c19f0), I ended up with this. Upside: Uses way less memory, controllable caching, similar performance, simpler code and supports priority donation implicitly since everything's sync.
static NSData *PSPDFCalculateSHA256FromFileURL(NSURL *fileURL, CC_LONG dataLength, NSError **error) {
NSCParameterAssert(fileURL);
NSData *shaData;
int fd = open(fileURL.path.UTF8String, O_RDONLY);
if (fd < 0) {
if (error) *error = [NSError pspdf_errorWithCode:PSPDFErrorCodeUnableToOpenPDF description:@"Failed to open file for calculating SHA256."];
return nil;
}
@odrobnik
odrobnik / gist:2106f26379fd609d4ed3
Created May 8, 2014 08:37
Toggle status bar and navigation bar together
- (IBAction)handleTap:(id)sender
{
BOOL isHiding = !_statusBarHidden;
_statusBarHidden = isHiding;
[UIView animateWithDuration:UINavigationControllerHideShowBarDuration delay:0 options:0
animations:^{
[self setNeedsStatusBarAppearanceUpdate];
}
completion:NULL];