-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optimized functions for creating selectors using property keys
- Loading branch information
1 parent
66e186e
commit c7cea15
Showing
3 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// MTLReflection.h | ||
// Mantle | ||
// | ||
// Created by Justin Spahr-Summers on 2013-03-12. | ||
// Copyright (c) 2013 GitHub. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
// Creates a selector from a key and a constant string. | ||
// | ||
// key - The key to insert into the generated selector. This key should be in | ||
// its natural case. | ||
// suffix - A string to append to the key as part of the selector. | ||
// | ||
// Returns a selector, or NULL if the input strings cannot form a valid | ||
// selector. | ||
SEL MTLSelectorWithKeyPattern(NSString *key, NSString *suffix) __attribute__((pure, nonnull(1, 2))); | ||
|
||
// Creates a selector from a key and a constant prefix and suffix. | ||
// | ||
// prefix - A string to prepend to the key as part of the selector. | ||
// key - The key to insert into the generated selector. This key should be in | ||
// its natural case, and will have its first letter capitalized when | ||
// inserted. | ||
// suffix - A string to append to the key as part of the selector. | ||
// | ||
// Returns a selector, or NULL if the input strings cannot form a valid | ||
// selector. | ||
SEL MTLSelectorWithCapitalizedKeyPattern(NSString *prefix, NSString *key, NSString *suffix) __attribute__((pure, nonnull(1, 2, 3))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// 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, NSString *suffix) { | ||
NSUInteger keyLength = [key lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; | ||
NSUInteger suffixLength = [suffix lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; | ||
|
||
char selector[keyLength + suffixLength + 1]; | ||
memcpy(selector, key.UTF8String, keyLength); | ||
memcpy(selector + keyLength, suffix.UTF8String, suffixLength); | ||
selector[sizeof(selector) - 1] = '\0'; | ||
|
||
return sel_registerName(selector); | ||
} | ||
|
||
SEL MTLSelectorWithCapitalizedKeyPattern(NSString *prefix, NSString *key, NSString *suffix) { | ||
NSUInteger prefixLength = [prefix lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; | ||
NSUInteger suffixLength = [suffix lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; | ||
|
||
NSString *initial = [key substringToIndex:1].uppercaseString; | ||
NSUInteger initialLength = [initial lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; | ||
|
||
NSString *rest = [key substringFromIndex:1]; | ||
NSUInteger restLength = [rest lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; | ||
|
||
char selector[prefixLength + initialLength + restLength + suffixLength + 1]; | ||
memcpy(selector, prefix.UTF8String, prefixLength); | ||
memcpy(selector + prefixLength, initial.UTF8String, initialLength); | ||
memcpy(selector + prefixLength + initialLength, rest.UTF8String, restLength); | ||
memcpy(selector + prefixLength + initialLength + restLength, suffix.UTF8String, suffixLength); | ||
selector[sizeof(selector) - 1] = '\0'; | ||
|
||
return sel_registerName(selector); | ||
} |