Paste number 71193: Poor, poor runtime.

Paste number 71193: Poor, poor runtime.
Pasted by: Ahruman
When:1 year, 8 months ago
Share:Tweet this! | http://paste.lisp.org/+1IXL
Channel:#iphonedev
Paste contents:
Raw Source | XML | Display As
// For more fun with runtime hackery, see http://paste.lisp.org/display/55627 .
// I take no responsibility if you're silly enough to actually use this.

// SillyString.h:
#import <Foundation/Foundation.h>


@interface NSString (JASillyString)

-:a;
-:a:b;
-:a:b:c;
-:a:b:c:d;
-:a:b:c:d:e;
-:a:b:c:d:e:f;
-:a:b:c:d:e:f:g;
-:a:b:c:d:e:f:g:h;
-:a:b:c:d:e:f:g:h:i;
-:a:b:c:d:e:f:g:h:i:j;
-:a:b:c:d:e:f:g:h:i:j:k;
-:a:b:c:d:e:f:g:h:i:j:k:l;
-:a:b:c:d:e:f:g:h:i:j:k:l:m;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y;
-:a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z;

@end


// SillyString.m:
#import "SillyString.h"
#import <objc/runtime.h>
#import <stdarg.h>


static BOOL IsColonOnlySelector(SEL selector);
static NSUInteger ColonCount(SEL selector);
static NSString *SillyStringImplementation(id self, SEL _cmd, ...);


@implementation NSString (JASillyStringImpl)

+ (BOOL)resolveInstanceMethod:(SEL)sel
{
	if (IsColonOnlySelector(sel))
	{
		NSUInteger i, colonCount = ColonCount(sel);
		NSMutableString *typeStr = [NSMutableString stringWithCapacity:colonCount + 3];
		[typeStr appendString:@"@@:"];
		for (i = 0; i != colonCount; ++i)
		{
			[typeStr appendString:@"@"];
		}
		
		return class_addMethod([self class], sel, (IMP)SillyStringImplementation, typeStr.UTF8String);
	}
	
	else return [super resolveClassMethod:sel];
}

@end


static BOOL IsColonOnlySelector(SEL selector)
{
	NSString *selString = NSStringFromSelector(selector);
	NSUInteger i, count = selString.length;
	for (i = 0; i < count; ++i)
	{
		if ([selString characterAtIndex:i] != ':')  return NO;
	}
	
	return YES;
}


static NSUInteger ColonCount(SEL selector)
{
	assert(IsColonOnlySelector(selector));
	return NSStringFromSelector(selector).length;
}


static NSString *SillyStringImplementation(id self, SEL _cmd, ...)
{
	NSUInteger i, count = ColonCount(_cmd);
	NSMutableString *result = [[self mutableCopy] autorelease];
	
	va_list args;
	id obj = nil;
	va_start(args, _cmd);
	for (i = 0; i != count; ++i)
	{
		obj = va_arg(args, id);
		if (obj == nil)  obj = @"";
		[result appendString:[obj description]];
	}
	va_end(args);
	
	return result;
}

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.