YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/gutil/strings/human_readable.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2007 Google Inc. All Rights Reserved.
2
//
3
// The following only applies to changes made to this file as part of YugaByte development.
4
//
5
// Portions Copyright (c) YugaByte, Inc.
6
//
7
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8
// in compliance with the License.  You may obtain a copy of the License at
9
//
10
// http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing, software distributed under the License
13
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14
// or implied.  See the License for the specific language governing permissions and limitations
15
// under the License.
16
//
17
// A collection of methods to convert back and forth between a number
18
// and a human-readable string representing the number.
19
20
#ifndef YB_GUTIL_STRINGS_HUMAN_READABLE_H
21
#define YB_GUTIL_STRINGS_HUMAN_READABLE_H
22
23
#include <functional>
24
25
#include "yb/gutil/integral_types.h"
26
#include "yb/gutil/macros.h"
27
28
using std::binary_function;
29
using std::less;
30
using std::string;
31
32
33
//                                 WARNING
34
// HumanReadable{NumBytes, Int} don't give you the standard set of SI prefixes.
35
//
36
// HumanReadableNumBytes uses binary powers -- 1M = 1 << 20 -- but for numbers
37
// less than 1024, it adds the suffix "B" for "bytes." It is OK when you need
38
// to print a literal number of bytes, but can be awfully confusing for
39
// anything else.
40
//
41
// HumanReadableInt uses decimal powers -- 1M = 10^3 -- but prints
42
// 'B'-for-billion instead of 'G'-for-giga. It's good for representing
43
// true numbers, like how many documents are in a repository.
44
// HumanReadableNum is the same as HumanReadableInt but has additional
45
// support for DoubleToString(), where smaller numbers will print more
46
// (up to 3) decimal places.
47
//
48
// If you want SI prefixes, use the functions in si_prefix.h instead; for
49
// example, strings::si_prefix::ToDecimalString(1053.2) == "1.05k".
50
51
class HumanReadableNumBytes {
52
 public:
53
  // Converts between an int64 representing a number of bytes and a
54
  // human readable string representing the same number.
55
  // e.g. 1000000 -> "976.6K".
56
  //  Note that calling these two functions in succession isn't a
57
  //  noop, since ToString() may round.
58
  static bool ToInt64(const string &str, int64 *num_bytes);
59
  static string ToString(int64 num_bytes);
60
  // Like ToString but without rounding.  For example 1025 would return
61
  // "1025B" rather than "1.0K".  Uses the largest common denominator.
62
  static string ToStringWithoutRounding(int64 num_bytes);
63
64
  static bool ToDouble(const string &str, double *num_bytes);
65
  // Function overloading this with a function that takes an int64 is just
66
  // asking for trouble.
67
  static string DoubleToString(double num_bytes);
68
69
  // TODO(user): Maybe change this class to use SIPrefix?
70
71
  // ----------------------------------------------------------------------
72
  // LessThan
73
  // humanreadablebytes_less
74
  // humanreadablebytes_greater
75
  //    These numerically compare the values encoded in strings by
76
  //    ToString().  Strings which cannot be parsed are treated as
77
  //    if they represented the value 0.  The following byte sizes
78
  //    would be sorted as:
79
  //        3B
80
  //        .06K
81
  //        .03M
82
  //        10000G
83
  //        10T
84
  //        3.01P
85
  //        3.02P
86
  //        0.007E
87
  // ----------------------------------------------------------------------
88
  static bool LessThan(const string &a, const string &b);
89
90
 private:
91
  DISALLOW_IMPLICIT_CONSTRUCTORS(HumanReadableNumBytes);
92
};
93
94
95
// See documentation at HumanReadableNumBytes::LessThan().
96
struct humanreadablebytes_less
97
    : public binary_function<const string&, const string&, bool> {
98
0
  bool operator()(const string& a, const string &b) const {
99
0
    return HumanReadableNumBytes::LessThan(a, b);
100
0
  }
101
};
102
103
// See documentation at HumanReadableNumBytes::LessThan().
104
struct humanreadablebytes_greater
105
    : public binary_function<const string&, const string&, bool> {
106
0
  bool operator()(const string& a, const string &b) const {
107
0
    return HumanReadableNumBytes::LessThan(b, a);
108
0
  }
109
};
110
111
class HumanReadableInt {
112
 public:
113
  // Similar to HumanReadableNumBytes::ToInt64(), but uses decimal
114
  // rather than binary expansions - so M = 1 million, B = 1 billion,
115
  // etc. Numbers beyond 1T are expressed as "3E14" etc.
116
  static string ToString(int64 value);
117
118
  // Reverses ToString(). Note that calling these two functions in
119
  // succession isn't a noop, since ToString() may round.
120
  static bool ToInt64(const string &str, int64 *value);
121
122
 private:
123
  DISALLOW_IMPLICIT_CONSTRUCTORS(HumanReadableInt);
124
};
125
126
class HumanReadableNum {
127
 public:
128
  // Same as HumanReadableInt::ToString().
129
  static string ToString(int64 value);
130
131
  // Similar to HumanReadableInt::ToString(), but prints 2 decimal
132
  // places for numbers with absolute value < 10.0 and 1 decimal place
133
  // for numbers >= 10.0 and < 100.0.
134
  static string DoubleToString(double value);
135
136
  // Reverses DoubleToString(). Note that calling these two functions in
137
  // succession isn't a noop, since there may be rounding errors.
138
  static bool ToDouble(const string &str, double *value);
139
140
 private:
141
  DISALLOW_IMPLICIT_CONSTRUCTORS(HumanReadableNum);
142
};
143
144
class HumanReadableElapsedTime {
145
 public:
146
  // Converts a time interval as double to a human readable
147
  // string. For example:
148
  //   0.001       -> "1 ms"
149
  //   10.0        -> "10 s"
150
  //   933120.0    -> "10.8 days"
151
  //   39420000.0  -> "1.25 years"
152
  //   -10         -> "-10 s"
153
  static string ToShortString(double seconds);
154
155
  // Reverses ToShortString(). Note that calling these two functions in
156
  // succession isn't a noop, since ToShortString() may round.
157
  // This accepts multiple forms of units, but the abbreviated forms are
158
  // us (microseconds), ms (milliseconds), s, m (minutes), h, d, w,
159
  // M (month = 30 days), y
160
  // This function is not particularly fast.  Use at performance peril.
161
  // Only leading negative signs are allowed.
162
  // Examples:
163
  //   "1ms"        -> 0.001
164
  //   "10 second"  -> 10
165
  //   "10.8 days"  -> 933120.0
166
  //   "1m 30s"     -> 90
167
  //   "-10 sec"    -> -10
168
  //   "18.3"       -> 18.3
169
  //   "1M"         -> 2592000 (1 month = 30 days)
170
  static bool ToDouble(const string& str, double* value);
171
172
 private:
173
  DISALLOW_IMPLICIT_CONSTRUCTORS(HumanReadableElapsedTime);
174
};
175
176
#endif  // YB_GUTIL_STRINGS_HUMAN_READABLE_H