// // StringAdditions.m // Transmission // // Created by Mitchell Livingston on 1/16/06. // Copyright 2006 __MyCompanyName__. All rights reserved. // #import "StringAdditions.h" #import "Utils.h" @implementation NSString (StringAdditions) + (NSString *) stringForFileSize: (uint64_t) size { if (size < 1024) return [NSString stringWithFormat: @"%lld bytes", size]; else if (size < 1048576) return [NSString stringWithFormat: @"%lld.%lld KB", size / 1024, ( size % 1024 ) / 103]; else if (size < 1073741824) return [NSString stringWithFormat: @"%lld.%lld MB", size / 1048576, ( size % 1048576 ) / 104858]; else return [NSString stringWithFormat: @"%lld.%lld GB", size / 1073741824, ( size % 1073741824 ) / 107374183]; } + (NSString *) stringForSpeed: (float) speed { if (speed < 1024) return [NSString stringWithFormat: @"%.1f KB/s", speed]; else if (speed < 1048576) return [NSString stringWithFormat: @"%.2f MB/s", speed / 1024]; else return [NSString stringWithFormat: @"%.2f GB/s", speed / 1048576]; } - (NSString *) stringFittingInWidth: (float) width withAttributes: (NSDictionary *) attributes { NSString * newString = self; unsigned i; for (i = [self length]; [newString sizeWithAttributes: attributes].width > width; i--) newString = [[self substringToIndex: i] stringByAppendingString: NS_ELLIPSIS]; return newString; } @end