1 /******************************************************************************
2 * $Id: NSStringAdditions.m 11417 2010-11-14 20:26:58Z livings124 $
4 * Copyright (c) 2005-2010 Transmission authors and contributors
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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 *****************************************************************************/
25 #import "NSStringAdditions.h"
26 #import "NSApplicationAdditions.h"
28 #import <transmission.h>
31 @interface NSString (Private)
33 + (NSString *) stringForSpeed: (CGFloat) speed kb: (NSString *) kb mb: (NSString *) mb gb: (NSString *) gb;
37 @implementation NSString (NSStringAdditions)
39 + (NSString *) ellipsis
41 return [NSString stringWithUTF8String: "\xE2\x80\xA6"];
44 - (NSString *) stringByAppendingEllipsis
46 return [self stringByAppendingString: [NSString ellipsis]];
49 + (NSString *) formattedUInteger: (NSUInteger) value
51 NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
52 [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
53 [numberFormatter setMaximumFractionDigits: 0];
55 return [numberFormatter stringFromNumber: [NSNumber numberWithUnsignedInteger: value]];
58 + (NSString *) stringForFileSize: (uint64_t) size
60 const CGFloat baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0;
61 const NSUInteger baseInt = [NSApp isOnSnowLeopardOrBetter] ? 1000 : 1024;
66 return [NSString stringWithFormat: @"%lld %@", size, NSLocalizedString(@"bytes", "File size - bytes")];
68 return NSLocalizedString(@"1 byte", "File size - bytes");
71 CGFloat convertedSize;
73 if (size < pow(baseInt, 2))
75 convertedSize = size / baseFloat;
76 unit = NSLocalizedString(@"KB", "File size - kilobytes");
78 else if (size < pow(baseInt, 3))
80 convertedSize = size / powf(baseFloat, 2);
81 unit = NSLocalizedString(@"MB", "File size - megabytes");
83 else if (size < pow(baseInt, 4))
85 convertedSize = size / powf(baseFloat, 3);
86 unit = NSLocalizedString(@"GB", "File size - gigabytes");
90 convertedSize = size / powf(baseFloat, 4);
91 unit = NSLocalizedString(@"TB", "File size - terabytes");
94 //attempt to have minimum of 3 digits with at least 1 decimal
95 return convertedSize <= 9.995 ? [NSString localizedStringWithFormat: @"%.2f %@", convertedSize, unit]
96 : [NSString localizedStringWithFormat: @"%.1f %@", convertedSize, unit];
99 + (NSString *) stringForSpeed: (CGFloat) speed
101 return [self stringForSpeed: speed
102 kb: NSLocalizedString(@"KB/s", "Transfer speed (kilobytes per second)")
103 mb: NSLocalizedString(@"MB/s", "Transfer speed (megabytes per second)")
104 gb: NSLocalizedString(@"GB/s", "Transfer speed (gigabytes per second)")];
107 + (NSString *) stringForSpeedAbbrev: (CGFloat) speed
109 return [self stringForSpeed: speed kb: @"K" mb: @"M" gb: @"G"];
112 + (NSString *) stringForRatio: (CGFloat) ratio
114 //N/A is different than libtransmission's
115 if ((int)ratio == TR_RATIO_NA)
116 return NSLocalizedString(@"N/A", "No Ratio");
117 else if ((int)ratio == TR_RATIO_INF)
118 return [NSString stringWithUTF8String: "\xE2\x88\x9E"];
122 return [NSString localizedStringWithFormat: @"%.2f", tr_truncd(ratio, 2)];
123 else if (ratio < 100.0)
124 return [NSString localizedStringWithFormat: @"%.1f", tr_truncd(ratio, 1)];
126 return [NSString localizedStringWithFormat: @"%.0f", tr_truncd(ratio, 0)];
130 + (NSString *) percentString: (CGFloat) progress longDecimals: (BOOL) longDecimals
134 else if (longDecimals)
135 return [NSString localizedStringWithFormat: @"%.2f%%", tr_truncd(progress * 100.0, 2)];
137 return [NSString localizedStringWithFormat: @"%.1f%%", tr_truncd(progress * 100.0, 1)];
140 + (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds
142 return [NSString timeString: seconds showSeconds: showSeconds maxFields: NSUIntegerMax];
145 + (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds maxFields: (NSUInteger) max
147 NSAssert(max > 0, @"Cannot generate a time string with no fields");
149 NSMutableArray * timeArray = [NSMutableArray arrayWithCapacity: MIN(max, 4)];
150 NSUInteger remaining = seconds; //causes problems for some users when it's a uint64_t
152 if (seconds >= (24 * 60 * 60))
154 const NSUInteger days = remaining / (24 * 60 * 60);
156 [timeArray addObject: NSLocalizedString(@"1 day", "time string")];
158 [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u days", "time string"), days]];
159 remaining %= (24 * 60 * 60);
162 if (max > 0 && seconds >= (60 * 60))
164 [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u hr", "time string"), remaining / (60 * 60)]];
165 remaining %= (60 * 60);
168 if (max > 0 && (!showSeconds || seconds >= 60))
170 [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u min", "time string"), remaining / 60]];
174 if (max > 0 && showSeconds)
175 [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u sec", "time string"), remaining]];
177 return [timeArray componentsJoinedByString: @" "];
180 //also used in InfoWindow.xib and MessageWindow.xib
181 - (NSComparisonResult) compareFinder: (NSString *) string
183 if ([NSApp isOnSnowLeopardOrBetter])
184 return [self localizedStandardCompare: string];
187 const NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch | NSWidthInsensitiveSearch
188 | NSForcedOrderingSearch;
189 return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]];
193 - (NSComparisonResult) compareNumeric: (NSString *) string
195 const NSStringCompareOptions comparisonOptions = NSNumericSearch | NSForcedOrderingSearch;
196 return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]];
201 @implementation NSString (Private)
203 + (NSString *) stringForSpeed: (CGFloat) speed kb: (NSString *) kb mb: (NSString *) mb gb: (NSString *) gb
205 const CGFloat baseFloat = [NSApp isOnSnowLeopardOrBetter] ? 1000.0 : 1024.0;
207 if (speed <= 999.95) //0.0 KB/s to 999.9 KB/s
208 return [NSString localizedStringWithFormat: @"%.1f %@", speed, kb];
212 if (speed <= 99.995) //1.00 MB/s to 99.99 MB/s
213 return [NSString localizedStringWithFormat: @"%.2f %@", speed, mb];
214 else if (speed <= 999.95) //100.0 MB/s to 999.9 MB/s
215 return [NSString localizedStringWithFormat: @"%.1f %@", speed, mb];
217 return [NSString localizedStringWithFormat: @"%.2f %@", (speed / baseFloat), gb];