source: trunk/macosx/StringAdditions.m @ 1569

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

return "error" when requesting a speed or ratio string for speed/ratio less then 0

  • Property svn:keywords set to Date Rev Author Id
File size: 5.0 KB
Line 
1/******************************************************************************
2 * $Id: StringAdditions.m 1569 2007-03-15 12:46: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#import <transmission.h>
27
28@implementation NSString (StringAdditions)
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 *) stringWithInt: (int) value
41{
42    return [NSString stringWithFormat: @"%d", value];
43}
44
45+ (NSString *) stringForFileSize: (uint64_t) size
46{
47    if (size < 1024)
48        return [NSString stringWithFormat: NSLocalizedString(@"%lld bytes", "File size"), size];
49
50    float convertedSize = (float) size;
51    NSString * unit;
52    if (size < 1048576)
53    {
54        convertedSize /= 1024.0;
55        unit = NSLocalizedString(@" KB", "File size (beware of leading space)");
56    }
57    else if (size < 1073741824)
58    {
59        convertedSize /= 1048576.0;
60        unit = NSLocalizedString(@" MB", "File size (beware of leading space)");
61    }
62    else
63    {
64        convertedSize /= 1073741824.0;
65        unit = NSLocalizedString(@" GB", "File size (beware of leading space)");
66    }
67
68    NSString * sizeString;
69    //attempt to have values with 3 digits
70    if (convertedSize < 10.0)
71        sizeString = [NSString stringWithFormat: @"%.2f", convertedSize];
72    else if (convertedSize < 100.0)
73        sizeString = [NSString stringWithFormat: @"%.1f", convertedSize];
74    else
75        sizeString = [NSString stringWithFormat: @"%.0f", convertedSize];
76
77    return [sizeString stringByAppendingString: unit];
78}
79
80+ (NSString *) stringForSpeed: (float) speed
81{
82    if (speed < 0)
83        return NSLocalizedString(@"error", "Transfer speed invalid");
84   
85    return [[self stringForSpeedAbbrev: speed] stringByAppendingString:
86                    NSLocalizedString(@"B/s", "Transfer speed (Bytes per second)")];
87}
88
89+ (NSString *) stringForSpeedAbbrev: (float) speed
90{
91    if (speed < 0)
92        return NSLocalizedString(@"error", "Transfer speed invalid");
93   
94    if (speed < 1000.0) //0.0 K to 999.9 K
95        return [NSString stringWithFormat: @"%.1f K", speed];
96    else if (speed < 102400.0) //0.98 M to 99.99 M
97        return [NSString stringWithFormat: @"%.2f M", speed / 1024.0];
98    else if (speed < 1024000.0) //100.0 M to 999.9 M
99        return [NSString stringWithFormat: @"%.1f M", speed / 1024.0];
100    else //insane speeds
101        return [NSString stringWithFormat: @"%.2f G", speed / 1048576.0];
102}
103
104+ (NSString *) stringForRatio: (float) ratio
105{
106    if (ratio == TR_RATIO_NA)
107        return NSLocalizedString(@"N/A", "No Ratio");
108    else if (ratio == TR_RATIO_INF)
109        return [NSString stringWithUTF8String: "\xE2\x88\x9E"];
110    else if (ratio < 0)
111        return NSLocalizedString(@"error", "Ratio invalid");
112   
113    if (ratio < 10.0)
114        return [NSString stringWithFormat: @"%.2f", ratio];
115    else if (ratio < 100.0)
116        return [NSString stringWithFormat: @"%.1f", ratio];
117    else
118        return [NSString stringWithFormat: @"%.0f", ratio];
119}
120
121- (NSComparisonResult) compareIP: (NSString *) string
122{
123    NSArray * selfSections = [self componentsSeparatedByString: @"."],
124            * newSections = [string componentsSeparatedByString: @"."];
125   
126    if ([selfSections count] != [newSections count])
127        return [selfSections count] > [newSections count] ? NSOrderedDescending : NSOrderedAscending;
128
129    NSEnumerator * selfSectionsEnum = [selfSections objectEnumerator], * newSectionsEnum = [newSections objectEnumerator];
130    NSString * selfString, * newString;
131    NSComparisonResult result;
132    while ((selfString = [selfSectionsEnum nextObject]) && (newString = [newSectionsEnum nextObject]))
133        if ((result = [selfString compare: newString options: NSNumericSearch]) != NSOrderedSame)
134            return result;
135   
136    return NSOrderedSame;
137}
138
139@end
Note: See TracBrowser for help on using the repository browser.