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