source: trunk/macosx/StringAdditions.m @ 118

Last change on this file since 118 was 118, checked in by titer, 16 years ago

Cleaner stringFittingInWidth

File size: 3.3 KB
Line 
1//
2//  StringAdditions.m
3//  Transmission
4//
5//  Created by Mitchell Livingston on 1/16/06.
6//  Copyright 2006 __MyCompanyName__. All rights reserved.
7//
8
9#import "StringAdditions.h"
10#import "Utils.h"
11
12@implementation NSString (StringAdditions)
13
14+ (NSString *) stringForFileSize: (uint64_t) size
15{
16    if (size < 1024)
17        return [NSString stringWithFormat: @"%lld bytes", size];
18    else if (size < 1048576)
19        return [NSString stringWithFormat: @"%lld.%lld KB",
20                size / 1024, ( size % 1024 ) / 103];
21    else if (size < 1073741824)
22        return [NSString stringWithFormat: @"%lld.%lld MB",
23                size / 1048576, ( size % 1048576 ) / 104858];
24    else
25        return [NSString stringWithFormat: @"%lld.%lld GB",
26                size / 1073741824, ( size % 1073741824 ) / 107374183];
27}
28
29+ (NSString *) stringForSpeed: (float) speed
30{
31    return [[self stringForSpeedAbbrev: speed]
32        stringByAppendingString: @"B/s"];
33}
34
35+ (NSString *) stringForSpeedAbbrev: (float) speed
36{
37    if (speed < 1000)         /* 0.0 K to 999.9 K */
38        return [NSString stringWithFormat: @"%.1f K", speed];
39    else if (speed < 102400)  /* 0.98 M to 99.99 M */
40        return [NSString stringWithFormat: @"%.2f M", speed / 1024];
41    else if (speed < 1024000) /* 100.0 M to 999.9 M */
42        return [NSString stringWithFormat: @"%.1f M", speed / 1024];
43    else                      /* Insane speeds */
44        return [NSString stringWithFormat: @"%.2f G", speed / 1048576];
45}
46
47+ (NSString *) stringForRatio: (uint64_t) down upload: (uint64_t) up
48{
49    if( !down && !up )
50        return @"N/A";
51    if( !down )
52        return [NSString stringWithUTF8String: "\xE2\x88\x9E"];
53
54    float ratio = (float) up / (float) down;
55    if( ratio < 10.0 )
56        return [NSString stringWithFormat: @"%.2f", ratio];
57    else if( ratio < 100.0 )
58        return [NSString stringWithFormat: @"%.1f", ratio];
59    else
60        return [NSString stringWithFormat: @"%.0f", ratio];
61}
62
63- (NSString *) stringFittingInWidth: (float) width
64                    withAttributes: (NSDictionary *) attributes
65{
66    int i;
67    float realWidth = [self sizeWithAttributes: attributes].width;
68   
69    /* The whole string fits */
70    if( realWidth <= width )
71        return self;
72       
73    /* Width is too small */
74    if ( [NS_ELLIPSIS sizeWithAttributes: attributes].width > width )
75        return @"";
76
77    /* Don't worry about ellipsis until the end */
78    width -= [NS_ELLIPSIS sizeWithAttributes: attributes].width;
79
80    /* Approximate how many characters we'll need to drop... */
81    i = [self length] * (width / realWidth);
82
83    /* ... then refine it */
84    NSString * newString = [self substringToIndex: i];
85    realWidth = [newString sizeWithAttributes: attributes].width;
86
87    if( realWidth < width )
88    {
89        NSString * smallerString;
90        do
91        {
92            smallerString = newString;
93            newString = [self substringToIndex: ++i];
94        } while ([newString sizeWithAttributes: attributes].width <= width);
95       
96        newString = smallerString;
97    }
98    else if( realWidth > width )
99    {
100        do
101        {
102            newString = [self substringToIndex: --i];
103        } while ([newString sizeWithAttributes: attributes].width > width);
104    }
105    else;
106
107    return [newString stringByAppendingString: NS_ELLIPSIS];
108}
109
110@end
Note: See TracBrowser for help on using the repository browser.