//
//  MHString.m
//  Hootenanny
//
//  Created by Noah Sorscher on 12/18/04.
//  Copyright 2004 __MyCompanyName__. All rights reserved.
//

#import "MHString.h"


@implementation NSString (MHString)

- (void)getSafeCString:(void *)buffer
{
	NSMutableString *s = [NSMutableString stringWithCapacity:[self length]];
	unichar uch = 0;
	int i, n = [self length];
	for (i=0;i<n;i++) {
		uch = [self characterAtIndex:i];
		if (uch < 128) {
			[s appendFormat:@"%c", uch];
		} else {
			switch (uch) {
				case 145: case 146: [s appendFormat:@"%c", 39]; break;
				default: break;
			}
		}
	}
	[s getCString:buffer];
}

- (BOOL) allNumbers
{
    int i, n = [self length];
    for (i=0;i<n;i++) {
        char ch = [self characterAtIndex:i];
        if (ch != '.' && (ch < '0' || ch > '9')) return NO;
    }
    return YES;
}

- (BOOL) allInts
{
    int i, n = [self length];
    for (i=0;i<n;i++) {
        char ch = [self characterAtIndex:i];
        if (ch < '0' || ch > '9') return NO;
    }
    return YES;
}

- (BOOL) hasExtension:(NSArray *)exts
{
    int i, n = [exts count];
    for (i=0;i<n;i++) {
        NSString *ext = [[exts objectAtIndex:i] lowercaseString];
        NSString *me = [self lowercaseString];
        NSString *last = [[self componentsSeparatedByString:@" "] lastObject];
        if ([last allNumbers]) me = [me substringToIndex:[me length] - [last length] - 1];
        if ([me hasSuffix:[NSString stringWithFormat:@".%@", ext]]) return YES;
    }
    return NO;
}

- (NSString *) extension
{
    NSRange r = [self rangeOfString:@"." options:NSBackwardsSearch];
    if (!r.length) return nil;
    int index = r.location;
    if (index >= [self length] - 1) return nil;
    if ([self characterAtIndex:index+1] == ' ') return nil;
    return [self substringFromIndex:index+1];
}

- (NSString *) extensionRemoved
{
    NSRange r = [self rangeOfString:@"." options:NSBackwardsSearch];
    if (!r.length || r.location >= [self length] -1 || [self characterAtIndex:r.location+1] == ' ') return [NSString stringWithString:self];
    return [self substringToIndex:r.location];
}

- (int) hasIndex
{
    NSString *last = [[self componentsSeparatedByString:@" "] lastObject];
    if ([last allInts]) return [last intValue];
    return 0;
}

- (NSString *) substringToString:(NSString *)sub
{
    if (!sub || ![sub length]) return [NSString stringWithString:self];
    NSRange r = [self rangeOfString:sub];
    if (!r.length) return [NSString stringWithString:self];
    return [self substringToIndex:r.location];
}

- (NSString *) substringFromString:(NSString *)sub
{
    if (!sub || ![sub length]) return [NSString stringWithString:self];
    NSRange r = [self rangeOfString:sub];
    if (!r.length) return [NSString stringWithString:self];
    return [self substringFromIndex:r.location + [sub length]];
}

- (NSString *) extensionLessSharedPrefix:(NSArray *)choices
{
    int i, n = [choices count];
    NSRange r;
    for (i=0;i<n;i++) {
        NSString *s = [choices objectAtIndex:i];
        if ([self length] == [s length]) {
            if (NSOrderedSame == [self caseInsensitiveCompare:s]) return @"";
        } else if ([self length] < [s length]) {
            r.location = 0;
            r.length = [self length];
            if (NSOrderedSame == [s compare:self options:NSCaseInsensitiveSearch range:r]) {
                return [s substringFromIndex:[self length]];
            }
        } else {
            r.location = 0;
            r.length = [s length];
            if (NSOrderedSame == [self compare:s options:NSCaseInsensitiveSearch range:r]) {
                return @"";
            }
        }
    }
    return nil;
}

- (BOOL) hasCaseInsensitivePrefix:(NSString *)pref
{
    return [[self lowercaseString] hasPrefix:[pref lowercaseString]];
}

- (NSArray *) extensionsFromArray:(NSArray *)choices
{
    int i, n = [choices count];
    NSMutableArray *array = [NSMutableArray array];
    NSRange r = NSMakeRange(0, [self length]);
    for (i=0;i<n;i++) {
        NSString *s = [choices objectAtIndex:i];
        if ([self length] < [s length]) {
            if (NSOrderedSame == [s compare:self options:NSCaseInsensitiveSearch range:r]) {
                [array addObject:[self stringByAppendingString:[s substringFromIndex:[self length]]]];
            }
        }
    }
    if (![array count]) return nil;
    return array;
}

@end
