Нет описания

AWSMTLReflection.m 2.0KB

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