mirror of
https://github.com/transmission/transmission
synced 2024-12-24 08:43:27 +00:00
390b3e0466
Category for NSString instead of using Utils Speed string in NSString additions
51 lines
1.5 KiB
Objective-C
51 lines
1.5 KiB
Objective-C
//
|
|
// 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
|