source: trunk/macosx/NSStringAdditions.m @ 3358

Last change on this file since 3358 was 3358, checked in by livings124, 15 years ago

display terabytes for file size when applicable

File size: 4.7 KB
Line 
1/******************************************************************************
2 * $Id: StringAdditions.m 2869 2007-08-19 03:03:28Z livings124 $
3 *
4 * Copyright (c) 2005-2007 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 "NSStringAdditions.h"
26#import <transmission.h>
27
28@implementation NSString (NSStringAdditions)
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 *) stringForFileSize: (uint64_t) size
41{
42    if (size < 1024)
43        return [NSString stringWithFormat: NSLocalizedString(@"%lld bytes", "File size"), size];
44
45    float convertedSize;
46    NSString * unit;
47    if (size < 1048576)
48    {
49        convertedSize = size / 1024.0;
50        unit = NSLocalizedString(@"KB", "File size");
51    }
52    else if (size < 1073741824)
53    {
54        convertedSize = size / 1048576.0;
55        unit = NSLocalizedString(@"MB", "File size");
56    }
57    else if (size < 1099511627776.0)
58    {
59        convertedSize = size / 1073741824.0;
60        unit = NSLocalizedString(@"GB", "File size");
61    }
62    else
63    {
64        convertedSize = size / 1099511627776.0;
65        unit = NSLocalizedString(@"TB", "File size");
66    }
67   
68    //attempt to have minimum of 3 digits with at least 1 decimal
69    return [NSString stringWithFormat: convertedSize < 10.0 ? @"%.2f %@" : @"%.1f %@", convertedSize, unit];
70}
71
72+ (NSString *) stringForSpeed: (float) speed
73{
74    if (speed < 0)
75        return NSLocalizedString(@"error", "Transfer speed invalid");
76   
77    return [[self stringForSpeedAbbrev: speed] stringByAppendingString:
78                    NSLocalizedString(@"B/s", "Transfer speed (Bytes per second)")];
79}
80
81+ (NSString *) stringForSpeedAbbrev: (float) speed
82{
83    if (speed < 0)
84        return NSLocalizedString(@"error", "Transfer speed invalid");
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 *) stringForRatio: (float) ratio
97{
98    if (ratio == TR_RATIO_NA)
99        return NSLocalizedString(@"N/A", "No Ratio");
100    else if (ratio < 0)
101        return NSLocalizedString(@"error", "Ratio invalid");
102    else;
103   
104    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
Note: See TracBrowser for help on using the repository browser.