| 1 | /* |
|---|
| 2 | * NSString-Extensions.m |
|---|
| 3 | * Sapphire |
|---|
| 4 | * |
|---|
| 5 | * Created by Graham Booker on Jun. 30, 2007. |
|---|
| 6 | * Copyright 2007 Sapphire Development Team and/or www.nanopi.net |
|---|
| 7 | * All rights reserved. |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or modify it under the terms of the GNU |
|---|
| 10 | * General Public License as published by the Free Software Foundation; either version 3 of the License, |
|---|
| 11 | * or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
|---|
| 14 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|---|
| 15 | * Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License along with this program; if not, |
|---|
| 18 | * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #import "NSString-Extensions.h" |
|---|
| 22 | |
|---|
| 23 | @implementation NSString (PostStrings) |
|---|
| 24 | - (NSString *)URLEncode |
|---|
| 25 | { |
|---|
| 26 | /*Create a new one using the CFURL function*/ |
|---|
| 27 | NSString *encoded = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, CFSTR(" "), CFSTR("?=&+"), kCFStringEncodingUTF8); |
|---|
| 28 | /*Return it*/ |
|---|
| 29 | NSString *result = [encoded stringByReplacingAllOccurancesOf:@" " withString:@"+"]; |
|---|
| 30 | [encoded release]; |
|---|
| 31 | return result; |
|---|
| 32 | } |
|---|
| 33 | @end |
|---|
| 34 | |
|---|
| 35 | @implementation NSString (Replacements) |
|---|
| 36 | - (NSString *)stringByReplacingAllOccurancesOf:(NSString *)search withString:(NSString *)replacement |
|---|
| 37 | { |
|---|
| 38 | NSMutableString *mut = [[self mutableCopy] autorelease]; |
|---|
| 39 | [mut replaceAllOccurancesOf:search withString:replacement]; |
|---|
| 40 | return [NSString stringWithString:mut]; |
|---|
| 41 | } |
|---|
| 42 | @end |
|---|
| 43 | |
|---|
| 44 | @implementation NSString (Additions) |
|---|
| 45 | + (NSString *)stringByCroppingDirectoryPath:(NSString *)directoryPath toLength:(int)requestedLength |
|---|
| 46 | { |
|---|
| 47 | int dirLength=[[directoryPath pathComponents] count]; |
|---|
| 48 | NSString *returnPath=directoryPath ; |
|---|
| 49 | if(dirLength>requestedLength) |
|---|
| 50 | { |
|---|
| 51 | NSRange croppedRange; |
|---|
| 52 | croppedRange.location=dirLength-requestedLength; |
|---|
| 53 | croppedRange.length=requestedLength; |
|---|
| 54 | returnPath=[NSString stringWithFormat:@" .../%@",[NSString pathWithComponents:[[directoryPath pathComponents] subarrayWithRange:croppedRange]]]; |
|---|
| 55 | } |
|---|
| 56 | return returnPath; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | - (NSComparisonResult)nameCompare:(NSString *)other |
|---|
| 60 | { |
|---|
| 61 | NSString *myShortenedName=nil ; |
|---|
| 62 | NSString *otherShortenedName=nil ; |
|---|
| 63 | /* Make sure we get titles leading with "A" & "The" where the belong */ |
|---|
| 64 | if([[self lowercaseString] hasPrefix:@"a "] && [self length]>2) |
|---|
| 65 | myShortenedName=[self substringFromIndex:2]; |
|---|
| 66 | else if([[self lowercaseString] hasPrefix:@"the "] && [self length]>4) |
|---|
| 67 | myShortenedName=[self substringFromIndex:4]; |
|---|
| 68 | if([[other lowercaseString] hasPrefix:@"a "]&& [other length]>2) |
|---|
| 69 | otherShortenedName=[other substringFromIndex:2]; |
|---|
| 70 | else if([[other lowercaseString] hasPrefix:@"the "] && [other length]>4) |
|---|
| 71 | otherShortenedName=[other substringFromIndex:4]; |
|---|
| 72 | |
|---|
| 73 | if(myShortenedName==nil) |
|---|
| 74 | myShortenedName=self; |
|---|
| 75 | if(otherShortenedName==nil) |
|---|
| 76 | otherShortenedName=other; |
|---|
| 77 | |
|---|
| 78 | return [myShortenedName compare:otherShortenedName options:NSCaseInsensitiveSearch | NSNumericSearch]; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | @end |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | @implementation NSMutableString (Replacements) |
|---|
| 85 | - (void)replaceAllOccurancesOf:(NSString *)search withString:(NSString *)replacement |
|---|
| 86 | { |
|---|
| 87 | [self replaceOccurrencesOfString:search withString:replacement options:0 range:NSMakeRange(0, [self length])]; |
|---|
| 88 | } |
|---|
| 89 | @end |
|---|