// // NSString+Additions.m // Perforce // // Created by Adam Czubernat on 16.05.2013. // Copyright (c) 2013 Perforce Software, Inc. All rights reserved. // #import "NSString+Additions.h" @implementation NSString (Additions) + (NSString *)stringWithTimeInterval:(NSTimeInterval)timeInterval { if (timeInterval == INFINITY) return @"∞"; NSInteger time = (NSInteger)timeInterval; return [NSString stringWithFormat:@"%@%@%@", time < 3600 ? @"" : [NSString stringWithFormat:@"%02ld:", time/3600], time < 60 ? @"" : [NSString stringWithFormat:@"%02ld:", (time/60)%60], [NSString stringWithFormat:(time < 60 ? @"%lds" : @"%02ld"), time%60]]; } + (NSString *)stringWithByteCount:(NSInteger)bytes { NSString *units; int decimals = 0; CGFloat size = bytes; if (size < 1024.0f) units = @"bytes"; else if ((size /= 1024.0f) < 1024.0f) units = @"KB"; else if ((size /= 1024.0f) < 1024.0f) units = @"MB", decimals = 1; else size /= 1024.0f, units = @"GB", decimals = 2; return [NSString stringWithFormat:@"%.*f %@", decimals, size, units]; } - (NSString *)stringByRemovingPrefix:(NSString *)prefix { return ([self hasPrefix:prefix] ? [self substringFromIndex:prefix.length] : self); } - (NSString *)stringByRemovingSuffix:(NSString *)suffix { return ([self hasSuffix:suffix] ? [self substringToIndex:self.length - suffix.length] : self); } - (NSArray *)arrayOfArguments { CFStringRef string = (__bridge CFStringRef)self; CFIndex length = CFStringGetLength(string); NSMutableArray *array = [NSMutableArray arrayWithCapacity:length]; unichar quoteChar; CFIndex bufferLength = 0; unichar *buffer = malloc(sizeof(unichar) * length); BOOL inQuote = NO; for (CFIndex i=0; i length) { return true; } NSRange subrange = NSMakeRange(range.location, 3); NSString *code = [self substringWithRange:subrange]; if(!( [code isEqualToString:@"%25"] || [code isEqualToString:@"%23"] || [code isEqualToString:@"%2A"] || [code isEqualToString:@"%40"] )) { return true; } range = NSMakeRange(range.location + range.length, length - (range.location + range.length)); } } } return false; } - (BOOL)hasEncodedCharacters { if(([self rangeOfString:@"%25"].location != NSNotFound) || ([self rangeOfString:@"%23"].location != NSNotFound) || ([self rangeOfString:@"%2A"].location != NSNotFound) || ([self rangeOfString:@"%2a"].location != NSNotFound) || ([self rangeOfString:@"%40"].location != NSNotFound)) { return true; } else { return false; } } - (NSString *)encodePath { // force decoding to prevent double encoding NSString *newString = [self decodePath]; newString = [newString stringByReplacingOccurrencesOfString:@"%" withString:@"%25"]; newString = [newString stringByReplacingOccurrencesOfString:@"#" withString:@"%23"]; newString = [newString stringByReplacingOccurrencesOfString:@"*" withString:@"%2A"]; newString = [newString stringByReplacingOccurrencesOfString:@"@" withString:@"%40"]; return newString; } - (NSString *)encodeLucene { NSString *newString; newString = [self stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"]; newString = [newString stringByReplacingOccurrencesOfString:@"+" withString:@"\\+"]; newString = [newString stringByReplacingOccurrencesOfString:@"-" withString:@"\\-"]; newString = [newString stringByReplacingOccurrencesOfString:@"&&" withString:@"\\&&"]; newString = [newString stringByReplacingOccurrencesOfString:@"||" withString:@"\\||"]; newString = [newString stringByReplacingOccurrencesOfString:@"!" withString:@"\\!"]; newString = [newString stringByReplacingOccurrencesOfString:@"(" withString:@"\\("]; newString = [newString stringByReplacingOccurrencesOfString:@")" withString:@"\\)"]; newString = [newString stringByReplacingOccurrencesOfString:@"{" withString:@"\\{"]; newString = [newString stringByReplacingOccurrencesOfString:@"}" withString:@"\\}"]; newString = [newString stringByReplacingOccurrencesOfString:@"[" withString:@"\\["]; newString = [newString stringByReplacingOccurrencesOfString:@"]" withString:@"\\]"]; newString = [newString stringByReplacingOccurrencesOfString:@"^" withString:@"\\^"]; newString = [newString stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]; newString = [newString stringByReplacingOccurrencesOfString:@"~" withString:@"\\~"]; newString = [newString stringByReplacingOccurrencesOfString:@"*" withString:@"\\*"]; newString = [newString stringByReplacingOccurrencesOfString:@"?" withString:@"\\?"]; newString = [newString stringByReplacingOccurrencesOfString:@":" withString:@"\\:"]; return newString; } - (NSString *)decodePath { NSString *newString; newString = [self stringByReplacingOccurrencesOfString:@"%23" withString:@"#"]; newString = [newString stringByReplacingOccurrencesOfString:@"%2A" withString:@"*"]; newString = [newString stringByReplacingOccurrencesOfString:@"%2a" withString:@"*"]; newString = [newString stringByReplacingOccurrencesOfString:@"%40" withString:@"@"]; newString = [newString stringByReplacingOccurrencesOfString:@"%25" withString:@"%"]; return newString; } #pragma mark - - (BOOL)isEqualCaseInsensitive:(NSString *)string { return string && [self caseInsensitiveCompare:string] == NSOrderedSame; } - (BOOL)isEmptyString { NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet]; return [self stringByTrimmingCharactersInSet:set].length == 0; } - (BOOL)hasPrefixCaseInsensitive:(NSString *)prefix { return prefix && [self rangeOfString:prefix options:NSAnchoredSearch | NSCaseInsensitiveSearch ].location != NSNotFound; } - (BOOL)hasSuffixCaseInsensitive:(NSString *)suffix { return suffix && [self rangeOfString:suffix options:NSAnchoredSearch | NSCaseInsensitiveSearch | NSBackwardsSearch ].location != NSNotFound; } @end