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__) || defined(__CYGWIN__) ||\
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)));
332 StringPiece skipWhitespace(StringPiece sp) {
333 // Spaces other than ' ' characters are less common but should be
334 // checked. This configuration where we loop on the ' '
335 // separately from oddspaces was empirically fastest.
336 auto oddspace = [] (char c) {
337 return c == '\n' || c == '\t' || c == '\r';
341 for (; !sp.empty() && sp.front() == ' '; sp.pop_front()) {
343 if (!sp.empty() && oddspace(sp.front())) {
353 void toLowerAscii8(char& c) {
354 // Branchless tolower, based on the input-rotating trick described
355 // at http://www.azillionmonkeys.com/qed/asmexample.html
357 // This algorithm depends on an observation: each uppercase
358 // ASCII character can be converted to its lowercase equivalent
361 // Step 1: Clear the high order bit. We'll deal with it in Step 5.
362 unsigned char rotated = c & 0x7f;
363 // Currently, the value of rotated, as a function of the original c is:
368 // Step 2: Add 0x25 (37)
370 // Now the value of rotated, as a function of the original c is:
373 // above 'Z': 128-164
375 // Step 3: clear the high order bit
381 // Step 4: Add 0x1a (26)
387 // At this point, note that only the uppercase letters have been
388 // transformed into values with the high order bit set (128 and above).
390 // Step 5: Shift the high order bit 2 spaces to the right: the spot
391 // where the only 1 bit in 0x20 is. But first, how we ignored the
392 // high order bit of the original c in step 1? If that bit was set,
393 // we may have just gotten a false match on a value in the range
394 // 128+'A' to 128+'Z'. To correct this, need to clear the high order
395 // bit of rotated if the high order bit of c is set. Since we don't
396 // care about the other bits in rotated, the easiest thing to do
397 // is invert all the bits in c and bitwise-and them with rotated.
401 // Step 6: Apply a mask to clear everything except the 0x20 bit
405 // At this point, rotated is 0x20 if c is 'A'-'Z' and 0x00 otherwise
407 // Step 7: Add rotated to c
411 void toLowerAscii32(uint32_t& c) {
412 // Besides being branchless, the algorithm in toLowerAscii8() has another
413 // interesting property: None of the addition operations will cause
414 // an overflow in the 8-bit value. So we can pack four 8-bit values
415 // into a uint32_t and run each operation on all four values in parallel
416 // without having to use any CPU-specific SIMD instructions.
417 uint32_t rotated = c & uint32_t(0x7f7f7f7fL);
418 rotated += uint32_t(0x25252525L);
419 rotated &= uint32_t(0x7f7f7f7fL);
420 rotated += uint32_t(0x1a1a1a1aL);
422 // Step 5 involves a shift, so some bits will spill over from each
423 // 8-bit value into the next. But that's okay, because they're bits
424 // that will be cleared by the mask in step 6 anyway.
427 rotated &= uint32_t(0x20202020L);
431 void toLowerAscii64(uint64_t& c) {
432 // 64-bit version of toLower32
433 uint64_t rotated = c & uint64_t(0x7f7f7f7f7f7f7f7fL);
434 rotated += uint64_t(0x2525252525252525L);
435 rotated &= uint64_t(0x7f7f7f7f7f7f7f7fL);
436 rotated += uint64_t(0x1a1a1a1a1a1a1a1aL);
439 rotated &= uint64_t(0x2020202020202020L);
445 void toLowerAscii(char* str, size_t length) {
446 static const size_t kAlignMask64 = 7;
447 static const size_t kAlignMask32 = 3;
449 // Convert a character at a time until we reach an address that
450 // is at least 32-bit aligned
451 size_t n = (size_t)str;
453 n = std::min(n, length);
456 n = std::min(4 - n, length);
458 toLowerAscii8(str[offset]);
460 } while (offset < n);
463 n = (size_t)(str + offset);
465 if ((n != 0) && (offset + 4 <= length)) {
466 // The next address is 32-bit aligned but not 64-bit aligned.
467 // Convert the next 4 bytes in order to get to the 64-bit aligned
468 // part of the input.
469 toLowerAscii32(*(uint32_t*)(str + offset));
473 // Convert 8 characters at a time
474 while (offset + 8 <= length) {
475 toLowerAscii64(*(uint64_t*)(str + offset));
479 // Convert 4 characters at a time
480 while (offset + 4 <= length) {
481 toLowerAscii32(*(uint32_t*)(str + offset));
485 // Convert any characters remaining after the last 4-byte aligned group
486 while (offset < length) {
487 toLowerAscii8(str[offset]);
494 size_t hexDumpLine(const void* ptr, size_t offset, size_t size,
499 // (1+2)*16: hex bytes, each preceded by a space
500 // 1: space separating the two halves
507 const uint8_t* p = reinterpret_cast<const uint8_t*>(ptr) + offset;
508 size_t n = std::min(size - offset, size_t(16));
509 format("{:08x} ", offset).appendTo(line);
511 for (size_t i = 0; i < n; i++) {
515 format(" {:02x}", p[i]).appendTo(line);
518 // 3 spaces for each byte we're not printing, one separating the halves
520 line.append(3 * (16 - n) + (n <= 8), ' ');
523 for (size_t i = 0; i < n; i++) {
524 char c = (p[i] >= 32 && p[i] <= 126 ? static_cast<char>(p[i]) : '.');
527 line.append(16 - n, ' ');
529 DCHECK_EQ(line.size(), 78);
534 } // namespace detail
538 #ifdef FOLLY_DEFINED_DMGL
539 # undef FOLLY_DEFINED_DMGL
546 # undef DMGL_RET_POSTFIX