2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "folly/String.h"
18 #include "folly/Format.h"
26 #include <glog/logging.h>
32 inline void stringPrintfImpl(std::string& output, const char* format,
34 // Tru to the space at the end of output for our output buffer.
35 // Find out write point then inflate its size temporarily to its
36 // capacity; we will later shrink it to the size needed to represent
37 // the formatted string. If this buffer isn't large enough, we do a
38 // resize and try again.
40 const auto write_point = output.size();
41 auto remaining = output.capacity() - write_point;
42 output.resize(output.capacity());
45 va_copy(args_copy, args);
46 int bytes_used = vsnprintf(&output[write_point], remaining, format,
50 throw std::runtime_error(
51 to<std::string>("Invalid format string; snprintf returned negative "
52 "with format string: ", format));
53 } else if (bytes_used < remaining) {
54 // There was enough room, just shrink and return.
55 output.resize(write_point + bytes_used);
57 output.resize(write_point + bytes_used + 1);
58 remaining = bytes_used + 1;
60 va_copy(args_copy, args);
61 bytes_used = vsnprintf(&output[write_point], remaining, format,
64 if (bytes_used + 1 != remaining) {
65 throw std::runtime_error(
66 to<std::string>("vsnprint retry did not manage to work "
67 "with format string: ", format));
69 output.resize(write_point + bytes_used);
75 std::string stringPrintf(const char* format, ...) {
76 // snprintf will tell us how large the output buffer should be, but
77 // we then have to call it a second time, which is costly. By
78 // guestimating the final size, we avoid the double snprintf in many
79 // cases, resulting in a performance win. We use this constructor
80 // of std::string to avoid a double allocation, though it does pad
81 // the resulting string with nul bytes. Our guestimation is twice
82 // the format string size, or 32 bytes, whichever is larger. This
83 // is a hueristic that doesn't affect correctness but attempts to be
84 // reasonably fast for the most common cases.
85 std::string ret(std::max(32UL, strlen(format) * 2), '\0');
90 stringPrintfImpl(ret, format, ap);
95 // Basic declarations; allow for parameters of strings and string
96 // pieces to be specified.
97 std::string& stringAppendf(std::string* output, const char* format, ...) {
100 stringPrintfImpl(*output, format, ap);
105 void stringPrintf(std::string* output, const char* format, ...) {
108 va_start(ap, format);
109 stringPrintfImpl(*output, format, ap);
115 struct PrettySuffix {
120 const PrettySuffix kPrettyTimeSuffixes[] = {
130 const PrettySuffix kPrettyBytesMetricSuffixes[] = {
139 const PrettySuffix kPrettyBytesBinarySuffixes[] = {
140 { "TB", int64_t(1) << 40 },
141 { "GB", int64_t(1) << 30 },
142 { "MB", int64_t(1) << 20 },
143 { "kB", int64_t(1) << 10 },
148 const PrettySuffix kPrettyBytesBinaryIECSuffixes[] = {
149 { "TiB", int64_t(1) << 40 },
150 { "GiB", int64_t(1) << 30 },
151 { "MiB", int64_t(1) << 20 },
152 { "KiB", int64_t(1) << 10 },
157 const PrettySuffix kPrettyUnitsMetricSuffixes[] = {
166 const PrettySuffix kPrettyUnitsBinarySuffixes[] = {
167 { "T", int64_t(1) << 40 },
168 { "G", int64_t(1) << 30 },
169 { "M", int64_t(1) << 20 },
170 { "k", int64_t(1) << 10 },
175 const PrettySuffix kPrettyUnitsBinaryIECSuffixes[] = {
176 { "Ti", int64_t(1) << 40 },
177 { "Gi", int64_t(1) << 30 },
178 { "Mi", int64_t(1) << 20 },
179 { "Ki", int64_t(1) << 10 },
184 const PrettySuffix kPrettySISuffixes[] = {
209 const PrettySuffix* const kPrettySuffixes[PRETTY_NUM_TYPES] = {
211 kPrettyBytesMetricSuffixes,
212 kPrettyBytesBinarySuffixes,
213 kPrettyBytesBinaryIECSuffixes,
214 kPrettyUnitsMetricSuffixes,
215 kPrettyUnitsBinarySuffixes,
216 kPrettyUnitsBinaryIECSuffixes,
222 std::string prettyPrint(double val, PrettyType type, bool addSpace) {
225 // pick the suffixes to use
227 assert(type < PRETTY_NUM_TYPES);
228 const PrettySuffix* suffixes = kPrettySuffixes[type];
230 // find the first suffix we're bigger than -- then use it
231 double abs_val = fabs(val);
232 for (int i = 0; suffixes[i].suffix; ++i) {
233 if (abs_val >= suffixes[i].val) {
234 snprintf(buf, sizeof buf, "%.4g%s%s",
235 (suffixes[i].val ? (val / suffixes[i].val)
237 (addSpace ? " " : ""),
239 return std::string(buf);
243 // no suffix, we've got a tiny value -- just print it in sci-notation
244 snprintf(buf, sizeof buf, "%.4g", val);
245 return std::string(buf);
249 //1) Benchmark & optimize
250 double prettyToDouble(folly::StringPiece *const prettyString,
251 const PrettyType type) {
252 double value = folly::to<double>(prettyString);
253 while (prettyString->size() > 0 && std::isspace(prettyString->front())) {
254 prettyString->advance(1); //Skipping spaces between number and suffix
256 const PrettySuffix* suffixes = kPrettySuffixes[type];
257 int longestPrefixLen = -1;
258 int bestPrefixId = -1;
259 for (int j = 0 ; suffixes[j].suffix; ++j) {
260 if (suffixes[j].suffix[0] == ' '){//Checking for " " -> number rule.
261 if (longestPrefixLen == -1) {
262 longestPrefixLen = 0; //No characters to skip
265 } else if (prettyString->startsWith(suffixes[j].suffix)) {
266 int suffixLen = strlen(suffixes[j].suffix);
267 //We are looking for a longest suffix matching prefix of the string
268 //after numeric value. We need this in case suffixes have common prefix.
269 if (suffixLen > longestPrefixLen) {
270 longestPrefixLen = suffixLen;
275 if (bestPrefixId == -1) { //No valid suffix rule found
276 throw std::invalid_argument(folly::to<std::string>(
277 "Unable to parse suffix \"",
278 prettyString->toString(), "\""));
280 prettyString->advance(longestPrefixLen);
281 return suffixes[bestPrefixId].val ? value * suffixes[bestPrefixId].val :
285 double prettyToDouble(folly::StringPiece prettyString, const PrettyType type){
286 double result = prettyToDouble(&prettyString, type);
287 detail::enforceWhitespace(prettyString.data(),
288 prettyString.data() + prettyString.size());
292 std::string hexDump(const void* ptr, size_t size) {
293 std::ostringstream os;
294 hexDump(ptr, size, std::ostream_iterator<StringPiece>(os, "\n"));
298 fbstring errnoStr(int err) {
299 int savedErrno = errno;
301 // Ensure that we reset errno upon exit.
302 auto guard(makeGuard([&] { errno = savedErrno; }));
309 // https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/strerror_r.3.html
310 // http://www.kernel.org/doc/man-pages/online/pages/man3/strerror.3.html
311 #if defined(__APPLE__) || defined(__FreeBSD__) || \
312 ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE)
313 // Using XSI-compatible strerror_r
314 int r = strerror_r(err, buf, sizeof(buf));
316 // OSX/FreeBSD use EINVAL and Linux uses -1 so just check for non-zero
318 result = to<fbstring>(
319 "Unknown error ", err,
320 " (strerror_r failed with error ", errno, ")");
325 // Using GNU strerror_r
326 result.assign(strerror_r(err, buf, sizeof(buf)));
334 size_t hexDumpLine(const void* ptr, size_t offset, size_t size,
339 // (1+2)*16: hex bytes, each preceded by a space
340 // 1: space separating the two halves
347 const uint8_t* p = reinterpret_cast<const uint8_t*>(ptr) + offset;
348 size_t n = std::min(size - offset, size_t(16));
349 format("{:08x} ", offset).appendTo(line);
351 for (size_t i = 0; i < n; i++) {
355 format(" {:02x}", p[i]).appendTo(line);
358 // 3 spaces for each byte we're not printing, one separating the halves
360 line.append(3 * (16 - n) + (n <= 8), ' ');
363 for (size_t i = 0; i < n; i++) {
364 char c = (p[i] >= 32 && p[i] <= 126 ? static_cast<char>(p[i]) : '.');
367 line.append(16 - n, ' ');
369 DCHECK_EQ(line.size(), 78);
374 } // namespace detail
378 #ifdef FOLLY_DEFINED_DMGL
379 # undef FOLLY_DEFINED_DMGL
386 # undef DMGL_RET_POSTFIX