Changeset 6960
- Timestamp:
- Oct 25, 2008, 9:49:06 PM (12 years ago)
- Location:
- trunk/macosx
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/macosx/NSStringAdditions.h
r6732 r6960 32 32 + (NSString *) stringForFileSize: (uint64_t) size; 33 33 34 + (NSString *) stringForSpeed: ( float) speed;35 + (NSString *) stringForSpeedAbbrev: ( float) speed;36 + (NSString *) stringForRatio: ( float) ratio;34 + (NSString *) stringForSpeed: (CGFloat) speed; 35 + (NSString *) stringForSpeedAbbrev: (CGFloat) speed; 36 + (NSString *) stringForRatio: (CGFloat) ratio; 37 37 38 38 + (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds; -
trunk/macosx/NSStringAdditions.m
r6732 r6960 44 44 return [NSString stringWithFormat: @"%lld %@", size, NSLocalizedString(@"bytes", "File size - bytes")]; 45 45 46 float convertedSize;46 CGFloat convertedSize; 47 47 NSString * unit; 48 if (size < 1048576)48 if (size < pow(1024, 2)) 49 49 { 50 50 convertedSize = size / 1024.0; 51 51 unit = NSLocalizedString(@"KB", "File size - kilobytes"); 52 52 } 53 else if (size < 1073741824)53 else if (size < pow(1024, 3)) 54 54 { 55 convertedSize = size / 1048576.0;55 convertedSize = size / powf(1024.0f, 2.0f); 56 56 unit = NSLocalizedString(@"MB", "File size - megabytes"); 57 57 } 58 else if (size < 1099511627776.0)58 else if (size < pow(1024, 4)) 59 59 { 60 convertedSize = size / 1073741824.0;60 convertedSize = size / powf(1024.0f, 3.0f); 61 61 unit = NSLocalizedString(@"GB", "File size - gigabytes"); 62 62 } 63 63 else 64 64 { 65 convertedSize = size / 1099511627776.0;65 convertedSize = size / powf(1024.0f, 4.0f); 66 66 unit = NSLocalizedString(@"TB", "File size - terabytes"); 67 67 } 68 68 69 69 //attempt to have minimum of 3 digits with at least 1 decimal 70 return convertedSize <= 9.995 ? [NSString localizedStringWithFormat: @"%.2f %@", convertedSize, unit]70 return convertedSize <= 9.995f ? [NSString localizedStringWithFormat: @"%.2f %@", convertedSize, unit] 71 71 : [NSString localizedStringWithFormat: @"%.1f %@", convertedSize, unit]; 72 72 } 73 73 74 + (NSString *) stringForSpeed: ( float) speed74 + (NSString *) stringForSpeed: (CGFloat) speed 75 75 { 76 76 return [[self stringForSpeedAbbrev: speed] stringByAppendingString: NSLocalizedString(@"B/s", "Transfer speed (Bytes per second)")]; 77 77 } 78 78 79 + (NSString *) stringForSpeedAbbrev: ( float) speed79 + (NSString *) stringForSpeedAbbrev: (CGFloat) speed 80 80 { 81 if (speed <= 999.95 ) //0.0 K to 999.9 K81 if (speed <= 999.95f) //0.0 K to 999.9 K 82 82 return [NSString localizedStringWithFormat: @"%.1f K", speed]; 83 83 84 speed /= 1024.0 ;84 speed /= 1024.0f; 85 85 86 if (speed <= 99.995 ) //0.98 M to 99.99 M86 if (speed <= 99.995f) //0.98 M to 99.99 M 87 87 return [NSString localizedStringWithFormat: @"%.2f M", speed]; 88 else if (speed <= 999.95 ) //100.0 M to 999.9 M88 else if (speed <= 999.95f) //100.0 M to 999.9 M 89 89 return [NSString localizedStringWithFormat: @"%.1f M", speed]; 90 90 else //insane speeds 91 return [NSString localizedStringWithFormat: @"%.2f G", (speed / 1024.0 )];91 return [NSString localizedStringWithFormat: @"%.2f G", (speed / 1024.0f)]; 92 92 } 93 93 94 + (NSString *) stringForRatio: ( float) ratio94 + (NSString *) stringForRatio: (CGFloat) ratio 95 95 { 96 96 if (ratio == TR_RATIO_NA) … … 100 100 else; 101 101 102 if (ratio <= 9.995 ) //0.00 to 9.99102 if (ratio <= 9.995f) //0.00 to 9.99 103 103 return [NSString localizedStringWithFormat: @"%.2f", ratio]; 104 else if (ratio <= 99.95 ) //10.0 to 99.9104 else if (ratio <= 99.95f) //10.0 to 99.9 105 105 return [NSString localizedStringWithFormat: @"%.1f", ratio]; 106 106 else //rest are single digit … … 118 118 NSUInteger remaining = seconds; 119 119 120 if (max > 0 && seconds >= 86400) //24 * 60 * 60120 if (max > 0 && seconds >= (24 * 60 * 60)) 121 121 { 122 int days = remaining / 86400;122 NSInteger days = remaining / (24 * 60 * 60); 123 123 if (days == 1) 124 124 [timeArray addObject: NSLocalizedString(@"1 day", "time string")]; 125 125 else 126 126 [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u days", "time string"), days]]; 127 remaining %= 86400;127 remaining %= (24 * 60 * 60); 128 128 max--; 129 129 } 130 if (max > 0 && seconds >= 3600) //60 * 60130 if (max > 0 && seconds >= (60 * 60)) 131 131 { 132 [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u hr", "time string"), remaining / 3600]];133 remaining %= 3600;132 [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u hr", "time string"), remaining / (60 * 60)]]; 133 remaining %= (60 * 60); 134 134 max--; 135 135 } … … 148 148 - (NSComparisonResult) compareFinder: (NSString *) string 149 149 { 150 intcomparisonOptions = [NSApp isOnLeopardOrBetter] ? (NSCaseInsensitiveSearch | NSNumericSearch150 NSInteger comparisonOptions = [NSApp isOnLeopardOrBetter] ? (NSCaseInsensitiveSearch | NSNumericSearch 151 151 | NSWidthInsensitiveSearch | NSForcedOrderingSearch) 152 152 : (NSCaseInsensitiveSearch | NSNumericSearch); … … 156 156 - (NSComparisonResult) compareNumeric: (NSString *) string 157 157 { 158 intcomparisonOptions = [NSApp isOnLeopardOrBetter] ? (NSNumericSearch | NSForcedOrderingSearch) : NSNumericSearch;158 NSInteger comparisonOptions = [NSApp isOnLeopardOrBetter] ? (NSNumericSearch | NSForcedOrderingSearch) : NSNumericSearch; 159 159 return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]]; 160 160 }
Note: See TracChangeset
for help on using the changeset viewer.