Changeset 6960


Ignore:
Timestamp:
Oct 25, 2008, 9:49:06 PM (13 years ago)
Author:
livings124
Message:

clean up the string additions class

Location:
trunk/macosx
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/NSStringAdditions.h

    r6732 r6960  
    3232+ (NSString *) stringForFileSize: (uint64_t) size;
    3333
    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;
    3737
    3838+ (NSString *) timeString: (uint64_t) seconds showSeconds: (BOOL) showSeconds;
  • trunk/macosx/NSStringAdditions.m

    r6732 r6960  
    4444        return [NSString stringWithFormat: @"%lld %@", size, NSLocalizedString(@"bytes", "File size - bytes")];
    4545
    46     float convertedSize;
     46    CGFloat convertedSize;
    4747    NSString * unit;
    48     if (size < 1048576)
     48    if (size < pow(1024, 2))
    4949    {
    5050        convertedSize = size / 1024.0;
    5151        unit = NSLocalizedString(@"KB", "File size - kilobytes");
    5252    }
    53     else if (size < 1073741824)
     53    else if (size < pow(1024, 3))
    5454    {
    55         convertedSize = size / 1048576.0;
     55        convertedSize = size / powf(1024.0f, 2.0f);
    5656        unit = NSLocalizedString(@"MB", "File size - megabytes");
    5757    }
    58     else if (size < 1099511627776.0)
     58    else if (size < pow(1024, 4))
    5959    {
    60         convertedSize = size / 1073741824.0;
     60        convertedSize = size / powf(1024.0f, 3.0f);
    6161        unit = NSLocalizedString(@"GB", "File size - gigabytes");
    6262    }
    6363    else
    6464    {
    65         convertedSize = size / 1099511627776.0;
     65        convertedSize = size / powf(1024.0f, 4.0f);
    6666        unit = NSLocalizedString(@"TB", "File size - terabytes");
    6767    }
    6868   
    6969    //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]
    7171                                : [NSString localizedStringWithFormat: @"%.1f %@", convertedSize, unit];
    7272}
    7373
    74 + (NSString *) stringForSpeed: (float) speed
     74+ (NSString *) stringForSpeed: (CGFloat) speed
    7575{
    7676    return [[self stringForSpeedAbbrev: speed] stringByAppendingString: NSLocalizedString(@"B/s", "Transfer speed (Bytes per second)")];
    7777}
    7878
    79 + (NSString *) stringForSpeedAbbrev: (float) speed
     79+ (NSString *) stringForSpeedAbbrev: (CGFloat) speed
    8080{
    81     if (speed <= 999.95) //0.0 K to 999.9 K
     81    if (speed <= 999.95f) //0.0 K to 999.9 K
    8282        return [NSString localizedStringWithFormat: @"%.1f K", speed];
    8383   
    84     speed /= 1024.0;
     84    speed /= 1024.0f;
    8585   
    86     if (speed <= 99.995) //0.98 M to 99.99 M
     86    if (speed <= 99.995f) //0.98 M to 99.99 M
    8787        return [NSString localizedStringWithFormat: @"%.2f M", speed];
    88     else if (speed <= 999.95) //100.0 M to 999.9 M
     88    else if (speed <= 999.95f) //100.0 M to 999.9 M
    8989        return [NSString localizedStringWithFormat: @"%.1f M", speed];
    9090    else //insane speeds
    91         return [NSString localizedStringWithFormat: @"%.2f G", (speed / 1024.0)];
     91        return [NSString localizedStringWithFormat: @"%.2f G", (speed / 1024.0f)];
    9292}
    9393
    94 + (NSString *) stringForRatio: (float) ratio
     94+ (NSString *) stringForRatio: (CGFloat) ratio
    9595{
    9696    if (ratio == TR_RATIO_NA)
     
    100100    else;
    101101   
    102     if (ratio <= 9.995) //0.00 to 9.99
     102    if (ratio <= 9.995f) //0.00 to 9.99
    103103        return [NSString localizedStringWithFormat: @"%.2f", ratio];
    104     else if (ratio <= 99.95) //10.0 to 99.9
     104    else if (ratio <= 99.95f) //10.0 to 99.9
    105105        return [NSString localizedStringWithFormat: @"%.1f", ratio];
    106106    else //rest are single digit
     
    118118    NSUInteger remaining = seconds;
    119119   
    120     if (max > 0 && seconds >= 86400) //24 * 60 * 60
     120    if (max > 0 && seconds >= (24 * 60 * 60))
    121121    {
    122         int days = remaining / 86400;
     122        NSInteger days = remaining / (24 * 60 * 60);
    123123        if (days == 1)
    124124            [timeArray addObject: NSLocalizedString(@"1 day", "time string")];
    125125        else
    126126            [timeArray addObject: [NSString stringWithFormat: NSLocalizedString(@"%u days", "time string"), days]];
    127         remaining %= 86400;
     127        remaining %= (24 * 60 * 60);
    128128        max--;
    129129    }
    130     if (max > 0 && seconds >= 3600) //60 * 60
     130    if (max > 0 && seconds >= (60 * 60))
    131131    {
    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);
    134134        max--;
    135135    }
     
    148148- (NSComparisonResult) compareFinder: (NSString *) string
    149149{
    150     int comparisonOptions = [NSApp isOnLeopardOrBetter] ? (NSCaseInsensitiveSearch | NSNumericSearch
     150    NSInteger comparisonOptions = [NSApp isOnLeopardOrBetter] ? (NSCaseInsensitiveSearch | NSNumericSearch
    151151                                                            | NSWidthInsensitiveSearch | NSForcedOrderingSearch)
    152152                                                        : (NSCaseInsensitiveSearch | NSNumericSearch);
     
    156156- (NSComparisonResult) compareNumeric: (NSString *) string
    157157{
    158     int comparisonOptions = [NSApp isOnLeopardOrBetter] ? (NSNumericSearch | NSForcedOrderingSearch) : NSNumericSearch;
     158    NSInteger comparisonOptions = [NSApp isOnLeopardOrBetter] ? (NSNumericSearch | NSForcedOrderingSearch) : NSNumericSearch;
    159159    return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]];
    160160}
Note: See TracChangeset for help on using the changeset viewer.