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