MTLReflection.m
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// MTLReflection.m
// Mantle
//
// Created by Justin Spahr-Summers on 2013-03-12.
// Copyright (c) 2013 GitHub. All rights reserved.
//
#import "MTLReflection.h"
#import <objc/runtime.h>
SEL MTLSelectorWithKeyPattern(NSString *key, const char *suffix) {
NSUInteger keyLength = [key maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSUInteger suffixLength = strlen(suffix);
char selector[keyLength + suffixLength + 1];
BOOL success = [key getBytes:selector maxLength:keyLength usedLength:&keyLength encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, key.length) remainingRange:NULL];
if (!success) return NULL;
memcpy(selector + keyLength, suffix, suffixLength);
selector[keyLength + suffixLength] = '\0';
return sel_registerName(selector);
}
SEL MTLSelectorWithCapitalizedKeyPattern(const char *prefix, NSString *key, const char *suffix) {
NSUInteger prefixLength = strlen(prefix);
NSUInteger suffixLength = strlen(suffix);
NSString *initial = [key substringToIndex:1].uppercaseString;
NSUInteger initialLength = [initial maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSString *rest = [key substringFromIndex:1];
NSUInteger restLength = [rest maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
char selector[prefixLength + initialLength + restLength + suffixLength + 1];
memcpy(selector, prefix, prefixLength);
BOOL success = [initial getBytes:selector + prefixLength maxLength:initialLength usedLength:&initialLength encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, initial.length) remainingRange:NULL];
if (!success) return NULL;
success = [rest getBytes:selector + prefixLength + initialLength maxLength:restLength usedLength:&restLength encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, rest.length) remainingRange:NULL];
if (!success) return NULL;
memcpy(selector + prefixLength + initialLength + restLength, suffix, suffixLength);
selector[prefixLength + initialLength + restLength + suffixLength] = '\0';
return sel_registerName(selector);
}