NSString-Addtions.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // NSString-Addtions.m
  3. // JasonDevelop
  4. //
  5. // Created by Jason Lee on 2/17/14.
  6. // Copyright (c) jasondevelop. All rights reserved.
  7. //
  8. #import "GTMNSString+HTML.h"
  9. #import "NSString-Addtions.h"
  10. @implementation NSString (Additions)
  11. - (BOOL)isEmptyString {
  12. if([self isEqualToString:@"<null>"] || [self isEqualToString:@"null"] || self == (id)[NSNull null] || [[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@""]) {
  13. return YES;
  14. }
  15. return NO;
  16. }
  17. - (BOOL)isNullString {
  18. if([self isEqualToString:@"<null>"] || [self isEqualToString:@"null"] || self == (id)[NSNull null])
  19. return YES;
  20. else
  21. return NO;
  22. }
  23. + (NSString *)stringWithUnsignedChar:(const unsigned char *)chr {
  24. return [NSString stringWithUTF8String:(char *)chr];
  25. }
  26. - (NSInteger)countForOccurrencesOfString:(NSString *)occurString {
  27. NSInteger count = 0, length = [self length];
  28. NSRange range = NSMakeRange(0, length);
  29. while(range.location != NSNotFound) {
  30. range = [self rangeOfString:occurString options:0 range:range];
  31. if(range.location != NSNotFound) {
  32. range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
  33. count++;
  34. }
  35. }
  36. return count;
  37. }
  38. #pragma mark - HTML
  39. // Decode all HTML entities using GTM
  40. - (NSString *)stringByDecodingHTMLEntities {
  41. return [self gtm_stringByUnescapingFromHTML];
  42. }
  43. // Encode all HTML entities using GTM
  44. - (NSString *)stringByEncodingHTMLEntities {
  45. return [self gtm_stringByEscapingForAsciiHTML];
  46. }
  47. // Strip HTML tags
  48. - (NSString *)stringByStrippingTags {
  49. // Find first & and short-cut if we can
  50. NSUInteger ampIndex = [self rangeOfString:@"<" options:NSLiteralSearch].location;
  51. if (ampIndex == NSNotFound) {
  52. return [NSString stringWithString:self]; // return copy of string as no tags found
  53. }
  54. // Scan and find all tags
  55. NSScanner *scanner = [NSScanner scannerWithString:self];
  56. [scanner setCharactersToBeSkipped:nil];
  57. NSMutableSet *tags = [[NSMutableSet alloc] init];
  58. NSString *tag;
  59. do {
  60. // Scan up to <
  61. tag = nil;
  62. [scanner scanUpToString:@"<" intoString:NULL];
  63. [scanner scanUpToString:@">" intoString:&tag];
  64. // Add to set
  65. if (tag) {
  66. NSString *t = [[NSString alloc] initWithFormat:@"%@>", tag];
  67. [tags addObject:t];
  68. }
  69. } while (![scanner isAtEnd]);
  70. // Strings
  71. NSMutableString *result = [[NSMutableString alloc] initWithString:self];
  72. NSString *finalString;
  73. // Replace tags
  74. NSString *replacement;
  75. for (NSString *t in tags) {
  76. // Replace tag with space unless it's an inline element
  77. replacement = @" ";
  78. if ([t isEqualToString:@"<a>"] ||
  79. [t isEqualToString:@"</a>"] ||
  80. [t isEqualToString:@"<span>"] ||
  81. [t isEqualToString:@"</span>"] ||
  82. [t isEqualToString:@"<strong>"] ||
  83. [t isEqualToString:@"</strong>"] ||
  84. [t isEqualToString:@"<em>"] ||
  85. [t isEqualToString:@"</em>"]) {
  86. replacement = @"";
  87. }
  88. // Replace
  89. [result replaceOccurrencesOfString:t
  90. withString:replacement
  91. options:NSLiteralSearch
  92. range:NSMakeRange(0, result.length)];
  93. }
  94. // Remove multi-spaces and line breaks
  95. finalString = [result stringByRemovingNewLinesAndWhitespace];
  96. // Cleanup & return
  97. return finalString;
  98. }
  99. // Replace newlines with <br /> tags
  100. - (NSString *)stringWithNewLinesAsBRs {
  101. // Strange New lines:
  102. // Next Line, U+0085
  103. // Form Feed, U+000C
  104. // Line Separator, U+2028
  105. // Paragraph Separator, U+2029
  106. // Scanner
  107. NSScanner *scanner = [[NSScanner alloc] initWithString:self];
  108. [scanner setCharactersToBeSkipped:nil];
  109. NSMutableString *result = [[NSMutableString alloc] init];
  110. NSString *temp;
  111. NSCharacterSet *newLineCharacters = [NSCharacterSet characterSetWithCharactersInString:
  112. [NSString stringWithFormat:@"\n\r%C%C%C%C", 0x0085, 0x000C, 0x2028, 0x2029]];
  113. // Scan
  114. do {
  115. // Get non new line characters
  116. temp = nil;
  117. [scanner scanUpToCharactersFromSet:newLineCharacters intoString:&temp];
  118. if (temp) [result appendString:temp];
  119. temp = nil;
  120. // Add <br /> s
  121. if ([scanner scanString:@"\r\n" intoString:nil]) {
  122. // Combine \r\n into just 1 <br />
  123. [result appendString:@"<br />"];
  124. } else if ([scanner scanCharactersFromSet:newLineCharacters intoString:&temp]) {
  125. // Scan other new line characters and add <br /> s
  126. if (temp) {
  127. for (int i = 0; i < temp.length; i++) {
  128. [result appendString:@"<br />"];
  129. }
  130. }
  131. }
  132. } while (![scanner isAtEnd]);
  133. // Cleanup & return
  134. NSString *retString = [NSString stringWithString:result];
  135. return retString;
  136. }
  137. // Remove newlines and white space from strong
  138. - (NSString *)stringByRemovingNewLinesAndWhitespace {
  139. // Strange New lines:
  140. // Next Line, U+0085
  141. // Form Feed, U+000C
  142. // Line Separator, U+2028
  143. // Paragraph Separator, U+2029
  144. // Scanner
  145. NSScanner *scanner = [[NSScanner alloc] initWithString:self];
  146. [scanner setCharactersToBeSkipped:nil];
  147. NSMutableString *result = [[NSMutableString alloc] init];
  148. NSString *temp;
  149. NSCharacterSet *newLineAndWhitespaceCharacters = [NSCharacterSet characterSetWithCharactersInString:
  150. [NSString stringWithFormat:@" \t\n\r%C%C%C%C", 0x0085, 0x000C, 0x2028, 0x2029]];
  151. // Scan
  152. while (![scanner isAtEnd]) {
  153. // Get non new line or whitespace characters
  154. temp = nil;
  155. [scanner scanUpToCharactersFromSet:newLineAndWhitespaceCharacters intoString:&temp];
  156. if (temp) [result appendString:temp];
  157. // Replace with a space
  158. if ([scanner scanCharactersFromSet:newLineAndWhitespaceCharacters intoString:NULL]) {
  159. if (result.length > 0 && ![scanner isAtEnd]) // Dont append space to beginning or end of result
  160. [result appendString:@" "];
  161. }
  162. }
  163. // Return
  164. NSString *retString = [NSString stringWithString:result];
  165. return retString;
  166. }
  167. @end