source: trunk/macosx/StringAdditions.m @ 261

Last change on this file since 261 was 261, checked in by titer, 16 years ago

Updated svn:keywords

  • Property svn:keywords set to Date Rev Author Id
File size: 4.9 KB
Line 
1/******************************************************************************
2 * $Id: StringAdditions.m 261 2006-05-29 21:27:31Z titer $
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 "Utils.h"
27
28@implementation NSString (StringAdditions)
29
30+ (NSString *) stringWithInt: (int) value
31{
32    return [NSString stringWithFormat: @"%d", value];
33}
34
35+ (NSString *) stringForFileSize: (uint64_t) size
36{
37    if (size < 1024)
38    {
39        /* 0 to 1023 bytes */
40        return [NSString stringWithFormat: @"%lld bytes", size];
41    }
42
43    NSString * unit;
44    if (size < 1048576)
45    {
46        unit = @" KB";
47    }
48    else if (size < 1073741824)
49    {
50        unit = @" MB";
51        size /= 1024;
52    }
53    else
54    {
55        unit = @" GB";
56        size /= 1048576;
57    }
58
59    NSString * value;
60    if (size < 10240)
61        /* 1.00 to 9.99 XB */
62        value = [NSString stringWithFormat: @"%lld.%02lld",
63                    size / 1024, ( size % 1024 ) * 100 / 1024];
64    else if (size < 102400)
65        /* 10.0 to 99.9 XB */
66        value = [NSString stringWithFormat: @"%lld.%lld",
67                    size / 1024, ( size % 1024 ) * 10 / 1024];
68    else
69        /* 100+ XB */
70        value = [NSString stringWithFormat: @"%lld", size / 1024];
71
72    return [value stringByAppendingString: unit];
73}
74
75+ (NSString *) stringForSpeed: (float) speed
76{
77    return [[self stringForSpeedAbbrev: speed]
78        stringByAppendingString: @"B/s"];
79}
80
81+ (NSString *) stringForSpeedAbbrev: (float) speed
82{
83    if (speed < 1000)         /* 0.0 K to 999.9 K */
84        return [NSString stringWithFormat: @"%.1f K", speed];
85    else if (speed < 102400)  /* 0.98 M to 99.99 M */
86        return [NSString stringWithFormat: @"%.2f M", speed / 1024];
87    else if (speed < 1024000) /* 100.0 M to 999.9 M */
88        return [NSString stringWithFormat: @"%.1f M", speed / 1024];
89    else                      /* Insane speeds */
90        return [NSString stringWithFormat: @"%.2f G", speed / 1048576];
91}
92
93+ (NSString *) stringForRatio: (uint64_t) down upload: (uint64_t) up
94{
95    if( !down && !up )
96        return @"N/A";
97    if( !down )
98        return [NSString stringWithUTF8String: "\xE2\x88\x9E"];
99
100    float ratio = (float) up / (float) down;
101    if( ratio < 10.0 )
102        return [NSString stringWithFormat: @"%.2f", ratio];
103    else if( ratio < 100.0 )
104        return [NSString stringWithFormat: @"%.1f", ratio];
105    else
106        return [NSString stringWithFormat: @"%.0f", ratio];
107}
108
109- (NSString *) stringFittingInWidth: (float) width
110                    withAttributes: (NSDictionary *) attributes
111{
112    int i;
113    float realWidth = [self sizeWithAttributes: attributes].width;
114   
115    /* The whole string fits */
116    if( realWidth <= width )
117        return self;
118       
119    /* Width is too small */
120    if ( [NS_ELLIPSIS sizeWithAttributes: attributes].width > width )
121        return @"";
122
123    /* Don't worry about ellipsis until the end */
124    width -= [NS_ELLIPSIS sizeWithAttributes: attributes].width;
125
126    /* Approximate how many characters we'll need to drop... */
127    i = [self length] * (width / realWidth);
128
129    /* ... then refine it */
130    NSString * newString = [self substringToIndex: i];
131    realWidth = [newString sizeWithAttributes: attributes].width;
132
133    if( realWidth < width )
134    {
135        NSString * smallerString;
136        do
137        {
138            smallerString = newString;
139            newString = [self substringToIndex: ++i];
140        } while ([newString sizeWithAttributes: attributes].width <= width);
141       
142        newString = smallerString;
143    }
144    else if( realWidth > width )
145    {
146        do
147        {
148            newString = [self substringToIndex: --i];
149        } while ([newString sizeWithAttributes: attributes].width > width);
150    }
151    else;
152
153    return [newString stringByAppendingString: NS_ELLIPSIS];
154}
155
156@end
Note: See TracBrowser for help on using the repository browser.