source: trunk/macosx/StringAdditions.m @ 777

Last change on this file since 777 was 777, checked in by livings124, 16 years ago

Sort IP addresses correctly.

  • Property svn:keywords set to Date Rev Author Id
File size: 6.1 KB
Line 
1/******************************************************************************
2 * $Id: StringAdditions.m 777 2006-08-14 23:39:51Z 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: @"%lld bytes", size];
48
49    float convertedSize = (float) size;
50    NSString * unit;
51    if (size < 1048576)
52    {
53        convertedSize /= 1024.0;
54        unit = @" KB";
55    }
56    else if (size < 1073741824)
57    {
58        convertedSize /= 1048576.0;
59        unit = @" MB";
60    }
61    else
62    {
63        convertedSize /= 1073741824.0;
64        unit = @" GB";
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: @"B/s"];
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    NSEnumerator * selfSections = [[self componentsSeparatedByString: @"."] objectEnumerator],
166            * newSections = [[string componentsSeparatedByString: @"."] objectEnumerator];
167   
168    NSComparisonResult result;
169    NSString * selfString = [selfSections nextObject], * newString = [newSections nextObject];
170    while (selfString && newString)
171    {
172        if ((result = [selfString compare: newString options: NSNumericSearch]) != NSOrderedSame)
173            return result;
174       
175        selfString = [selfSections nextObject];
176        newString = [newSections nextObject];
177    }
178   
179    if (selfString)
180        return NSOrderedDescending;
181    else if (newString)
182        return NSOrderedAscending;
183    else
184        return NSOrderedSame;
185}
186
187@end
Note: See TracBrowser for help on using the repository browser.