source: trunk/macosx/NSStringAdditions.m @ 5099

Last change on this file since 5099 was 5099, checked in by livings124, 14 years ago

dead code removal

File size: 5.8 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2005-2008 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "NSStringAdditions.h"
26#import <transmission.h>
27
28@implementation NSString (NSStringAdditions)
29
30+ (NSString *) ellipsis
31{
32        return [NSString stringWithUTF8String: "\xE2\x80\xA6"];
33}
34
35- (NSString *) stringByAppendingEllipsis
36{
37        return [self stringByAppendingString: [NSString ellipsis]];
38}
39
40+ (NSString *) stringForFileSize: (uint64_t) size
41{
42    if (size < 1024)
43        return [NSString stringWithFormat: NSLocalizedString(@"%lld bytes", "File size"), size];
44
45    float convertedSize;
46    NSString * unit;
47    if (size < 1048576)
48    {
49        convertedSize = size / 1024.0;
50        unit = NSLocalizedString(@"KB", "File size");
51    }
52    else if (size < 1073741824)
53    {
54        convertedSize = size / 1048576.0;
55        unit = NSLocalizedString(@"MB", "File size");
56    }
57    else if (size < 1099511627776.0)
58    {
59        convertedSize = size / 1073741824.0;
60        unit = NSLocalizedString(@"GB", "File size");
61    }
62    else
63    {
64        convertedSize = size / 1099511627776.0;
65        unit = NSLocalizedString(@"TB", "File size");
66    }
67   
68    //attempt to have minimum of 3 digits with at least 1 decimal
69    return [NSString stringWithFormat: convertedSize < 10.0 ? @"%.2f %@" : @"%.1f %@", convertedSize, unit];
70}
71
72+ (NSString *) stringForSpeed: (float) speed
73{
74    return [[self stringForSpeedAbbrev: speed] stringByAppendingString: NSLocalizedString(@"B/s", "Transfer speed (Bytes per second)")];
75}
76
77+ (NSString *) stringForSpeedAbbrev: (float) speed
78{
79    if (speed < 1000.0) //0.0 K to 999.9 K
80        return [NSString stringWithFormat: @"%.1f K", speed];
81    else if (speed < 102400.0) //0.98 M to 99.99 M
82        return [NSString stringWithFormat: @"%.2f M", speed / 1024.0];
83    else if (speed < 1024000.0) //100.0 M to 999.9 M
84        return [NSString stringWithFormat: @"%.1f M", speed / 1024.0];
85    else //insane speeds
86        return [NSString stringWithFormat: @"%.2f G", speed / 1048576.0];
87}
88
89+ (NSString *) stringForRatio: (float) ratio
90{
91    if (ratio == TR_RATIO_NA)
92        return NSLocalizedString(@"N/A", "No Ratio");
93    else if (ratio == TR_RATIO_INF)
94        return [NSString stringWithUTF8String: "\xE2\x88\x9E"];
95    else;
96   
97    if (ratio < 10.0)
98        return [NSString stringWithFormat: @"%.2f", ratio];
99    else if (ratio < 100.0)
100        return [NSString stringWithFormat: @"%.1f", ratio];
101    else
102        return [NSString stringWithFormat: @"%.0f", ratio];
103}
104
105+ (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds
106{
107    return [NSString timeString: seconds showSeconds:showSeconds maxDigits: UINT_MAX];
108}
109
110+ (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds maxDigits: (NSUInteger) max
111{
112    NSMutableArray * timeArray = [NSMutableArray arrayWithCapacity: MIN(max, 4)];
113    uint64_t remaining = seconds;
114   
115    if (max > 0 && seconds >= 86400) //24 * 60 * 60
116    {
117        int days = remaining / 86400;
118        if (days == 1)
119            [timeArray addObject: NSLocalizedString(@"1 day", "time string")];
120        else
121            [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%d days", "time string"), days]];
122        remaining %= 86400;
123        max--;
124    }
125    if (max > 0 && seconds >= 3600) //60 * 60
126    {
127        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%d hr", "time string"), remaining / 3600]];
128        remaining %= 3600;
129        max--;
130    }
131    if (max > 0 && (!showSeconds || seconds >= 60))
132    {
133        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%d min", "time string"), remaining / 60]];
134        remaining %= 60;
135        max--;
136    }
137    if (max > 0 && showSeconds)
138        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%d sec", "time string"), remaining]];
139   
140    return [timeArray componentsJoinedByString: @" "];
141}
142
143- (NSComparisonResult) compareIP: (NSString *) string
144{
145    NSArray * selfSections = [self componentsSeparatedByString: @"."],
146            * newSections = [string componentsSeparatedByString: @"."];
147   
148    if ([selfSections count] != [newSections count])
149        return [selfSections count] > [newSections count] ? NSOrderedDescending : NSOrderedAscending;
150
151    NSEnumerator * selfSectionsEnum = [selfSections objectEnumerator], * newSectionsEnum = [newSections objectEnumerator];
152    NSString * selfString, * newString;
153    NSComparisonResult result;
154    while ((selfString = [selfSectionsEnum nextObject]) && (newString = [newSectionsEnum nextObject]))
155        if ((result = [selfString compare: newString options: NSNumericSearch]) != NSOrderedSame)
156            return result;
157   
158    return NSOrderedSame;
159}
160
161@end
Note: See TracBrowser for help on using the repository browser.