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