NSString+FLEX.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. - (BOOL)flex_typeIsObjectOrClass {
  62. FLEXTypeEncoding type = self.flex_firstNonConstType;
  63. return type == FLEXTypeEncodingObjcObject || type == FLEXTypeEncodingObjcClass;
  64. }
  65. - (Class)flex_typeClass {
  66. if (!self.flex_typeIsObjectOrClass) {
  67. return nil;
  68. }
  69. NSScanner *scan = [NSScanner scannerWithString:self];
  70. // Skip const
  71. [scan scanString:@"r" intoString:nil];
  72. // Scan leading @"
  73. if (![scan scanString:@"@\"" intoString:nil]) {
  74. return nil;
  75. }
  76. // Scan class name
  77. NSString *name = nil;
  78. if (![scan scanCharactersFromSet:self.flex_classNameAllowedCharactersSet intoString:&name]) {
  79. return nil;
  80. }
  81. // Scan trailing quote
  82. if (![scan scanString:@"\"" intoString:nil]) {
  83. return nil;
  84. }
  85. // Return found class
  86. return NSClassFromString(name);
  87. }
  88. - (BOOL)flex_typeIsNonObjcPointer {
  89. FLEXTypeEncoding type = self.flex_firstNonConstType;
  90. return type == FLEXTypeEncodingPointer ||
  91. type == FLEXTypeEncodingCString ||
  92. type == FLEXTypeEncodingSelector;
  93. }
  94. @end
  95. @implementation NSString (KeyPaths)
  96. - (NSString *)stringByRemovingLastKeyPathComponent {
  97. if (![self containsString:@"."]) {
  98. return @"";
  99. }
  100. NSMutableString *mself = self.mutableCopy;
  101. [mself removeLastKeyPathComponent];
  102. return mself;
  103. }
  104. - (NSString *)stringByReplacingLastKeyPathComponent:(NSString *)replacement {
  105. // replacement should not have any escaped '.' in it,
  106. // so we escape all '.'
  107. if ([replacement containsString:@"."]) {
  108. replacement = [replacement stringByReplacingOccurrencesOfString:@"." withString:@"\\."];
  109. }
  110. // Case like "Foo"
  111. if (![self containsString:@"."]) {
  112. return [replacement stringByAppendingString:@"."];
  113. }
  114. NSMutableString *mself = self.mutableCopy;
  115. [mself removeLastKeyPathComponent];
  116. [mself appendString:replacement];
  117. [mself appendString:@"."];
  118. return mself;
  119. }
  120. @end