NSString+FLEX.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // NSString+FLEX.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 3/26/17.
  6. // Copyright © 2017 Tanner Bennett. All rights reserved.
  7. //
  8. #import "NSString+FLEX.h"
  9. @interface NSMutableString (Replacement)
  10. - (void)replaceOccurencesOfString:(NSString *)string with:(NSString *)replacement;
  11. - (void)removeLastKeyPathComponent;
  12. @end
  13. @implementation NSMutableString (Replacement)
  14. - (void)replaceOccurencesOfString:(NSString *)string with:(NSString *)replacement {
  15. [self replaceOccurrencesOfString:string withString:replacement options:0 range:NSMakeRange(0, self.length)];
  16. }
  17. - (void)removeLastKeyPathComponent {
  18. if (![self containsString:@"."]) {
  19. [self deleteCharactersInRange:NSMakeRange(0, self.length)];
  20. return;
  21. }
  22. BOOL putEscapesBack = NO;
  23. if ([self containsString:@"\\."]) {
  24. [self replaceOccurencesOfString:@"\\." with:@"\\~"];
  25. // Case like "UIKit\.framework"
  26. if (![self containsString:@"."]) {
  27. [self deleteCharactersInRange:NSMakeRange(0, self.length)];
  28. return;
  29. }
  30. putEscapesBack = YES;
  31. }
  32. // Case like "Bund" or "Bundle.cla"
  33. if (![self hasSuffix:@"."]) {
  34. NSUInteger len = self.pathExtension.length;
  35. [self deleteCharactersInRange:NSMakeRange(self.length-len, len)];
  36. }
  37. if (putEscapesBack) {
  38. [self replaceOccurencesOfString:@"\\~" with:@"\\."];
  39. }
  40. }
  41. @end
  42. @implementation NSString (FLEXTypeEncoding)
  43. - (NSCharacterSet *)flex_classNameAllowedCharactersSet {
  44. static NSCharacterSet *classNameAllowedCharactersSet = nil;
  45. static dispatch_once_t onceToken;
  46. dispatch_once(&onceToken, ^{
  47. NSMutableCharacterSet *temp = NSMutableCharacterSet.alphanumericCharacterSet;
  48. [temp addCharactersInString:@"_"];
  49. classNameAllowedCharactersSet = temp.copy;
  50. });
  51. return classNameAllowedCharactersSet;
  52. }
  53. - (BOOL)flex_typeIsConst {
  54. if (!self.length) return NO;
  55. return [self characterAtIndex:0] == FLEXTypeEncodingConst;
  56. }
  57. - (FLEXTypeEncoding)flex_firstNonConstType {
  58. if (!self.length) return FLEXTypeEncodingNull;
  59. return [self characterAtIndex:(self.flex_typeIsConst ? 1 : 0)];
  60. }
  61. - (FLEXTypeEncoding)flex_pointeeType {
  62. if (!self.length) return FLEXTypeEncodingNull;
  63. if (self.flex_firstNonConstType == FLEXTypeEncodingPointer) {
  64. return [self characterAtIndex:(self.flex_typeIsConst ? 2 : 1)];
  65. }
  66. return FLEXTypeEncodingNull;
  67. }
  68. - (BOOL)flex_typeIsObjectOrClass {
  69. FLEXTypeEncoding type = self.flex_firstNonConstType;
  70. return type == FLEXTypeEncodingObjcObject || type == FLEXTypeEncodingObjcClass;
  71. }
  72. - (Class)flex_typeClass {
  73. if (!self.flex_typeIsObjectOrClass) {
  74. return nil;
  75. }
  76. NSScanner *scan = [NSScanner scannerWithString:self];
  77. // Skip const
  78. [scan scanString:@"r" intoString:nil];
  79. // Scan leading @"
  80. if (![scan scanString:@"@\"" intoString:nil]) {
  81. return nil;
  82. }
  83. // Scan class name
  84. NSString *name = nil;
  85. if (![scan scanCharactersFromSet:self.flex_classNameAllowedCharactersSet intoString:&name]) {
  86. return nil;
  87. }
  88. // Scan trailing quote
  89. if (![scan scanString:@"\"" intoString:nil]) {
  90. return nil;
  91. }
  92. // Return found class
  93. return NSClassFromString(name);
  94. }
  95. - (BOOL)flex_typeIsNonObjcPointer {
  96. FLEXTypeEncoding type = self.flex_firstNonConstType;
  97. return type == FLEXTypeEncodingPointer ||
  98. type == FLEXTypeEncodingCString ||
  99. type == FLEXTypeEncodingSelector;
  100. }
  101. @end
  102. @implementation NSString (KeyPaths)
  103. - (NSString *)flex_stringByRemovingLastKeyPathComponent {
  104. if (![self containsString:@"."]) {
  105. return @"";
  106. }
  107. NSMutableString *mself = self.mutableCopy;
  108. [mself removeLastKeyPathComponent];
  109. return mself;
  110. }
  111. - (NSString *)flex_stringByReplacingLastKeyPathComponent:(NSString *)replacement {
  112. // replacement should not have any escaped '.' in it,
  113. // so we escape all '.'
  114. if ([replacement containsString:@"."]) {
  115. replacement = [replacement stringByReplacingOccurrencesOfString:@"." withString:@"\\."];
  116. }
  117. // Case like "Foo"
  118. if (![self containsString:@"."]) {
  119. return [replacement stringByAppendingString:@"."];
  120. }
  121. NSMutableString *mself = self.mutableCopy;
  122. [mself removeLastKeyPathComponent];
  123. [mself appendString:replacement];
  124. [mself appendString:@"."];
  125. return mself;
  126. }
  127. @end