Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | c217373 | 2015-05-13 13:18:04 -0700 | [diff] [blame] | 17 | #include "utils.h" |
| 18 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 19 | #include <inttypes.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | #include <string> |
| 25 | |
Elliott Hughes | c217373 | 2015-05-13 13:18:04 -0700 | [diff] [blame] | 26 | #include <base/stringprintf.h> |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 27 | |
| 28 | int Round(int n) { |
| 29 | int base = 1; |
| 30 | while (base*10 < n) { |
| 31 | base *= 10; |
| 32 | } |
| 33 | if (n < 2*base) { |
| 34 | return 2*base; |
| 35 | } |
| 36 | if (n < 5*base) { |
| 37 | return 5*base; |
| 38 | } |
| 39 | return 10*base; |
| 40 | } |
| 41 | |
| 42 | // Similar to the code in art, but supporting both binary and decimal prefixes. |
| 43 | std::string PrettyInt(long value, size_t base) { |
| 44 | if (base != 2 && base != 10) abort(); |
| 45 | |
| 46 | uint64_t count = static_cast<uint64_t>(value); |
| 47 | bool negative_number = false; |
| 48 | if (value < 0) { |
| 49 | negative_number = true; |
| 50 | count = static_cast<uint64_t>(-value); |
| 51 | } |
| 52 | |
| 53 | // The byte thresholds at which we display amounts. A count is displayed |
| 54 | // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1]. |
| 55 | static const uint64_t kUnitThresholds2[] = { |
| 56 | 1024*1024*1024 /* Gi */, 2*1024*1024 /* Mi */, 3*1024 /* Ki */, 0, |
| 57 | }; |
| 58 | static const uint64_t kUnitThresholds10[] = { |
| 59 | 1000*1000*1000 /* G */, 2*1000*1000 /* M */, 3*1000 /* k */, 0, |
| 60 | }; |
| 61 | static const uint64_t kAmountPerUnit2[] = { 1024*1024*1024, 1024*1024, 1024, 1 }; |
| 62 | static const uint64_t kAmountPerUnit10[] = { 1000*1000*1000, 1000*1000, 1000, 1 }; |
| 63 | static const char* const kUnitStrings2[] = { "Gi", "Mi", "Ki", "" }; |
| 64 | static const char* const kUnitStrings10[] = { "G", "M", "k", "" }; |
| 65 | |
| 66 | // Which set are we using? |
| 67 | const uint64_t* kUnitThresholds = ((base == 2) ? kUnitThresholds2 : kUnitThresholds10); |
| 68 | const uint64_t* kAmountPerUnit = ((base == 2) ? kAmountPerUnit2 : kAmountPerUnit10); |
| 69 | const char* const* kUnitStrings = ((base == 2) ? kUnitStrings2 : kUnitStrings10); |
| 70 | |
| 71 | size_t i = 0; |
| 72 | for (; kUnitThresholds[i] != 0; ++i) { |
| 73 | if (count >= kUnitThresholds[i]) { |
| 74 | break; |
| 75 | } |
| 76 | } |
Elliott Hughes | c217373 | 2015-05-13 13:18:04 -0700 | [diff] [blame] | 77 | return android::base::StringPrintf("%s%" PRId64 "%s", |
| 78 | negative_number ? "-" : "", |
| 79 | count / kAmountPerUnit[i], kUnitStrings[i]); |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 80 | } |