source: branches/new_api/macosx/StringAdditions.m @ 156

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

Added [NSString stringWithInt:] for convenience.
UI cosmetic: bigger margin left from the pause/resume button

File size: 4.5 KB
Line 
1/******************************************************************************
2 * Copyright (c) 2005-2006 Transmission authors and contributors
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *****************************************************************************/
22
23#import "StringAdditions.h"
24#import "Utils.h"
25
26@implementation NSString (StringAdditions)
27
28+ (NSString *) stringWithInt: (int) value
29{
30    return [NSString stringWithFormat: @"%d", value];
31}
32
33+ (NSString *) stringForFileSize: (uint64_t) size
34{
35    if (size < 1024)
36        return [NSString stringWithFormat: @"%lld bytes", size];
37    else if (size < 1048576)
38        return [NSString stringWithFormat: @"%lld.%lld KB",
39                size / 1024, ( size % 1024 ) / 103];
40    else if (size < 1073741824)
41        return [NSString stringWithFormat: @"%lld.%lld MB",
42                size / 1048576, ( size % 1048576 ) / 104858];
43    else
44        return [NSString stringWithFormat: @"%lld.%lld GB",
45                size / 1073741824, ( size % 1073741824 ) / 107374183];
46}
47
48+ (NSString *) stringForSpeed: (float) speed
49{
50    return [[self stringForSpeedAbbrev: speed]
51        stringByAppendingString: @"B/s"];
52}
53
54+ (NSString *) stringForSpeedAbbrev: (float) speed
55{
56    if (speed < 1000)         /* 0.0 K to 999.9 K */
57        return [NSString stringWithFormat: @"%.1f K", speed];
58    else if (speed < 102400)  /* 0.98 M to 99.99 M */
59        return [NSString stringWithFormat: @"%.2f M", speed / 1024];
60    else if (speed < 1024000) /* 100.0 M to 999.9 M */
61        return [NSString stringWithFormat: @"%.1f M", speed / 1024];
62    else                      /* Insane speeds */
63        return [NSString stringWithFormat: @"%.2f G", speed / 1048576];
64}
65
66+ (NSString *) stringForRatio: (uint64_t) down upload: (uint64_t) up
67{
68    if( !down && !up )
69        return @"N/A";
70    if( !down )
71        return [NSString stringWithUTF8String: "\xE2\x88\x9E"];
72
73    float ratio = (float) up / (float) down;
74    if( ratio < 10.0 )
75        return [NSString stringWithFormat: @"%.2f", ratio];
76    else if( ratio < 100.0 )
77        return [NSString stringWithFormat: @"%.1f", ratio];
78    else
79        return [NSString stringWithFormat: @"%.0f", ratio];
80}
81
82- (NSString *) stringFittingInWidth: (float) width
83                    withAttributes: (NSDictionary *) attributes
84{
85    int i;
86    float realWidth = [self sizeWithAttributes: attributes].width;
87   
88    /* The whole string fits */
89    if( realWidth <= width )
90        return self;
91       
92    /* Width is too small */
93    if ( [NS_ELLIPSIS sizeWithAttributes: attributes].width > width )
94        return @"";
95
96    /* Don't worry about ellipsis until the end */
97    width -= [NS_ELLIPSIS sizeWithAttributes: attributes].width;
98
99    /* Approximate how many characters we'll need to drop... */
100    i = [self length] * (width / realWidth);
101
102    /* ... then refine it */
103    NSString * newString = [self substringToIndex: i];
104    realWidth = [newString sizeWithAttributes: attributes].width;
105
106    if( realWidth < width )
107    {
108        NSString * smallerString;
109        do
110        {
111            smallerString = newString;
112            newString = [self substringToIndex: ++i];
113        } while ([newString sizeWithAttributes: attributes].width <= width);
114       
115        newString = smallerString;
116    }
117    else if( realWidth > width )
118    {
119        do
120        {
121            newString = [self substringToIndex: --i];
122        } while ([newString sizeWithAttributes: attributes].width > width);
123    }
124    else;
125
126    return [newString stringByAppendingString: NS_ELLIPSIS];
127}
128
129@end
Note: See TracBrowser for help on using the repository browser.