
#import "NSString+MiscExtensions.h"
//#import <objc/objc.h>

@implementation NSString (MiscExtensions)

// This method donated by Robert Todd Thomas
- (BOOL) hasNonEmptyString
/*"
Returns YES if the string is not empty. This method is a good substitute
for something like if (aString != nil && ![aString isEqualToString:@""])
since if the receiver is nil or the string is empty NO is returned.
"*/
{
        if ([self isEqualToString:@""]) {
                return NO;
        }
        return YES;
}

- (SEL)selectorValue { return sel_getUid([self cString]); }

- (NSArray *)wordArray
{
	NSCharacterSet	*spaceSet    = [NSCharacterSet whitespaceAndNewlineCharacterSet];
	NSCharacterSet	*nonspaceSet = [spaceSet invertedSet];
	NSMutableArray	*wordArray   = [NSMutableArray array];
	NSScanner		*scanner     = [NSScanner scannerWithString:self];
	NSString		*aWord;

	[scanner setCharactersToBeSkipped:spaceSet];

	while (![scanner isAtEnd])
	{
		if ([scanner scanCharactersFromSet:nonspaceSet intoString:&aWord])
			[wordArray addObject:aWord];
	}

	return wordArray;
}

- (unsigned)wordCount
{
	NSCharacterSet	*spaceSet    = [NSCharacterSet whitespaceAndNewlineCharacterSet];
	NSCharacterSet	*nonspaceSet = [spaceSet invertedSet];
	NSScanner		*scanner     = [NSScanner scannerWithString:self];
	unsigned		count        = 0;

	[scanner setCharactersToBeSkipped:spaceSet];

	while (![scanner isAtEnd])
	{
		if ([scanner scanCharactersFromSet:nonspaceSet intoString:NULL])
			count++;
	}

	return count;
}

- (NSString *)wordNum:(unsigned)n
{
	NSCharacterSet	*spaceSet    = [NSCharacterSet whitespaceAndNewlineCharacterSet];
	NSCharacterSet	*nonspaceSet = [spaceSet invertedSet];
	NSScanner		*scanner     = [NSScanner scannerWithString:self];
	NSString		*aWord;
	int				count = 0;

	[scanner setCharactersToBeSkipped:spaceSet];

	while (![scanner isAtEnd])
	{
		if ([scanner scanCharactersFromSet:nonspaceSet intoString:&aWord])
		{
			if (n == count) return aWord;
		}
		
		count++;
	}

	return nil;
}

- (NSEnumerator *)wordEnumerator
{
	return [[self wordArray] objectEnumerator];
}

@end



@implementation NSMutableString (MiscExtensions)

// This method donated by Robert Todd Thomas
- (void) setCString:(const char*)aString
{
        [self setString:[NSString stringWithCString:aString]];
}

@end
