// // NSString+UsefulStuff.m // // Duane Sibilly // 6/21/11 // Copyright (c) 2011-2012 Duane Sibilly. All rights reserved. #import "NSString+UsefulStuff.h" #import #define ELLIPSIS @"..." @interface NSString (UsefulStuffPrivate) +(BOOL) stringIsPalindrome:(NSString *)aString position:(NSInteger)position; @end @implementation NSString (UsefulStuff) +(NSString*) stringTruncatedToWidth:(CGFloat)width withString:(NSString *)string andFont:(UIFont *)font { return [string truncateToWidth:width withFont:font]; } +(BOOL) stringIsPalindrome:(NSString *)aString { return [NSString stringIsPalindrome:aString position:0]; } +(BOOL) stringIsPalindrome:(NSString *)aString position:(NSInteger)position { NSString *_string = [NSString stringWithString:aString]; NSInteger _position = position; if (! _string) { return NO; } NSInteger stringLength = [_string length]; NSString *firstChar = [[_string substringToIndex:_position] substringToIndex:1]; NSString *lastChar = [[_string substringToIndex:(stringLength - 1 - _position)] substringToIndex:1]; if (_position > (stringLength / 2)) { return YES; } if (! [firstChar isEqualToString:lastChar]) { return NO; } return [NSString stringIsPalindrome:_string position:(_position + 1)]; } -(NSString*) MD5Hash { // Create a C-style pointer to the UT8-encoded contents of the NSString const char *pointer = [self UTF8String]; // Create a buffer array big enough to hold the digest unsigned char buffer[CC_MD5_DIGEST_LENGTH]; // Create 16-byte MD5 hash value, store in buffer // See: CC_MD5(3cc) manpage on OS X & iOS. CC_MD5(pointer, strlen(pointer), buffer); // Convert MD5 digest in buffer to an autoreleased NSString of hexidecimal // values. NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i += 1) { [result appendFormat:@"%02x", buffer[i]]; } return [result copy]; } -(NSString*) truncateToWidth:(CGFloat)width withFont:(UIFont *)font { // Obtain a mutable copy of this NSString. NSMutableString *truncatedString = [self mutableCopy]; // If this NSString is longer than the desired width, truncate. if ([self sizeWithFont:font].width > width) { // Subtract an ellipsis' worth of width from the desired width to obtain the // truncation width. width -= [ELLIPSIS sizeWithFont:font].width; // While the string is longer than the truncation width, remove characters // from the end of the string. NSRange range = {truncatedString.length - 1, 1}; while ([truncatedString sizeWithFont:font].width > width) { [truncatedString deleteCharactersInRange:range]; range.location -= 1; } // Once truncation is complete, append an ellipsis to the end of the string. [truncatedString replaceCharactersInRange:range withString:ELLIPSIS]; } return [truncatedString copy]; } -(BOOL) isPalindrome { return [NSString stringIsPalindrome:self]; } -(NSString*) reverse { NSMutableString *reversedString = [NSMutableString stringWithCapacity:[self length]]; for (int i = ([self length] - 1); i >= 0; i -= 1) { [reversedString appendString:[NSString stringWithFormat:@"%C", [self characterAtIndex:i]]]; } return reversedString; } @end