]> granicus.if.org Git - transmission/commitdiff
Refine the logic for determining the "partial/full file size" string. It's better...
authorMitchell Livingston <livings124@transmissionbt.com>
Tue, 11 Sep 2012 00:46:32 +0000 (00:46 +0000)
committerMitchell Livingston <livings124@transmissionbt.com>
Tue, 11 Sep 2012 00:46:32 +0000 (00:46 +0000)
macosx/Controller.m
macosx/NSStringAdditions.m

index 06954651553d041cc0c28f16e75d8282eaef4ea8..1aa25353536376e38972faabefb96c583578a4ba 100644 (file)
@@ -318,7 +318,7 @@ static void sleepCallback(void * controller, io_service_t y, natural_t messageTy
             [unitFormatter setAllowsNonnumericFormatting: NO];
             
             [unitFormatter setAllowedUnits: NSByteCountFormatterUseKB];
-            kbString = [unitFormatter stringFromByteCount: 17]; //use a random value to avoid possible pluralization issues with 1 or 0
+            kbString = [unitFormatter stringFromByteCount: 17]; //use a random value to avoid possible pluralization issues with 1 or 0 (an example is if we use 1 for bytes, we'd get "byte" when we'd want "bytes" for the generic libtransmission value at least)
             
             [unitFormatter setAllowedUnits: NSByteCountFormatterUseMB];
             mbString = [unitFormatter stringFromByteCount: 17];
index ea0531258fb0e1af9929a5e0740e6bdc1123305a..97ff5426c6019f0dbf2f0b308b64960aa7db921e 100644 (file)
     {
         NSByteCountFormatter * fileSizeFormatter = [[NSByteCountFormatterMtLion alloc] init];
         
-        //only show units for the partial file size if it's different than the full file size's
-        [fileSizeFormatter setIncludesCount: NO];
-        const BOOL partialUnitsDifferent = ![[fileSizeFormatter stringFromByteCount: partialSize] isEqualToString: [fileSizeFormatter stringFromByteCount: fullSize]];
-        
-        [fileSizeFormatter setIncludesCount: YES];
         fullString = [fileSizeFormatter stringFromByteCount: fullSize];
         
-        [fileSizeFormatter setIncludesUnit: partialUnitsDifferent];
+        //figure out the magniture of the two, since we can't rely on comparing the units because of localization and pluralization issues (for example, "1 byte of 2 bytes")
+        BOOL partialUnitsSame;
+        if (partialSize == 0)
+            partialUnitsSame = YES; //we want to just show "0" when we have no partial data, so always set to the same units
+        else
+        {
+            //we have to catch 0 with a special case, so might as well avoid the math for all of magnitude 0
+            const unsigned int magnitudePartial = partialSize >= 1000 ? log(partialSize)/log(1000) : 0;
+            const unsigned int magnitudeFull = fullSize >= 1000 ? log(fullSize)/log(1000) : 0;
+            partialUnitsSame = magnitudePartial == magnitudeFull;
+        }
+        
+        [fileSizeFormatter setIncludesUnit: !partialUnitsSame];
         partialString = [fileSizeFormatter stringFromByteCount: partialSize];
         
         [fileSizeFormatter release];