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