Skip to content

Instantly share code, notes, and snippets.

@douglashill
douglashill / NSIndexSet+DHDescription.m
Created May 9, 2014 17:28
Concise string representations of index sets, like [4-5, 16, 19-21] or alternatively [4, 5, 16, 19, 20, 21]
static NSString * DHStringFromRange(NSRange range);
@implementation NSIndexSet (DHDescription)
- (NSString *)dh_description
{
return [self dh_descriptionShowingRanges:YES];
}
- (NSString *)dh_descriptionShowingRanges:(BOOL)shouldShowAsRanges
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSCountedSet *set = [NSCountedSet set];
NSError *error;
NSString *corpus = [NSString stringWithContentsOfFile:@"some-file.txt" encoding:NSUTF8StringEncoding error:&error];
[corpus enumerateSubstringsInRange:NSMakeRange(0, [corpus length]) options:NSStringEnumerationByWords usingBlock:^(NSString *word, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[set addObject:word];
}];
@douglashill
douglashill / gist:644f3b468347f2bc1f77
Created June 27, 2014 18:09
A rough guess at the implementation of +[NSBundle preferredLocalizationsFromArray:]
+ (NSArray *)dh_preferredLocalizationsFromArray:(NSArray *)requestedLocalisations
{
NSMutableSet *possibleLocalisations = [NSMutableSet setWithArray:requestedLocalisations];
BOOL shouldLimitToAvailableLocalisations = YES; // Set to YES to match what NSBundle seems to do, or NO to be more like the documentation.
if (shouldLimitToAvailableLocalisations) {
[possibleLocalisations intersectSet:[NSSet setWithArray:[[NSBundle mainBundle] localizations]]];
}
@douglashill
douglashill / print-Core-Graphics.m
Last active August 29, 2015 14:03
Nicer than NSStringFromCGSize and friends
NSString *DHStringFromFloat(CGFloat value)
{
if (value == CGFLOAT_MAX) return @"max";
if (value == CGFLOAT_MIN) return @"min";
return [NSString stringWithFormat:@"%g", value];
}
NSString *DHStringFromPoint_bare(CGPoint point)
{
return [NSString stringWithFormat:@"%@, %@", DHStringFromFloat(point.x), DHStringFromFloat(point.y)];
@douglashill
douglashill / HTMLWriter.swift
Created July 9, 2014 16:57
Incomplete, but a promising start. I like either types.
// HTMLWriter.swift
// Douglas Hill, June 2014
// HTML syntax documentation: http://www.w3.org/TR/html-markup/syntax.html
enum HTMLContent {
case Element(HTMLElement)
case Text(String)
}
input = "NSHandlesContentAsCompoundValueBindingOption"
list = input.split /(?=[A-Z][a-z])/
if list.first.upcase == list.first
list.shift
end
puts list.map {|item| item.downcase}.join " "
@douglashill
douglashill / NSObject+DHRecursiveDescription.m
Last active August 29, 2015 14:06
NSObject recursive description category, for debugging trees (subviews, sublayers, child view controllers) or chains (superviews, next responders, superclasses).
@interface NSObject (DHRecursiveDescription)
- (NSString *)dh_recursiveDescriptionWithRecursion:(id (^)(id obj))recursion;
@end
@implementation NSObject (DHRecursiveDescription)
- (NSString *)dh_recursiveDescriptionWithRecursion:(id (^)(id obj))recursion
{
@implementation NSArray (DHShortDescription)
- (NSString *)dh_oneLineDescription
{
return [NSString stringWithFormat:@"[%@]", [self componentsJoinedByString:@", "]];
}
@end
#import <Foundation/Foundation.h>
void printInManyEncodings(u_int8_t hex)
{
NSData *const data = [NSData dataWithBytes:&hex length:sizeof(hex)];
NSDictionary *const encodings = @{
@"ASCII" : @(NSASCIIStringEncoding),
@"NEXTSTEP" : @(NSNEXTSTEPStringEncoding),
@"JapaneseEUC" : @(NSJapaneseEUCStringEncoding),
static void repeat(NSUInteger repeatCount, void (^blockToRepeat)(void))
{
if (blockToRepeat == nil) {
return;
}
while (repeatCount--) {
blockToRepeat();
}
}