Skip to content

Instantly share code, notes, and snippets.

@dsibilly
Created June 21, 2011 18:20
Show Gist options
  • Select an option

  • Save dsibilly/1038500 to your computer and use it in GitHub Desktop.

Select an option

Save dsibilly/1038500 to your computer and use it in GitHub Desktop.

Revisions

  1. dsibilly revised this gist May 26, 2012. 4 changed files with 83 additions and 315 deletions.
    80 changes: 40 additions & 40 deletions NSString+UsefulShit.h
    Original file line number Diff line number Diff line change
    @@ -1,49 +1,49 @@
    //
    // NSString+UsefulShit.h
    //
    // Created by Duane Sibilly on 6/21/11.
    // Copyright 2011 Duane Sibilly. All rights reserved.
    // NSString+UsefulStuff.h
    //
    // Duane Sibilly <duane@sibilly.com>
    // 6/21/11
    // Copyright (c) 2011-2012 Duane Sibilly. All rights reserved.

    #import <Foundation/Foundation.h>

    // The use of the parenthesis in the interface declaration is what tells the
    // compiler that this is a category on the NSString class instead of a
    // redefinition of NSString.
    @interface NSString (UsefulShit)
    @interface NSString (UsefulStuff)

    /**
    + (NSString*)stringTruncatedToWidth:withString:andFont:
    Generates a truncated copy of the given NSString, truncated to the desired
    width for the given typeface and size.
    width - A CGFloat representing the desired width of the truncated NSString.
    string - An NSString object with the content to be truncated.
    font - A UIFont object representing the desired typeface and font size.
    Example:
    NSString *message = @"Can you hear this long-winded message?";
    UIFont *messageFont = [UIFont fontWithName:@"Marker Felt" size:32];
    NSString *output = [NSString stringTruncatedToWidth:48.0f
    withString:message
    andFont:messageFont];
    NSString *message = @"Can you hear this long-winded message?";
    UIFont *messageFont = [UIFont fontWithName:@"Marker Felt" size:32];
    NSString *output = [NSString stringTruncatedToWidth:48.0f
    withString:message
    andFont:messageFont];
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    + (NSString*)stringTruncatedToWidth:(CGFloat)width
    withString:(NSString*)string
    andFont:(UIFont*)font;
    withString:(NSString*)string
    andFont:(UIFont*)font;


    /**
    + (NSString*)stringIsPalindrome:
    + (BOOL)stringIsPalindrome:
    Deterines if a provided NSString is a palindrome.
    aString - The NSString to be tested.
    Example:
    [NSString stringIsPalindrome:@"RADAR"]; // return YES
    [NSString stringIsPalindrome:@"RADAR"]; // return YES
    Returns a BOOL cooresponding to the NSString palindrome status.
    */
    + (BOOL)stringIsPalindrome:(NSString*)aString;
    @@ -52,10 +52,10 @@
    /**
    - (NSString*)MD5Hash
    Generates an MD5 cryptographic hash of this NSString's contents
    Example:
    NSString *hash = [@"The quick brown fox jumped over the lazy dog" MD5Hash];
    NSString *hash = [@"The quick brown fox jumped over the lazy dog" MD5Hash];
    Returns an NSString containing the hexidecimal representation of the MD5 hash.
    */
    - (NSString*)MD5Hash;
    @@ -65,16 +65,16 @@
    - (NSString*)truncateToWidth:withFont:
    Generates an NSString truncated to the indicated width
    for a given a typeface and size.
    width - A CGFloat representing the desired width of the truncated NSString.
    font - A UIFont object representing the desired typeface and font size.
    Example:
    NSString *testString = @"This string is too damn long!"
    [testString truncateToWidth:64.0f
    withFont:[UIFont fontWithName:@"Helvetica"
    size:28]];
    NSString *testString = @"This string is too damn long!"
    [testString truncateToWidth:64.0f
    withFont:[UIFont fontWithName:@"Helvetica"
    size:28]];
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    - (NSString*)truncateToWidth:(CGFloat)width withFont:(UIFont*)font;
    @@ -83,26 +83,26 @@
    /**
    - (BOOL)isPalindrome
    Determines whether this string is a palindrome.
    Example:
    [@"HANNAH" isPalindrome]; // returns YES
    [@"CLAUDE" isPalindrome]; // returns NO
    [@"HANNAH" isPalindrome]; // returns YES
    [@"CLAUDE" isPalindrome]; // returns NO
    Returns a BOOL corresponding to this NSString's palindrome status.
    */
    */
    - (BOOL)isPalindrome;


    /**
    -(NSString*)reverse
    Reverses the contents of this NSString.
    Example:
    NSString *testString = @"stressed";
    NSString *testReversed = [testString reverse]; // @"desserts"
    NSString *testString = @"stressed";
    NSString *testReversed = [testString reverse]; // @"desserts"
    Returns an autoreleased NSString with the original NSString's contents reversed
    Returns an NSString with the original NSString's contents reversed
    */
    - (NSString*)reverse;

    @end
    @end
    85 changes: 43 additions & 42 deletions NSString+UsefulShit.m
    Original file line number Diff line number Diff line change
    @@ -1,125 +1,126 @@
    //
    // NSString+UsefulShit.m
    //
    // Created by Duane Sibilly on 6/21/11.
    // Copyright 2011 Duane Sibilly. All rights reserved.
    // NSString+UsefulStuff.m
    //
    // Duane Sibilly <duane@sibilly.com>
    // 6/21/11
    // Copyright (c) 2011-2012 Duane Sibilly. All rights reserved.

    #import "NSString+UsefulShit.h"
    #import "NSString+UsefulStuff.h"
    #import <CommonCrypto/CommonDigest.h>

    #define ELLIPSIS @"..."

    // Like in the interface, the use of parentheses in the declaration implies that
    // this is a category to an existing class.
    @implementation NSString (UsefulShit)
    @interface NSString (UsefulStuffPrivate)

    +(BOOL) stringIsPalindrome:(NSString *)aString position:(NSInteger)position;

    @end

    + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font

    @implementation NSString (UsefulStuff)

    +(NSString*) stringTruncatedToWidth:(CGFloat)width withString:(NSString *)string andFont:(UIFont *)font
    {
    // Since the instance method truncateToWidth:withFont: already returns an
    // autoreleased truncated string, simply call that method on the supplied
    // NSString.
    return [string truncateToWidth:width withFont:font];
    }


    + (BOOL)stringIsPalindrome:(NSString*)aString
    +(BOOL) stringIsPalindrome:(NSString *)aString
    {
    return [NSString stringIsPalindrome:aString position:0];
    }

    // Undeclared helper class method for use with +(BOOL)stringIsPalindrome: and
    // -(BOOL)isPalindrome.
    + (BOOL)stringIsPalindrome:(NSString*)aString position:(NSInteger)position

    +(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 substringFromIndex:_position] substringToIndex:1];
    NSString *lastChar = [[_string substringFromIndex:(stringLength - 1 - _position)] substringToIndex:1];
    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
    -(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];
    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 output;

    return [result copy];
    }


    - (NSString*)truncateToWidth:(CGFloat)width withFont:(UIFont*)font
    -(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}
    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 autorelease];

    return [truncatedString copy];
    }


    - (BOOL)isPalindrome
    -(BOOL) isPalindrome
    {
    return [NSString stringIsPalindrome:self];
    }


    - (NSString*)reverse
    -(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
    @end
    108 changes: 0 additions & 108 deletions NSStringUsefulShit.h
    Original file line number Diff line number Diff line change
    @@ -1,108 +0,0 @@
    //
    // NSString+UsefulShit.h
    //
    // Created by Duane Sibilly on 6/21/11.
    // Copyright 2011 Duane Sibilly. All rights reserved.
    //

    #import <Foundation/Foundation.h>

    // The use of the parenthesis in the interface declaration is what tells the
    // compiler that this is a category on the NSString class instead of a
    // redefinition of NSString.
    @interface NSString (UsefulShit)

    /**
    + (NSString*)stringTruncatedToWidth:withString:andFont:
    Generates a truncated copy of the given NSString, truncated to the desired
    width for the given typeface and size.
    width - A CGFloat representing the desired width of the truncated NSString.
    string - An NSString object with the content to be truncated.
    font - A UIFont object representing the desired typeface and font size.
    Example:
    NSString *message = @"Can you hear this long-winded message?";
    UIFont *messageFont = [UIFont fontWithName:@"Marker Felt" size:32];
    NSString *output = [NSString stringTruncatedToWidth:48.0f
    withString:message
    andFont:messageFont];
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    + (NSString*)stringTruncatedToWidth:(CGFloat)width
    withString:(NSString*)string
    andFont:(UIFont*)font;


    /**
    + (BOOL)stringIsPalindrome:
    Deterines if a provided NSString is a palindrome.
    aString - The NSString to be tested.
    Example:
    [NSString stringIsPalindrome:@"RADAR"]; // return YES
    Returns a BOOL cooresponding to the NSString palindrome status.
    */
    + (BOOL)stringIsPalindrome:(NSString*)aString;


    /**
    - (NSString*)MD5Hash
    Generates an MD5 cryptographic hash of this NSString's contents
    Example:
    NSString *hash = [@"The quick brown fox jumped over the lazy dog" MD5Hash];
    Returns an NSString containing the hexidecimal representation of the MD5 hash.
    */
    - (NSString*)MD5Hash;


    /**
    - (NSString*)truncateToWidth:withFont:
    Generates an NSString truncated to the indicated width
    for a given a typeface and size.
    width - A CGFloat representing the desired width of the truncated NSString.
    font - A UIFont object representing the desired typeface and font size.
    Example:
    NSString *testString = @"This string is too damn long!"
    [testString truncateToWidth:64.0f
    withFont:[UIFont fontWithName:@"Helvetica"
    size:28]];
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    - (NSString*)truncateToWidth:(CGFloat)width withFont:(UIFont*)font;


    /**
    - (BOOL)isPalindrome
    Determines whether this string is a palindrome.
    Example:
    [@"HANNAH" isPalindrome]; // returns YES
    [@"CLAUDE" isPalindrome]; // returns NO
    Returns a BOOL corresponding to this NSString's palindrome status.
    */
    - (BOOL)isPalindrome;


    /**
    -(NSString*)reverse
    Reverses the contents of this NSString.
    Example:
    NSString *testString = @"stressed";
    NSString *testReversed = [testString reverse]; // @"desserts"
    Returns an autoreleased NSString with the original NSString's contents reversed
    */
    - (NSString*)reverse;

    @end
    125 changes: 0 additions & 125 deletions NSStringUsefulShit.m
    Original file line number Diff line number Diff line change
    @@ -1,125 +0,0 @@
    //
    // NSString+UsefulShit.m
    //
    // Created by Duane Sibilly on 6/21/11.
    // Copyright 2011 Duane Sibilly. All rights reserved.
    //

    #import "NSString+UsefulShit.h"
    #import <CommonCrypto/CommonDigest.h>

    #define ELLIPSIS @"..."

    // Like in the interface, the use of parentheses in the declaration implies that
    // this is a category to an existing class.
    @implementation NSString (UsefulShit)

    + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font
    {
    // Since the instance method truncateToWidth:withFont: already returns an
    // autoreleased truncated string, simply call that method on the supplied
    // NSString.
    return [string truncateToWidth:width withFont:font];
    }


    + (BOOL)stringIsPalindrome:(NSString*)aString
    {
    return [NSString stringIsPalindrome:aString position:0];
    }

    // Undeclared helper class method for use with +(BOOL)stringIsPalindrome: and
    // -(BOOL)isPalindrome.
    + (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 substringFromIndex:_position] substringToIndex:1];
    NSString *lastChar = [[_string substringFromIndex:(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 output;
    }


    - (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 autorelease];
    }


    - (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
  2. dsibilly revised this gist May 25, 2012. No changes.
  3. dsibilly revised this gist May 25, 2012. 2 changed files with 233 additions and 0 deletions.
    108 changes: 108 additions & 0 deletions NSStringUsefulShit.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,108 @@
    //
    // NSString+UsefulShit.h
    //
    // Created by Duane Sibilly on 6/21/11.
    // Copyright 2011 Duane Sibilly. All rights reserved.
    //

    #import <Foundation/Foundation.h>

    // The use of the parenthesis in the interface declaration is what tells the
    // compiler that this is a category on the NSString class instead of a
    // redefinition of NSString.
    @interface NSString (UsefulShit)

    /**
    + (NSString*)stringTruncatedToWidth:withString:andFont:
    Generates a truncated copy of the given NSString, truncated to the desired
    width for the given typeface and size.
    width - A CGFloat representing the desired width of the truncated NSString.
    string - An NSString object with the content to be truncated.
    font - A UIFont object representing the desired typeface and font size.
    Example:
    NSString *message = @"Can you hear this long-winded message?";
    UIFont *messageFont = [UIFont fontWithName:@"Marker Felt" size:32];
    NSString *output = [NSString stringTruncatedToWidth:48.0f
    withString:message
    andFont:messageFont];
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    + (NSString*)stringTruncatedToWidth:(CGFloat)width
    withString:(NSString*)string
    andFont:(UIFont*)font;


    /**
    + (BOOL)stringIsPalindrome:
    Deterines if a provided NSString is a palindrome.
    aString - The NSString to be tested.
    Example:
    [NSString stringIsPalindrome:@"RADAR"]; // return YES
    Returns a BOOL cooresponding to the NSString palindrome status.
    */
    + (BOOL)stringIsPalindrome:(NSString*)aString;


    /**
    - (NSString*)MD5Hash
    Generates an MD5 cryptographic hash of this NSString's contents
    Example:
    NSString *hash = [@"The quick brown fox jumped over the lazy dog" MD5Hash];
    Returns an NSString containing the hexidecimal representation of the MD5 hash.
    */
    - (NSString*)MD5Hash;


    /**
    - (NSString*)truncateToWidth:withFont:
    Generates an NSString truncated to the indicated width
    for a given a typeface and size.
    width - A CGFloat representing the desired width of the truncated NSString.
    font - A UIFont object representing the desired typeface and font size.
    Example:
    NSString *testString = @"This string is too damn long!"
    [testString truncateToWidth:64.0f
    withFont:[UIFont fontWithName:@"Helvetica"
    size:28]];
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    - (NSString*)truncateToWidth:(CGFloat)width withFont:(UIFont*)font;


    /**
    - (BOOL)isPalindrome
    Determines whether this string is a palindrome.
    Example:
    [@"HANNAH" isPalindrome]; // returns YES
    [@"CLAUDE" isPalindrome]; // returns NO
    Returns a BOOL corresponding to this NSString's palindrome status.
    */
    - (BOOL)isPalindrome;


    /**
    -(NSString*)reverse
    Reverses the contents of this NSString.
    Example:
    NSString *testString = @"stressed";
    NSString *testReversed = [testString reverse]; // @"desserts"
    Returns an autoreleased NSString with the original NSString's contents reversed
    */
    - (NSString*)reverse;

    @end
    125 changes: 125 additions & 0 deletions NSStringUsefulShit.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,125 @@
    //
    // NSString+UsefulShit.m
    //
    // Created by Duane Sibilly on 6/21/11.
    // Copyright 2011 Duane Sibilly. All rights reserved.
    //

    #import "NSString+UsefulShit.h"
    #import <CommonCrypto/CommonDigest.h>

    #define ELLIPSIS @"..."

    // Like in the interface, the use of parentheses in the declaration implies that
    // this is a category to an existing class.
    @implementation NSString (UsefulShit)

    + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font
    {
    // Since the instance method truncateToWidth:withFont: already returns an
    // autoreleased truncated string, simply call that method on the supplied
    // NSString.
    return [string truncateToWidth:width withFont:font];
    }


    + (BOOL)stringIsPalindrome:(NSString*)aString
    {
    return [NSString stringIsPalindrome:aString position:0];
    }

    // Undeclared helper class method for use with +(BOOL)stringIsPalindrome: and
    // -(BOOL)isPalindrome.
    + (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 substringFromIndex:_position] substringToIndex:1];
    NSString *lastChar = [[_string substringFromIndex:(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 output;
    }


    - (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 autorelease];
    }


    - (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
  4. dsibilly revised this gist Jun 21, 2011. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions NSString+UsefulShit.h
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,10 @@
    //
    // NSString+UsefulShit.h
    //
    // Created by Duane Sibilly on 6/21/11.
    // Copyright 2011 Duane Sibilly. All rights reserved.
    //

    #import <Foundation/Foundation.h>

    // The use of the parenthesis in the interface declaration is what tells the
  5. dsibilly revised this gist Jun 21, 2011. 2 changed files with 156 additions and 64 deletions.
    101 changes: 101 additions & 0 deletions NSString+UsefulShit.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    #import <Foundation/Foundation.h>

    // The use of the parenthesis in the interface declaration is what tells the
    // compiler that this is a category on the NSString class instead of a
    // redefinition of NSString.
    @interface NSString (UsefulShit)

    /**
    + (NSString*)stringTruncatedToWidth:withString:andFont:
    Generates a truncated copy of the given NSString, truncated to the desired
    width for the given typeface and size.
    width - A CGFloat representing the desired width of the truncated NSString.
    string - An NSString object with the content to be truncated.
    font - A UIFont object representing the desired typeface and font size.
    Example:
    NSString *message = @"Can you hear this long-winded message?";
    UIFont *messageFont = [UIFont fontWithName:@"Marker Felt" size:32];
    NSString *output = [NSString stringTruncatedToWidth:48.0f
    withString:message
    andFont:messageFont];
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    + (NSString*)stringTruncatedToWidth:(CGFloat)width
    withString:(NSString*)string
    andFont:(UIFont*)font;


    /**
    + (NSString*)stringIsPalindrome:
    Deterines if a provided NSString is a palindrome.
    aString - The NSString to be tested.
    Example:
    [NSString stringIsPalindrome:@"RADAR"]; // return YES
    Returns a BOOL cooresponding to the NSString palindrome status.
    */
    + (BOOL)stringIsPalindrome:(NSString*)aString;


    /**
    - (NSString*)MD5Hash
    Generates an MD5 cryptographic hash of this NSString's contents
    Example:
    NSString *hash = [@"The quick brown fox jumped over the lazy dog" MD5Hash];
    Returns an NSString containing the hexidecimal representation of the MD5 hash.
    */
    - (NSString*)MD5Hash;


    /**
    - (NSString*)truncateToWidth:withFont:
    Generates an NSString truncated to the indicated width
    for a given a typeface and size.
    width - A CGFloat representing the desired width of the truncated NSString.
    font - A UIFont object representing the desired typeface and font size.
    Example:
    NSString *testString = @"This string is too damn long!"
    [testString truncateToWidth:64.0f
    withFont:[UIFont fontWithName:@"Helvetica"
    size:28]];
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    - (NSString*)truncateToWidth:(CGFloat)width withFont:(UIFont*)font;


    /**
    - (BOOL)isPalindrome
    Determines whether this string is a palindrome.
    Example:
    [@"HANNAH" isPalindrome]; // returns YES
    [@"CLAUDE" isPalindrome]; // returns NO
    Returns a BOOL corresponding to this NSString's palindrome status.
    */
    - (BOOL)isPalindrome;


    /**
    -(NSString*)reverse
    Reverses the contents of this NSString.
    Example:
    NSString *testString = @"stressed";
    NSString *testReversed = [testString reverse]; // @"desserts"
    Returns an autoreleased NSString with the original NSString's contents reversed
    */
    - (NSString*)reverse;

    @end
    119 changes: 55 additions & 64 deletions NSString+UsefulShit.m
    Original file line number Diff line number Diff line change
    @@ -4,75 +4,17 @@
    // Created by Duane Sibilly on 6/21/11.
    // Copyright 2011 Duane Sibilly. All rights reserved.
    //
    //
    // Objective-C category demonstration
    // I've put the interface and implementation in the same file for the sake of
    // easy readability in a single gist.
    //

    #import <Foundation/Foundation.h>
    #import "NSString+UsefulShit.h"
    #import <CommonCrypto/CommonDigest.h>

    #define ELLIPSIS @"..."


    /* START CATEGORY INTERFACE */
    // The @interface would normally go in the header file, as in other C-like
    // languages. The use of the parenthesis in the interface declaration is what
    // tells the compiler that this is a category on the NSString class instead of a
    // redefinition of NSString.
    @interface NSString (UsefulShit)

    /**
    + (NSString*)stringTruncatedToWidth:withString:andFont:
    Generates a truncated copy of the given NSString, truncated to the desired
    width for the given typeface and size.
    width - A CGFloat representing the desired width of the truncated NSString.
    string - An NSString object with the content to be truncated.
    font - A UIFont object representing the desired typeface and font size.
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    + (NSString*)stringTruncatedToWidth:(CGFloat)width
    withString:(NSString*)string
    andFont:(UIFont*)font;

    /**
    - (NSString*)MD5Hash
    Generates an MD5 cryptographic hash of this NSString's contents
    Returns an NSString containing the hexidecimal representation of the MD5 hash.
    */
    - (NSString*)MD5Hash;


    /**
    Instance: truncateToWidth:withFont:
    Generates an NSString truncated to the indicated width
    for a given a typeface and size.
    width - A CGFloat representing the desired width of the truncated NSString.
    font - A UIFont object representing the desired typeface and font size.
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    - (NSString*)truncateToWdith:(CGFloat)width withFont:(UIFont*)font;

    @end
    /* END CATEGORY INTERFACE */


    /* START CATEGORY IMPLEMENTATION */
    // The implementation would usually live on its own in a .m file (.m is the
    // traditional file extension for Objective-C source, much like .c for C and
    // .cpp for C++. Like in the interface, the use of parentheses in the
    // declaration implies that this is a category to an existing class.
    // Like in the interface, the use of parentheses in the declaration implies that
    // this is a category to an existing class.
    @implementation NSString (UsefulShit)

    + (NSString*)stringTruncatedToWidth:(CGFloat)width
    withString:(NSString*)string
    andFont:(UIFont*)font
    + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font
    {
    // Since the instance method truncateToWidth:withFont: already returns an
    // autoreleased truncated string, simply call that method on the supplied
    @@ -81,7 +23,38 @@ + (NSString*)stringTruncatedToWidth:(CGFloat)width
    }


    - (NSString*)MD5Hash
    + (BOOL)stringIsPalindrome:(NSString*)aString
    {
    return [NSString stringIsPalindrome:aString position:0];
    }

    // Undeclared helper class method for use with +(BOOL)stringIsPalindrome: and
    // -(BOOL)isPalindrome.
    + (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 substringFromIndex:_position] substringToIndex:1];
    NSString *lastChar = [[_string substringFromIndex:(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];
    @@ -96,7 +69,7 @@ - (NSString*)MD5Hash
    // Convert MD5 digest in buffer to an autoreleased NSString of hexidecimal
    // values.
    NSMutableString *result =
    [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i += 1) {
    [result appendFormat:@"%02x", buffer[i]];
    }
    @@ -131,4 +104,22 @@ - (NSString*)truncateToWidth:(CGFloat)width withFont:(UIFont*)font
    return [truncatedString autorelease];
    }


    - (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
  6. dsibilly revised this gist Jun 21, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion NSString+UsefulShit.m
    Original file line number Diff line number Diff line change
    @@ -96,7 +96,7 @@ - (NSString*)MD5Hash
    // Convert MD5 digest in buffer to an autoreleased NSString of hexidecimal
    // values.
    NSMutableString *result =
    [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i += 1) {
    [result appendFormat:@"%02x", buffer[i]];
    }
  7. dsibilly revised this gist Jun 21, 2011. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions NSString+UsefulShit.m
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,9 @@ + (NSString*)stringTruncatedToWidth:withString:andFont:
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font;
    + (NSString*)stringTruncatedToWidth:(CGFloat)width
    withString:(NSString*)string
    andFont:(UIFont*)font;

    /**
    - (NSString*)MD5Hash
    @@ -68,7 +70,9 @@ - (NSString*)truncateToWdith:(CGFloat)width withFont:(UIFont*)font;
    // declaration implies that this is a category to an existing class.
    @implementation NSString (UsefulShit)

    + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font
    + (NSString*)stringTruncatedToWidth:(CGFloat)width
    withString:(NSString*)string
    andFont:(UIFont*)font
    {
    // Since the instance method truncateToWidth:withFont: already returns an
    // autoreleased truncated string, simply call that method on the supplied
  8. dsibilly revised this gist Jun 21, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion NSString+UsefulShit.m
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ + (NSString*)stringTruncatedToWidth:withString:andFont:
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont(UIFont*)font;
    + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font;

    /**
    - (NSString*)MD5Hash
  9. dsibilly created this gist Jun 21, 2011.
    130 changes: 130 additions & 0 deletions NSString+UsefulShit.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,130 @@
    //
    // NSString+UsefulShit.m
    //
    // Created by Duane Sibilly on 6/21/11.
    // Copyright 2011 Duane Sibilly. All rights reserved.
    //
    //
    // Objective-C category demonstration
    // I've put the interface and implementation in the same file for the sake of
    // easy readability in a single gist.
    //

    #import <Foundation/Foundation.h>
    #import <CommonCrypto/CommonDigest.h>

    #define ELLIPSIS @"..."


    /* START CATEGORY INTERFACE */
    // The @interface would normally go in the header file, as in other C-like
    // languages. The use of the parenthesis in the interface declaration is what
    // tells the compiler that this is a category on the NSString class instead of a
    // redefinition of NSString.
    @interface NSString (UsefulShit)

    /**
    + (NSString*)stringTruncatedToWidth:withString:andFont:
    Generates a truncated copy of the given NSString, truncated to the desired
    width for the given typeface and size.
    width - A CGFloat representing the desired width of the truncated NSString.
    string - An NSString object with the content to be truncated.
    font - A UIFont object representing the desired typeface and font size.
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont(UIFont*)font;

    /**
    - (NSString*)MD5Hash
    Generates an MD5 cryptographic hash of this NSString's contents
    Returns an NSString containing the hexidecimal representation of the MD5 hash.
    */
    - (NSString*)MD5Hash;


    /**
    Instance: truncateToWidth:withFont:
    Generates an NSString truncated to the indicated width
    for a given a typeface and size.
    width - A CGFloat representing the desired width of the truncated NSString.
    font - A UIFont object representing the desired typeface and font size.
    Returns an NSString containing the truncated string, followed by an ellipsis.
    */
    - (NSString*)truncateToWdith:(CGFloat)width withFont:(UIFont*)font;

    @end
    /* END CATEGORY INTERFACE */


    /* START CATEGORY IMPLEMENTATION */
    // The implementation would usually live on its own in a .m file (.m is the
    // traditional file extension for Objective-C source, much like .c for C and
    // .cpp for C++. Like in the interface, the use of parentheses in the
    // declaration implies that this is a category to an existing class.
    @implementation NSString (UsefulShit)

    + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font
    {
    // Since the instance method truncateToWidth:withFont: already returns an
    // autoreleased truncated string, simply call that method on the supplied
    // NSString.
    return [string truncateToWidth:width withFont:font];
    }


    - (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 output;
    }


    - (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 autorelease];
    }

    @end