NSString+FLEX.m 4.0 KB

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