NSString+YYAdd.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. //
  2. // NSString+YYAdd.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 13/4/3.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <UIKit/UIKit.h>
  12. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. Provide hash, encrypt, encode and some common method for 'NSString'.
  15. */
  16. @interface NSString (YYAdd)
  17. #pragma mark - Hash
  18. ///=============================================================================
  19. /// @name Hash
  20. ///=============================================================================
  21. /**
  22. Returns a lowercase NSString for md2 hash.
  23. */
  24. - (nullable NSString *)md2String;
  25. /**
  26. Returns a lowercase NSString for md4 hash.
  27. */
  28. - (nullable NSString *)md4String;
  29. /**
  30. Returns a lowercase NSString for md5 hash.
  31. */
  32. - (nullable NSString *)md5String;
  33. /**
  34. Returns a lowercase NSString for sha1 hash.
  35. */
  36. - (nullable NSString *)sha1String;
  37. /**
  38. Returns a lowercase NSString for sha224 hash.
  39. */
  40. - (nullable NSString *)sha224String;
  41. /**
  42. Returns a lowercase NSString for sha256 hash.
  43. */
  44. - (nullable NSString *)sha256String;
  45. /**
  46. Returns a lowercase NSString for sha384 hash.
  47. */
  48. - (nullable NSString *)sha384String;
  49. /**
  50. Returns a lowercase NSString for sha512 hash.
  51. */
  52. - (nullable NSString *)sha512String;
  53. /**
  54. Returns a lowercase NSString for hmac using algorithm md5 with key.
  55. @param key The hmac key.
  56. */
  57. - (nullable NSString *)hmacMD5StringWithKey:(NSString *)key;
  58. /**
  59. Returns a lowercase NSString for hmac using algorithm sha1 with key.
  60. @param key The hmac key.
  61. */
  62. - (nullable NSString *)hmacSHA1StringWithKey:(NSString *)key;
  63. /**
  64. Returns a lowercase NSString for hmac using algorithm sha224 with key.
  65. @param key The hmac key.
  66. */
  67. - (nullable NSString *)hmacSHA224StringWithKey:(NSString *)key;
  68. /**
  69. Returns a lowercase NSString for hmac using algorithm sha256 with key.
  70. @param key The hmac key.
  71. */
  72. - (nullable NSString *)hmacSHA256StringWithKey:(NSString *)key;
  73. /**
  74. Returns a lowercase NSString for hmac using algorithm sha384 with key.
  75. @param key The hmac key.
  76. */
  77. - (nullable NSString *)hmacSHA384StringWithKey:(NSString *)key;
  78. /**
  79. Returns a lowercase NSString for hmac using algorithm sha512 with key.
  80. @param key The hmac key.
  81. */
  82. - (nullable NSString *)hmacSHA512StringWithKey:(NSString *)key;
  83. /**
  84. Returns a lowercase NSString for crc32 hash.
  85. */
  86. - (nullable NSString *)crc32String;
  87. #pragma mark - Encode and decode
  88. ///=============================================================================
  89. /// @name Encode and decode
  90. ///=============================================================================
  91. /**
  92. Returns an NSString for base64 encoded.
  93. */
  94. - (nullable NSString *)base64EncodedString;
  95. /**
  96. Returns an NSString from base64 encoded string.
  97. @param base64Encoding The encoded string.
  98. */
  99. + (nullable NSString *)stringWithBase64EncodedString:(NSString *)base64EncodedString;
  100. /**
  101. URL encode a string in utf-8.
  102. @return the encoded string.
  103. */
  104. - (NSString *)stringByURLEncode;
  105. /**
  106. URL decode a string in utf-8.
  107. @return the decoded string.
  108. */
  109. - (NSString *)stringByURLDecode;
  110. /**
  111. Escape common HTML to Entity.
  112. Example: "a<b" will be escape to "a&lt;b".
  113. */
  114. - (NSString *)stringByEscapingHTML;
  115. #pragma mark - Drawing
  116. ///=============================================================================
  117. /// @name Drawing
  118. ///=============================================================================
  119. /**
  120. Returns the size of the string if it were rendered with the specified constraints.
  121. @param font The font to use for computing the string size.
  122. @param size The maximum acceptable size for the string. This value is
  123. used to calculate where line breaks and wrapping would occur.
  124. @param lineBreakMode The line break options for computing the size of the string.
  125. For a list of possible values, see NSLineBreakMode.
  126. @return The width and height of the resulting string's bounding box.
  127. These values may be rounded up to the nearest whole number.
  128. */
  129. - (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode;
  130. /**
  131. Returns the width of the string if it were to be rendered with the specified
  132. font on a single line.
  133. @param font The font to use for computing the string width.
  134. @return The width of the resulting string's bounding box. These values may be
  135. rounded up to the nearest whole number.
  136. */
  137. - (CGFloat)widthForFont:(UIFont *)font;
  138. /**
  139. Returns the height of the string if it were rendered with the specified constraints.
  140. @param font The font to use for computing the string size.
  141. @param width The maximum acceptable width for the string. This value is used
  142. to calculate where line breaks and wrapping would occur.
  143. @return The height of the resulting string's bounding box. These values
  144. may be rounded up to the nearest whole number.
  145. */
  146. - (CGFloat)heightForFont:(UIFont *)font width:(CGFloat)width;
  147. #pragma mark - Regular Expression
  148. ///=============================================================================
  149. /// @name Regular Expression
  150. ///=============================================================================
  151. /**
  152. Whether it can match the regular expression
  153. @param regex The regular expression
  154. @param options The matching options to report.
  155. @return YES if can match the regex; otherwise, NO.
  156. */
  157. - (BOOL)matchesRegex:(NSString *)regex options:(NSRegularExpressionOptions)options;
  158. /**
  159. Match the regular expression, and executes a given block using each object in the matches.
  160. @param regex The regular expression
  161. @param options The matching options to report.
  162. @param block The block to apply to elements in the array of matches.
  163. The block takes four arguments:
  164. match: The match substring.
  165. matchRange: The matching options.
  166. stop: A reference to a Boolean value. The block can set the value
  167. to YES to stop further processing of the array. The stop
  168. argument is an out-only argument. You should only ever set
  169. this Boolean to YES within the Block.
  170. */
  171. - (void)enumerateRegexMatches:(NSString *)regex
  172. options:(NSRegularExpressionOptions)options
  173. usingBlock:(void (^)(NSString *match, NSRange matchRange, BOOL *stop))block;
  174. /**
  175. Returns a new string containing matching regular expressions replaced with the template string.
  176. @param regex The regular expression
  177. @param options The matching options to report.
  178. @param replacement The substitution template used when replacing matching instances.
  179. @return A string with matching regular expressions replaced by the template string.
  180. */
  181. - (NSString *)stringByReplacingRegex:(NSString *)regex
  182. options:(NSRegularExpressionOptions)options
  183. withString:(NSString *)replacement;
  184. #pragma mark - NSNumber Compatible
  185. ///=============================================================================
  186. /// @name NSNumber Compatible
  187. ///=============================================================================
  188. // Now you can use NSString as a NSNumber.
  189. @property (readonly) char charValue;
  190. @property (readonly) unsigned char unsignedCharValue;
  191. @property (readonly) short shortValue;
  192. @property (readonly) unsigned short unsignedShortValue;
  193. @property (readonly) unsigned int unsignedIntValue;
  194. @property (readonly) long longValue;
  195. @property (readonly) unsigned long unsignedLongValue;
  196. @property (readonly) unsigned long long unsignedLongLongValue;
  197. @property (readonly) NSUInteger unsignedIntegerValue;
  198. #pragma mark - Utilities
  199. ///=============================================================================
  200. /// @name Utilities
  201. ///=============================================================================
  202. /**
  203. Returns a new UUID NSString
  204. e.g. "D1178E50-2A4D-4F1F-9BD3-F6AAB00E06B1"
  205. */
  206. + (NSString *)stringWithUUID;
  207. /**
  208. Returns a string containing the characters in a given UTF32Char.
  209. @param char32 A UTF-32 character.
  210. @return A new string, or nil if the character is invalid.
  211. */
  212. + (nullable NSString *)stringWithUTF32Char:(UTF32Char)char32;
  213. /**
  214. Returns a string containing the characters in a given UTF32Char array.
  215. @param char32 An array of UTF-32 character.
  216. @param length The character count in array.
  217. @return A new string, or nil if an error occurs.
  218. */
  219. + (nullable NSString *)stringWithUTF32Chars:(const UTF32Char *)char32 length:(NSUInteger)length;
  220. /**
  221. Enumerates the unicode characters (UTF-32) in the specified range of the string.
  222. @param range The range within the string to enumerate substrings.
  223. @param block The block executed for the enumeration. The block takes four arguments:
  224. char32: The unicode character.
  225. range: The range in receiver. If the range.length is 1, the character is in BMP;
  226. otherwise (range.length is 2) the character is in none-BMP Plane and stored
  227. by a surrogate pair in the receiver.
  228. stop: A reference to a Boolean value that the block can use to stop the enumeration
  229. by setting *stop = YES; it should not touch *stop otherwise.
  230. */
  231. - (void)enumerateUTF32CharInRange:(NSRange)range usingBlock:(void (^)(UTF32Char char32, NSRange range, BOOL *stop))block;
  232. /**
  233. Trim blank characters (space and newline) in head and tail.
  234. @return the trimmed string.
  235. */
  236. - (NSString *)stringByTrim;
  237. /**
  238. Add scale modifier to the file name (without path extension),
  239. From @"name" to @"name@2x".
  240. e.g.
  241. <table>
  242. <tr><th>Before </th><th>After(scale:2)</th></tr>
  243. <tr><td>"icon" </td><td>"icon@2x" </td></tr>
  244. <tr><td>"icon " </td><td>"icon @2x" </td></tr>
  245. <tr><td>"icon.top" </td><td>"icon.top@2x" </td></tr>
  246. <tr><td>"/p/name" </td><td>"/p/name@2x" </td></tr>
  247. <tr><td>"/path/" </td><td>"/path/" </td></tr>
  248. </table>
  249. @param scale Resource scale.
  250. @return String by add scale modifier, or just return if it's not end with file name.
  251. */
  252. - (NSString *)stringByAppendingNameScale:(CGFloat)scale;
  253. /**
  254. Add scale modifier to the file path (with path extension),
  255. From @"name.png" to @"name@2x.png".
  256. e.g.
  257. <table>
  258. <tr><th>Before </th><th>After(scale:2)</th></tr>
  259. <tr><td>"icon.png" </td><td>"icon@2x.png" </td></tr>
  260. <tr><td>"icon..png"</td><td>"icon.@2x.png"</td></tr>
  261. <tr><td>"icon" </td><td>"icon@2x" </td></tr>
  262. <tr><td>"icon " </td><td>"icon @2x" </td></tr>
  263. <tr><td>"icon." </td><td>"icon.@2x" </td></tr>
  264. <tr><td>"/p/name" </td><td>"/p/name@2x" </td></tr>
  265. <tr><td>"/path/" </td><td>"/path/" </td></tr>
  266. </table>
  267. @param scale Resource scale.
  268. @return String by add scale modifier, or just return if it's not end with file name.
  269. */
  270. - (NSString *)stringByAppendingPathScale:(CGFloat)scale;
  271. /**
  272. Return the path scale.
  273. e.g.
  274. <table>
  275. <tr><th>Path </th><th>Scale </th></tr>
  276. <tr><td>"icon.png" </td><td>1 </td></tr>
  277. <tr><td>"icon@2x.png" </td><td>2 </td></tr>
  278. <tr><td>"icon@2.5x.png" </td><td>2.5 </td></tr>
  279. <tr><td>"icon@2x" </td><td>1 </td></tr>
  280. <tr><td>"icon@2x..png" </td><td>1 </td></tr>
  281. <tr><td>"icon@2x.png/" </td><td>1 </td></tr>
  282. </table>
  283. */
  284. - (CGFloat)pathScale;
  285. /**
  286. nil, @"", @" ", @"\n" will Returns NO; otherwise Returns YES.
  287. */
  288. - (BOOL)isNotBlank;
  289. /**
  290. Returns YES if the target string is contained within the receiver.
  291. @param string A string to test the the receiver.
  292. @discussion Apple has implemented this method in iOS8.
  293. */
  294. - (BOOL)containsString:(NSString *)string;
  295. /**
  296. Returns YES if the target CharacterSet is contained within the receiver.
  297. @param set A character set to test the the receiver.
  298. */
  299. - (BOOL)containsCharacterSet:(NSCharacterSet *)set;
  300. /**
  301. Try to parse this string and returns an `NSNumber`.
  302. @return Returns an `NSNumber` if parse succeed, or nil if an error occurs.
  303. */
  304. - (nullable NSNumber *)numberValue;
  305. /**
  306. Returns an NSData using UTF-8 encoding.
  307. */
  308. - (nullable NSData *)dataValue;
  309. /**
  310. Returns NSMakeRange(0, self.length).
  311. */
  312. - (NSRange)rangeOfAll;
  313. /**
  314. Returns an NSDictionary/NSArray which is decoded from receiver.
  315. Returns nil if an error occurs.
  316. e.g. NSString: @"{"name":"a","count":2}" => NSDictionary: @[@"name":@"a",@"count":@2]
  317. */
  318. - (nullable id)jsonValueDecoded;
  319. /**
  320. Create a string from the file in main bundle (similar to [UIImage imageNamed:]).
  321. @param name The file name (in main bundle).
  322. @return A new string create from the file in UTF-8 character encoding.
  323. */
  324. + (nullable NSString *)stringNamed:(NSString *)name;
  325. @end
  326. NS_ASSUME_NONNULL_END