
#import "NSString+Trimming.h"
#import <Foundation/NSCharacterSet.h>
#import <stdio.h>

@implementation NSString (MiscTrimming)

// This method donated by Carl Lindberg
- (NSString *)stringByTrimmingLeadWhiteSpaces
{
        NSCharacterSet *nonSpaceSet;
        NSRange	validCharRange;

        nonSpaceSet    = [[NSCharacterSet whitespaceAndNewlineCharacterSet]
		invertedSet];
        validCharRange = [self rangeOfCharacterFromSet:nonSpaceSet];

        if (validCharRange.length == 0)
                return [NSString stringWithCString:""];
        else
                return [self substringFromIndex:validCharRange.location];
}

// This method donated by Carl Lindberg
- (NSString *)stringByTrimmingTailWhiteSpaces
{
        NSCharacterSet *nonSpaceSet;
        NSRange	validCharRange;

        nonSpaceSet    = [[NSCharacterSet whitespaceAndNewlineCharacterSet]
		invertedSet];
        validCharRange = [self rangeOfCharacterFromSet:nonSpaceSet
		options:NSBackwardsSearch];

        if (validCharRange.length == 0)
                return [NSString stringWithCString:""];
        else
                return [self substringToIndex:validCharRange.location+1];
}

// This method donated by Carl Lindberg
- (NSString *)stringByTrimmingWhiteSpaces
{
        return [[self stringByTrimmingLeadWhiteSpaces]
		stringByTrimmingTailWhiteSpaces];
}

// This method donated by Robert Todd Thomas
- (NSString *) stringByTrimmingLeadSpaces
{
        NSMutableString*	tempString;

        tempString = [[self mutableCopy] autorelease];
        [tempString trimLeadSpaces];
        // Make an immutable copy.
        return [[tempString copy] autorelease];
}

// This method donated by Robert Todd Thomas
- (NSString *) stringByTrimmingTailSpaces
{
        NSMutableString*	tempString;

        tempString = [[self mutableCopy] autorelease];
        [tempString trimTailSpaces];
        return [[tempString copy] autorelease];
}

// This method donated by Robert Todd Thomas
- (NSString *) stringByTrimmingSpaces
{
        NSMutableString*	tempString;

        tempString = [[self mutableCopy] autorelease];
        [tempString trimLeadSpaces];
        [tempString trimTailSpaces];
        return [[tempString copy] autorelease];
}

@end


@implementation NSMutableString (MiscMutableTrimming)

// This method donated by Robert Todd Thomas
- (void) trimLeadSpaces
/*"
Trims any leading spaces from our string value. If we are the empty
string or are completely made up of spaces then the result will be
the empty string.
"*/
{
	int location = 0;
	int length = [self length];
	
	while ((location < length) &&
			([self characterAtIndex:location] == ' ')) {
		location++;
	}
	
	// If there was at least one space..
	if (location > 0) {
		NSRange spaceRange;
		
		spaceRange.location = 0;
		spaceRange.length = location;
		[self deleteCharactersInRange:spaceRange];
	}
}

// This method donated by Robert Todd Thomas
- (void) trimTailSpaces
{
	int length = [self length];
	int location = length - 1;
	
	while ((location >= 0) &&
			([self characterAtIndex:location] == ' ')) {
		location--;
	}
	
	// If there was at least one space..
	if (location < length - 1) {
		NSRange spaceRange;
		
		// Different cases if all spaces..
		spaceRange.location = (location == 0) ? 0 : location + 1;
		spaceRange.length = length - ((location == 0) ? 0 : location + 1);
		[self deleteCharactersInRange:spaceRange];
	}
}

// This method donated by Robert Todd Thomas
- (void) trimSpaces
{
	[self trimLeadSpaces];
	[self trimTailSpaces];
}
	
@end

