source: trunk/macosx/NSStringAdditions.m @ 6252

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

store progress gradients in the gradient class; miscellaneous cleanup

  • Property svn:keywords set to Date Rev Author Id
File size: 6.1 KB
Line 
1/******************************************************************************
2 * $Id: NSStringAdditions.m 6252 2008-06-25 05:01:04Z livings124 $
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: @"%lld %@", size, NSLocalizedString(@"bytes", "File size - bytes")];
44
45    float convertedSize;
46    NSString * unit;
47    if (size < 1048576)
48    {
49        convertedSize = size / 1024.0;
50        unit = NSLocalizedString(@"KB", "File size - kilobytes");
51    }
52    else if (size < 1073741824)
53    {
54        convertedSize = size / 1048576.0;
55        unit = NSLocalizedString(@"MB", "File size - megabytes");
56    }
57    else if (size < 1099511627776.0)
58    {
59        convertedSize = size / 1073741824.0;
60        unit = NSLocalizedString(@"GB", "File size - gigabytes");
61    }
62    else
63    {
64        convertedSize = size / 1099511627776.0;
65        unit = NSLocalizedString(@"TB", "File size - terabytes");
66    }
67   
68    //attempt to have minimum of 3 digits with at least 1 decimal
69    return convertedSize <= 9.995 ? [NSString localizedStringWithFormat: @"%.2f %@", convertedSize, unit]
70                                : [NSString localizedStringWithFormat: @"%.1f %@", convertedSize, unit];
71}
72
73+ (NSString *) stringForSpeed: (float) speed
74{
75    return [[self stringForSpeedAbbrev: speed] stringByAppendingString: NSLocalizedString(@"B/s", "Transfer speed (Bytes per second)")];
76}
77
78+ (NSString *) stringForSpeedAbbrev: (float) speed
79{
80    if (speed <= 999.95) //0.0 K to 999.9 K
81        return [NSString localizedStringWithFormat: @"%.1f K", speed];
82   
83    speed /= 1024.0;
84   
85    if (speed <= 99.995) //0.98 M to 99.99 M
86        return [NSString localizedStringWithFormat: @"%.2f M", speed];
87    else if (speed <= 999.95) //100.0 M to 999.9 M
88        return [NSString localizedStringWithFormat: @"%.1f M", speed];
89    else //insane speeds
90        return [NSString localizedStringWithFormat: @"%.2f G", (speed / 1024.0)];
91}
92
93+ (NSString *) stringForRatio: (float) ratio
94{
95    if (ratio == TR_RATIO_NA)
96        return NSLocalizedString(@"N/A", "No Ratio");
97    else if (ratio == TR_RATIO_INF)
98        return [NSString stringWithUTF8String: "\xE2\x88\x9E"];
99    else;
100   
101    if (ratio <= 9.995) //0.00 to 9.99
102        return [NSString localizedStringWithFormat: @"%.2f", ratio];
103    else if (ratio <= 99.95) //10.0 to 99.9
104        return [NSString localizedStringWithFormat: @"%.1f", ratio];
105    else //rest are single digit
106        return [NSString localizedStringWithFormat: @"%.0f", ratio];
107}
108
109+ (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds
110{
111    return [NSString timeString: seconds showSeconds: showSeconds maxFields: UINT_MAX];
112}
113
114+ (NSString *) timeString: (NSUInteger) seconds showSeconds: (BOOL) showSeconds maxFields: (NSUInteger) max
115{
116    NSMutableArray * timeArray = [NSMutableArray arrayWithCapacity: MIN(max, 4)];
117    NSUInteger remaining = seconds;
118   
119    if (max > 0 && seconds >= 86400) //24 * 60 * 60
120    {
121        int days = remaining / 86400;
122        if (days == 1)
123            [timeArray addObject: NSLocalizedString(@"1 day", "time string")];
124        else
125            [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u days", "time string"), days]];
126        remaining %= 86400;
127        max--;
128    }
129    if (max > 0 && seconds >= 3600) //60 * 60
130    {
131        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u hr", "time string"), remaining / 3600]];
132        remaining %= 3600;
133        max--;
134    }
135    if (max > 0 && (!showSeconds || seconds >= 60))
136    {
137        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u min", "time string"), remaining / 60]];
138        remaining %= 60;
139        max--;
140    }
141    if (max > 0 && showSeconds)
142        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u sec", "time string"), remaining]];
143   
144    return [timeArray componentsJoinedByString: @" "];
145}
146
147- (NSComparisonResult) compareIP: (NSString *) string
148{
149    NSArray * selfSections = [self componentsSeparatedByString: @"."],
150            * newSections = [string componentsSeparatedByString: @"."];
151   
152    if ([selfSections count] != [newSections count])
153        return [selfSections count] > [newSections count] ? NSOrderedDescending : NSOrderedAscending;
154
155    NSEnumerator * selfSectionsEnum = [selfSections objectEnumerator], * newSectionsEnum = [newSections objectEnumerator];
156    NSString * selfString, * newString;
157    NSComparisonResult result;
158    while ((selfString = [selfSectionsEnum nextObject]) && (newString = [newSectionsEnum nextObject]))
159        if ((result = [selfString compare: newString options: NSNumericSearch]) != NSOrderedSame)
160            return result;
161   
162    return NSOrderedSame;
163}
164
165@end
Note: See TracBrowser for help on using the repository browser.