1 | /****************************************************************************** |
---|
2 | * $Id: StringAdditions.m 1026 2006-10-24 20:34:13Z 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 | |
---|
27 | @implementation NSString (StringAdditions) |
---|
28 | |
---|
29 | + (NSString *) ellipsis |
---|
30 | { |
---|
31 | return [NSString stringWithUTF8String: "\xE2\x80\xA6"]; |
---|
32 | } |
---|
33 | |
---|
34 | - (NSString *) stringByAppendingEllipsis |
---|
35 | { |
---|
36 | return [self stringByAppendingString: [NSString ellipsis]]; |
---|
37 | } |
---|
38 | |
---|
39 | + (NSString *) stringWithInt: (int) value |
---|
40 | { |
---|
41 | return [NSString stringWithFormat: @"%d", value]; |
---|
42 | } |
---|
43 | |
---|
44 | + (NSString *) stringForFileSize: (uint64_t) size |
---|
45 | { |
---|
46 | if (size < 1024) |
---|
47 | return [NSString stringWithFormat: NSLocalizedString(@"%lld bytes", "File size"), size]; |
---|
48 | |
---|
49 | float convertedSize = (float) size; |
---|
50 | NSString * unit; |
---|
51 | if (size < 1048576) |
---|
52 | { |
---|
53 | convertedSize /= 1024.0; |
---|
54 | unit = NSLocalizedString(@" KB", "File size (beware of leading space)"); |
---|
55 | } |
---|
56 | else if (size < 1073741824) |
---|
57 | { |
---|
58 | convertedSize /= 1048576.0; |
---|
59 | unit = NSLocalizedString(@" MB", "File size (beware of leading space)"); |
---|
60 | } |
---|
61 | else |
---|
62 | { |
---|
63 | convertedSize /= 1073741824.0; |
---|
64 | unit = NSLocalizedString(@" GB", "File size (beware of leading space)"); |
---|
65 | } |
---|
66 | |
---|
67 | NSString * sizeString; |
---|
68 | //attempt to have values with 3 digits |
---|
69 | if (convertedSize < 10.0) |
---|
70 | sizeString = [NSString stringWithFormat: @"%.2f", convertedSize]; |
---|
71 | else if (convertedSize < 100.0) |
---|
72 | sizeString = [NSString stringWithFormat: @"%.1f", convertedSize]; |
---|
73 | else |
---|
74 | sizeString = [NSString stringWithFormat: @"%.0f", convertedSize]; |
---|
75 | |
---|
76 | return [sizeString stringByAppendingString: unit]; |
---|
77 | } |
---|
78 | |
---|
79 | + (NSString *) stringForSpeed: (float) speed |
---|
80 | { |
---|
81 | return [[self stringForSpeedAbbrev: speed] stringByAppendingString: NSLocalizedString(@"B/s", "Transfer speed (Bytes per second)")]; |
---|
82 | } |
---|
83 | |
---|
84 | + (NSString *) stringForSpeedAbbrev: (float) speed |
---|
85 | { |
---|
86 | if (speed < 1000.0) //0.0 K to 999.9 K |
---|
87 | return [NSString stringWithFormat: @"%.1f K", speed]; |
---|
88 | else if (speed < 102400.0) //0.98 M to 99.99 M |
---|
89 | return [NSString stringWithFormat: @"%.2f M", speed / 1024.0]; |
---|
90 | else if (speed < 1024000.0) //100.0 M to 999.9 M |
---|
91 | return [NSString stringWithFormat: @"%.1f M", speed / 1024.0]; |
---|
92 | else //insane speeds |
---|
93 | return [NSString stringWithFormat: @"%.2f G", speed / 1048576.0]; |
---|
94 | } |
---|
95 | |
---|
96 | + (NSString *) stringForRatioWithDownload: (uint64_t) down upload: (uint64_t) up |
---|
97 | { |
---|
98 | if (down == 0) |
---|
99 | return up == 0 ? @"N/A" : [NSString stringWithUTF8String: "\xE2\x88\x9E"]; |
---|
100 | |
---|
101 | float ratio = (float) up / (float) down; |
---|
102 | if (ratio < 10.0) |
---|
103 | return [NSString stringWithFormat: @"%.2f", ratio]; |
---|
104 | else if (ratio < 100.0) |
---|
105 | return [NSString stringWithFormat: @"%.1f", ratio]; |
---|
106 | else |
---|
107 | return [NSString stringWithFormat: @"%.0f", ratio]; |
---|
108 | } |
---|
109 | |
---|
110 | - (NSAttributedString *) attributedStringFittingInWidth: (float) width |
---|
111 | attributes: (NSDictionary *) attributes |
---|
112 | { |
---|
113 | int i; |
---|
114 | float realWidth = [self sizeWithAttributes: attributes].width; |
---|
115 | |
---|
116 | /* The whole string fits */ |
---|
117 | if( realWidth <= width ) |
---|
118 | return [[[NSAttributedString alloc] initWithString: self attributes: attributes] autorelease]; |
---|
119 | |
---|
120 | float ellipsisWidth = [[NSString ellipsis] sizeWithAttributes: attributes].width; |
---|
121 | |
---|
122 | /* Width is too small */ |
---|
123 | if ( ellipsisWidth > width ) |
---|
124 | return [[[NSAttributedString alloc] initWithString: @"" attributes: attributes] autorelease]; |
---|
125 | |
---|
126 | /* Don't worry about ellipsis until the end */ |
---|
127 | width -= ellipsisWidth; |
---|
128 | |
---|
129 | /* Approximate how many characters we'll need to drop... */ |
---|
130 | i = [self length] * (width / realWidth); |
---|
131 | |
---|
132 | /* ... then refine it */ |
---|
133 | NSString * newString = [self substringToIndex: i]; |
---|
134 | realWidth = [newString sizeWithAttributes: attributes].width; |
---|
135 | |
---|
136 | if( realWidth < width ) |
---|
137 | { |
---|
138 | NSString * smallerString; |
---|
139 | do |
---|
140 | { |
---|
141 | smallerString = newString; |
---|
142 | newString = [self substringToIndex: ++i]; |
---|
143 | } while ([newString sizeWithAttributes: attributes].width <= width); |
---|
144 | |
---|
145 | newString = smallerString; |
---|
146 | } |
---|
147 | else if( realWidth > width ) |
---|
148 | { |
---|
149 | do |
---|
150 | { |
---|
151 | newString = [self substringToIndex: --i]; |
---|
152 | } while ([newString sizeWithAttributes: attributes].width > width); |
---|
153 | } |
---|
154 | else; |
---|
155 | |
---|
156 | return [[[NSAttributedString alloc] initWithString: [newString stringByAppendingEllipsis] |
---|
157 | attributes: attributes] autorelease]; |
---|
158 | } |
---|
159 | |
---|
160 | - (NSComparisonResult) compareIP: (NSString *) string |
---|
161 | { |
---|
162 | if ([self isEqualToString: string]) |
---|
163 | return NSOrderedSame; |
---|
164 | |
---|
165 | NSArray * selfSections = [self componentsSeparatedByString: @"."], |
---|
166 | * newSections = [string componentsSeparatedByString: @"."]; |
---|
167 | |
---|
168 | if ([selfSections count] != [newSections count]) |
---|
169 | return [[NSNumber numberWithUnsignedInt: [selfSections count]] compare: |
---|
170 | [NSNumber numberWithUnsignedInt: [newSections count]]]; |
---|
171 | |
---|
172 | NSEnumerator * selfSectionsEnum = [selfSections objectEnumerator], * newSectionsEnum = [newSections objectEnumerator]; |
---|
173 | NSString * selfString, * newString; |
---|
174 | NSComparisonResult result; |
---|
175 | while ((selfString = [selfSectionsEnum nextObject]) && (newString = [newSectionsEnum nextObject])) |
---|
176 | if ((result = [selfString compare: newString options: NSNumericSearch]) != NSOrderedSame) |
---|
177 | return result; |
---|
178 | |
---|
179 | return NSOrderedSame; |
---|
180 | } |
---|
181 | |
---|
182 | @end |
---|