feat(util): add binary unit label support to humanFileSize()#58026
Open
joshtrichards wants to merge 4 commits intomasterfrom
Open
feat(util): add binary unit label support to humanFileSize()#58026joshtrichards wants to merge 4 commits intomasterfrom
joshtrichards wants to merge 4 commits intomasterfrom
Conversation
Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
provokateurin
requested changes
Feb 9, 2026
Comment on lines
+373
to
+380
| if ($value < 1024 || $unitIndex === self::MAX_LABEL_INDEX) { | ||
| /** @var int<0, 5> $unitIndex */ | ||
| return "$value {$units[$unitIndex]}"; | ||
| } | ||
| } | ||
|
|
||
| $bytes = round($bytes / 1024, 1); | ||
| return "$bytes PB"; | ||
| // Unreachable, but keeps static analyzer happy | ||
| return "$value {$units[self::MAX_LABEL_INDEX]}"; |
Member
There was a problem hiding this comment.
Suggested change
| if ($value < 1024 || $unitIndex === self::MAX_LABEL_INDEX) { | |
| /** @var int<0, 5> $unitIndex */ | |
| return "$value {$units[$unitIndex]}"; | |
| } | |
| } | |
| $bytes = round($bytes / 1024, 1); | |
| return "$bytes PB"; | |
| // Unreachable, but keeps static analyzer happy | |
| return "$value {$units[self::MAX_LABEL_INDEX]}"; | |
| if ($value < 1024) { | |
| /** @var int<0, 5> $unitIndex */ | |
| return "$value {$units[$unitIndex]}"; | |
| } | |
| } | |
| return "$value {$units[self::MAX_LABEL_INDEX]}"; |
I think this is easier to understand and also doesn't need to workaround the analyzer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refactors
humanFileSize()to support IEC binary unit labels (KiB, MiB, GiB) while maintaining backward compatibility with existing decimal labels (KB, MB, GB). Math stays the same, but permits us to make the string be technically accurate where we'd prefer that (i.e. in logs - e.g. see #58019) versus where the conventional (albeit inaccurate) end-user friendly facing convention of using KB/MB/GB is preferred even when doing base-1024 math.Changes
$useBinaryLabelparameter (defaults tofalse)DECIMAL_LABELS,BINARY_LABELS, andMAX_LABEL_INDEXfor better maintainabilityMotivation
TODO
Checklist
3. to review, feature component)stable32)