// // NSString+YYAdd.h // YYKit // // Created by ibireme on 13/4/3. // Copyright (c) 2015 ibireme. // // This source code is licensed under the MIT-style license found in the // LICENSE file in the root directory of this source tree. // #import NS_ASSUME_NONNULL_BEGIN /** Provide hash, encrypt, encode and some common method for 'NSString'. */ @interface NSString (YYAdd) #pragma mark - Hash ///============================================================================= /// @name Hash ///============================================================================= /** Returns a lowercase NSString for md2 hash. */ - (nullable NSString *)md2String; /** Returns a lowercase NSString for md4 hash. */ - (nullable NSString *)md4String; /** Returns a lowercase NSString for md5 hash. */ - (nullable NSString *)md5String; /** Returns a lowercase NSString for sha1 hash. */ - (nullable NSString *)sha1String; /** Returns a lowercase NSString for sha224 hash. */ - (nullable NSString *)sha224String; /** Returns a lowercase NSString for sha256 hash. */ - (nullable NSString *)sha256String; /** Returns a lowercase NSString for sha384 hash. */ - (nullable NSString *)sha384String; /** Returns a lowercase NSString for sha512 hash. */ - (nullable NSString *)sha512String; /** Returns a lowercase NSString for hmac using algorithm md5 with key. @param key The hmac key. */ - (nullable NSString *)hmacMD5StringWithKey:(NSString *)key; /** Returns a lowercase NSString for hmac using algorithm sha1 with key. @param key The hmac key. */ - (nullable NSString *)hmacSHA1StringWithKey:(NSString *)key; /** Returns a lowercase NSString for hmac using algorithm sha224 with key. @param key The hmac key. */ - (nullable NSString *)hmacSHA224StringWithKey:(NSString *)key; /** Returns a lowercase NSString for hmac using algorithm sha256 with key. @param key The hmac key. */ - (nullable NSString *)hmacSHA256StringWithKey:(NSString *)key; /** Returns a lowercase NSString for hmac using algorithm sha384 with key. @param key The hmac key. */ - (nullable NSString *)hmacSHA384StringWithKey:(NSString *)key; /** Returns a lowercase NSString for hmac using algorithm sha512 with key. @param key The hmac key. */ - (nullable NSString *)hmacSHA512StringWithKey:(NSString *)key; /** Returns a lowercase NSString for crc32 hash. */ - (nullable NSString *)crc32String; #pragma mark - Encode and decode ///============================================================================= /// @name Encode and decode ///============================================================================= /** Returns an NSString for base64 encoded. */ - (nullable NSString *)base64EncodedString; /** Returns an NSString from base64 encoded string. @param base64Encoding The encoded string. */ + (nullable NSString *)stringWithBase64EncodedString:(NSString *)base64EncodedString; /** URL encode a string in utf-8. @return the encoded string. */ - (NSString *)stringByURLEncode; /** URL decode a string in utf-8. @return the decoded string. */ - (NSString *)stringByURLDecode; /** Escape common HTML to Entity. Example: "a Before After(scale:2) "icon" "icon@2x" "icon " "icon @2x" "icon.top" "icon.top@2x" "/p/name" "/p/name@2x" "/path/" "/path/" @param scale Resource scale. @return String by add scale modifier, or just return if it's not end with file name. */ - (NSString *)stringByAppendingNameScale:(CGFloat)scale; /** Add scale modifier to the file path (with path extension), From @"name.png" to @"name@2x.png". e.g.
Before After(scale:2)
"icon.png" "icon@2x.png"
"icon..png""icon.@2x.png"
"icon" "icon@2x"
"icon " "icon @2x"
"icon." "icon.@2x"
"/p/name" "/p/name@2x"
"/path/" "/path/"
@param scale Resource scale. @return String by add scale modifier, or just return if it's not end with file name. */ - (NSString *)stringByAppendingPathScale:(CGFloat)scale; /** Return the path scale. e.g.
Path Scale
"icon.png" 1
"icon@2x.png" 2
"icon@2.5x.png" 2.5
"icon@2x" 1
"icon@2x..png" 1
"icon@2x.png/" 1
*/ - (CGFloat)pathScale; /** nil, @"", @" ", @"\n" will Returns NO; otherwise Returns YES. */ - (BOOL)isNotBlank; /** Returns YES if the target string is contained within the receiver. @param string A string to test the the receiver. @discussion Apple has implemented this method in iOS8. */ - (BOOL)containsString:(NSString *)string; /** Returns YES if the target CharacterSet is contained within the receiver. @param set A character set to test the the receiver. */ - (BOOL)containsCharacterSet:(NSCharacterSet *)set; /** Try to parse this string and returns an `NSNumber`. @return Returns an `NSNumber` if parse succeed, or nil if an error occurs. */ - (nullable NSNumber *)numberValue; /** Returns an NSData using UTF-8 encoding. */ - (nullable NSData *)dataValue; /** Returns NSMakeRange(0, self.length). */ - (NSRange)rangeOfAll; /** Returns an NSDictionary/NSArray which is decoded from receiver. Returns nil if an error occurs. e.g. NSString: @"{"name":"a","count":2}" => NSDictionary: @[@"name":@"a",@"count":@2] */ - (nullable id)jsonValueDecoded; /** Create a string from the file in main bundle (similar to [UIImage imageNamed:]). @param name The file name (in main bundle). @return A new string create from the file in UTF-8 character encoding. */ + (nullable NSString *)stringNamed:(NSString *)name; @end NS_ASSUME_NONNULL_END