// // 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