1 | /****************************************************************************** |
---|
2 | * $Id: StringAdditions.m 2869 2007-08-19 03:03:28Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2007 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 | [self stringForLargeFileSizeGigs: size / 1073741824 bytes: size % 1073741824]; |
---|
43 | } |
---|
44 | |
---|
45 | + (NSString *) stringForLargeFileSizeGigs: (uint64_t) gigs bytes: (uint64_t) bytes |
---|
46 | { |
---|
47 | float convertedSize; |
---|
48 | NSString * unit; |
---|
49 | if (gigs == 0) |
---|
50 | { |
---|
51 | if (bytes < 1024) |
---|
52 | return [NSString stringWithFormat: NSLocalizedString(@"%lld bytes", "File size"), bytes]; |
---|
53 | else if (bytes < 1048576) |
---|
54 | { |
---|
55 | convertedSize = bytes / 1024.0; |
---|
56 | unit = NSLocalizedString(@"KB", "File size"); |
---|
57 | } |
---|
58 | else |
---|
59 | { |
---|
60 | convertedSize = bytes / 1048576.0; |
---|
61 | unit = NSLocalizedString(@"MB", "File size"); |
---|
62 | } |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | if (gigs < 1024) |
---|
67 | { |
---|
68 | convertedSize = (float)gigs + bytes / 1073741824.0; |
---|
69 | unit = NSLocalizedString(@"GB", "File size"); |
---|
70 | } |
---|
71 | else |
---|
72 | { |
---|
73 | convertedSize = gigs / 1024.0; |
---|
74 | unit = NSLocalizedString(@"TB", "File size"); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | //attempt to have minimum of 3 digits with at least 1 decimal |
---|
79 | return [NSString stringWithFormat: convertedSize < 10.0 ? @"%.2f %@" : @"%.1f %@", convertedSize, unit]; |
---|
80 | } |
---|
81 | |
---|
82 | + (NSString *) stringForSpeed: (float) speed |
---|
83 | { |
---|
84 | if (speed < 0) |
---|
85 | return NSLocalizedString(@"error", "Transfer speed invalid"); |
---|
86 | |
---|
87 | return [[self stringForSpeedAbbrev: speed] stringByAppendingString: |
---|
88 | NSLocalizedString(@"B/s", "Transfer speed (Bytes per second)")]; |
---|
89 | } |
---|
90 | |
---|
91 | + (NSString *) stringForSpeedAbbrev: (float) speed |
---|
92 | { |
---|
93 | if (speed < 0) |
---|
94 | return NSLocalizedString(@"error", "Transfer speed invalid"); |
---|
95 | |
---|
96 | if (speed < 1000.0) //0.0 K to 999.9 K |
---|
97 | return [NSString stringWithFormat: @"%.1f K", speed]; |
---|
98 | else if (speed < 102400.0) //0.98 M to 99.99 M |
---|
99 | return [NSString stringWithFormat: @"%.2f M", speed / 1024.0]; |
---|
100 | else if (speed < 1024000.0) //100.0 M to 999.9 M |
---|
101 | return [NSString stringWithFormat: @"%.1f M", speed / 1024.0]; |
---|
102 | else //insane speeds |
---|
103 | return [NSString stringWithFormat: @"%.2f G", speed / 1048576.0]; |
---|
104 | } |
---|
105 | |
---|
106 | + (NSString *) stringForRatio: (float) ratio |
---|
107 | { |
---|
108 | if (ratio == TR_RATIO_NA) |
---|
109 | return NSLocalizedString(@"N/A", "No Ratio"); |
---|
110 | else if (ratio < 0) |
---|
111 | return NSLocalizedString(@"error", "Ratio invalid"); |
---|
112 | else; |
---|
113 | |
---|
114 | if (ratio < 10.0) |
---|
115 | return [NSString stringWithFormat: @"%.2f", ratio]; |
---|
116 | else if (ratio < 100.0) |
---|
117 | return [NSString stringWithFormat: @"%.1f", ratio]; |
---|
118 | else |
---|
119 | return [NSString stringWithFormat: @"%.0f", ratio]; |
---|
120 | } |
---|
121 | |
---|
122 | - (NSComparisonResult) compareIP: (NSString *) string |
---|
123 | { |
---|
124 | NSArray * selfSections = [self componentsSeparatedByString: @"."], |
---|
125 | * newSections = [string componentsSeparatedByString: @"."]; |
---|
126 | |
---|
127 | if ([selfSections count] != [newSections count]) |
---|
128 | return [selfSections count] > [newSections count] ? NSOrderedDescending : NSOrderedAscending; |
---|
129 | |
---|
130 | NSEnumerator * selfSectionsEnum = [selfSections objectEnumerator], * newSectionsEnum = [newSections objectEnumerator]; |
---|
131 | NSString * selfString, * newString; |
---|
132 | NSComparisonResult result; |
---|
133 | while ((selfString = [selfSectionsEnum nextObject]) && (newString = [newSectionsEnum nextObject])) |
---|
134 | if ((result = [selfString compare: newString options: NSNumericSearch]) != NSOrderedSame) |
---|
135 | return result; |
---|
136 | |
---|
137 | return NSOrderedSame; |
---|
138 | } |
---|
139 | |
---|
140 | @end |
---|