source: trunk/macosx/NSStringAdditions.m @ 10832

Last change on this file since 10832 was 10832, checked in by livings124, 12 years ago

#3298 Move percentage string formatting into its own method, and don't append .00 to 100%

  • Property svn:keywords set to Date Rev Author Id
File size: 7.0 KB
Line 
1/******************************************************************************
2 * $Id: NSStringAdditions.m 10832 2010-06-24 00:00:43Z livings124 $
3 *
4 * Copyright (c) 2005-2010 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 "NSApplicationAdditions.h"
27
28#import <transmission.h>
29#import "utils.h"
30
31@implementation NSString (NSStringAdditions)
32
33+ (NSString *) ellipsis
34{
35        return [NSString stringWithUTF8String: "\xE2\x80\xA6"];
36}
37
38- (NSString *) stringByAppendingEllipsis
39{
40        return [self stringByAppendingString: [NSString ellipsis]];
41}
42
43+ (NSString *) stringForFileSize: (uint64_t) size
44{
45    if (size < 1024)
46    {
47        if (size != 1)
48            return [NSString stringWithFormat: @"%lld %@", size, NSLocalizedString(@"bytes", "File size - bytes")];
49        else
50            return NSLocalizedString(@"1 byte", "File size - bytes");
51    }
52
53    CGFloat convertedSize;
54    NSString * unit;
55    if (size < pow(1024, 2))
56    {
57        convertedSize = size / 1024.0;
58        unit = NSLocalizedString(@"KB", "File size - kilobytes");
59    }
60    else if (size < pow(1024, 3))
61    {
62        convertedSize = size / (CGFloat)pow(1024, 2);
63        unit = NSLocalizedString(@"MB", "File size - megabytes");
64    }
65    else if (size < pow(1024, 4))
66    {
67        convertedSize = size / (CGFloat)pow(1024, 3);
68        unit = NSLocalizedString(@"GB", "File size - gigabytes");
69    }
70    else
71    {
72        convertedSize = size / (CGFloat)pow(1024, 4);
73        unit = NSLocalizedString(@"TB", "File size - terabytes");
74    }
75   
76    //attempt to have minimum of 3 digits with at least 1 decimal
77    return convertedSize <= 9.995 ? [NSString localizedStringWithFormat: @"%.2f %@", convertedSize, unit]
78                                : [NSString localizedStringWithFormat: @"%.1f %@", convertedSize, unit];
79}
80
81+ (NSString *) stringForSpeed: (CGFloat) speed
82{
83    return [[self stringForSpeedAbbrev: speed] stringByAppendingString: NSLocalizedString(@"B/s", "Transfer speed (Bytes per second)")];
84}
85
86+ (NSString *) stringForSpeedAbbrev: (CGFloat) speed
87{
88    if (speed <= 999.95) //0.0 K to 999.9 K
89        return [NSString localizedStringWithFormat: @"%.1f K", speed];
90   
91    speed /= 1024.0;
92   
93    if (speed <= 99.995) //0.98 M to 99.99 M
94        return [NSString localizedStringWithFormat: @"%.2f M", speed];
95    else if (speed <= 999.95) //100.0 M to 999.9 M
96        return [NSString localizedStringWithFormat: @"%.1f M", speed];
97    else //insane speeds
98        return [NSString localizedStringWithFormat: @"%.2f G", (speed / 1024.0)];
99}
100
101+ (NSString *) stringForRatio: (CGFloat) ratio
102{
103    //N/A is different than libtransmission's
104    if ((int)ratio == TR_RATIO_NA)
105        return NSLocalizedString(@"N/A", "No Ratio");
106    else if ((int)ratio == TR_RATIO_INF)
107        return [NSString stringWithUTF8String: "\xE2\x88\x9E"];
108    else
109    {
110        if (ratio < 10.0)
111            return [NSString localizedStringWithFormat: @"%.2f", tr_truncd(ratio, 2)];
112        else if (ratio < 100.0)
113            return [NSString localizedStringWithFormat: @"%.1f", tr_truncd(ratio, 1)];
114        else
115            return [NSString localizedStringWithFormat: @"%.0f", tr_truncd(ratio, 0)];
116    }
117}
118
119+ (NSString *) percentString: (CGFloat) progress longDecimals: (BOOL) longDecimals
120{
121    if (progress >= 1.0)
122        return @"100%";
123    else if (longDecimals)
124        return [NSString localizedStringWithFormat: @"%.2f%%", tr_truncd(progress * 100.0, 2)];
125    else
126        return [NSString localizedStringWithFormat: @"%.1f%%", tr_truncd(progress * 100.0, 1)];
127}
128
129+ (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds
130{
131    return [NSString timeString: seconds showSeconds: showSeconds maxFields: NSUIntegerMax];
132}
133
134+ (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds maxFields: (NSUInteger) max
135{
136    NSAssert(max > 0, @"Cannot generate a time string with no fields");
137   
138    NSMutableArray * timeArray = [NSMutableArray arrayWithCapacity: MIN(max, 4)];
139    NSUInteger remaining = seconds; //causes problems for some users when it's a uint64_t
140   
141    if (seconds >= (24 * 60 * 60))
142    {
143        const NSUInteger days = remaining / (24 * 60 * 60);
144        if (days == 1)
145            [timeArray addObject: NSLocalizedString(@"1 day", "time string")];
146        else
147            [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u days", "time string"), days]];
148        remaining %= (24 * 60 * 60);
149        max--;
150    }
151    if (max > 0 && seconds >= (60 * 60))
152    {
153        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u hr", "time string"), remaining / (60 * 60)]];
154        remaining %= (60 * 60);
155        max--;
156    }
157    if (max > 0 && (!showSeconds || seconds >= 60))
158    {
159        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u min", "time string"), remaining / 60]];
160        remaining %= 60;
161        max--;
162    }
163    if (max > 0 && showSeconds)
164        [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u sec", "time string"), remaining]];
165   
166    return [timeArray componentsJoinedByString: @" "];
167}
168
169//also used in InfoWindow.xib and MessageWindow.xib
170- (NSComparisonResult) compareFinder: (NSString *) string
171{
172    if ([NSApp isOnSnowLeopardOrBetter])
173        return [self localizedStandardCompare: string];
174    else
175    {
176        const NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch | NSWidthInsensitiveSearch
177                                                            | NSForcedOrderingSearch;
178        return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]];
179    }
180}
181
182- (NSComparisonResult) compareNumeric: (NSString *) string
183{
184    const NSStringCompareOptions comparisonOptions = NSNumericSearch | NSForcedOrderingSearch;
185    return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]];
186}
187
188@end
Note: See TracBrowser for help on using the repository browser.