2 * Copyright 2017 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>
26 #include <glog/logging.h>
28 #include <folly/ScopeGuard.h>
32 static_assert(IsConvertible<float>::value, "");
33 static_assert(IsConvertible<int>::value, "");
34 static_assert(IsConvertible<bool>::value, "");
35 static_assert(IsConvertible<int>::value, "");
36 static_assert(!IsConvertible<std::vector<int>>::value, "");
38 static inline bool is_oddspace(char c) {
39 return c == '\n' || c == '\t' || c == '\r';
42 StringPiece ltrimWhitespace(StringPiece sp) {
43 // Spaces other than ' ' characters are less common but should be
44 // checked. This configuration where we loop on the ' '
45 // separately from oddspaces was empirically fastest.
48 for (; !sp.empty() && sp.front() == ' '; sp.pop_front()) {
50 if (!sp.empty() && is_oddspace(sp.front())) {
58 StringPiece rtrimWhitespace(StringPiece sp) {
59 // Spaces other than ' ' characters are less common but should be
60 // checked. This configuration where we loop on the ' '
61 // separately from oddspaces was empirically fastest.
64 for (; !sp.empty() && sp.back() == ' '; sp.pop_back()) {
66 if (!sp.empty() && is_oddspace(sp.back())) {
76 int stringAppendfImplHelper(char* buf,
81 va_copy(args_copy, args);
82 int bytes_used = vsnprintf(buf, bufsize, format, args_copy);
87 void stringAppendfImpl(std::string& output, const char* format, va_list args) {
88 // Very simple; first, try to avoid an allocation by using an inline
89 // buffer. If that fails to hold the output string, allocate one on
90 // the heap, use it instead.
92 // It is hard to guess the proper size of this buffer; some
93 // heuristics could be based on the number of format characters, or
94 // static analysis of a codebase. Or, we can just pick a number
95 // that seems big enough for simple cases (say, one line of text on
96 // a terminal) without being large enough to be concerning as a
98 std::array<char, 128> inline_buffer;
100 int bytes_used = stringAppendfImplHelper(
101 inline_buffer.data(), inline_buffer.size(), format, args);
102 if (bytes_used < 0) {
103 throw std::runtime_error(to<std::string>(
104 "Invalid format string; snprintf returned negative "
105 "with format string: ",
109 if (static_cast<size_t>(bytes_used) < inline_buffer.size()) {
110 output.append(inline_buffer.data(), size_t(bytes_used));
114 // Couldn't fit. Heap allocate a buffer, oh well.
115 std::unique_ptr<char[]> heap_buffer(new char[size_t(bytes_used + 1)]);
116 int final_bytes_used = stringAppendfImplHelper(
117 heap_buffer.get(), size_t(bytes_used + 1), format, args);
118 // The second call can take fewer bytes if, for example, we were printing a
119 // string buffer with null-terminating char using a width specifier -
120 // vsnprintf("%.*s", buf.size(), buf)
121 CHECK(bytes_used >= final_bytes_used);
123 // We don't keep the trailing '\0' in our output string
124 output.append(heap_buffer.get(), size_t(final_bytes_used));
129 std::string stringPrintf(const char* format, ...) {
131 va_start(ap, format);
135 return stringVPrintf(format, ap);
138 std::string stringVPrintf(const char* format, va_list ap) {
140 stringAppendfImpl(ret, format, ap);
144 // Basic declarations; allow for parameters of strings and string
145 // pieces to be specified.
146 std::string& stringAppendf(std::string* output, const char* format, ...) {
148 va_start(ap, format);
152 return stringVAppendf(output, format, ap);
155 std::string& stringVAppendf(std::string* output,
158 stringAppendfImpl(*output, format, ap);
162 void stringPrintf(std::string* output, const char* format, ...) {
164 va_start(ap, format);
168 return stringVPrintf(output, format, ap);
171 void stringVPrintf(std::string* output, const char* format, va_list ap) {
173 stringAppendfImpl(*output, format, ap);
178 struct PrettySuffix {
183 const PrettySuffix kPrettyTimeSuffixes[] = {
193 const PrettySuffix kPrettyBytesMetricSuffixes[] = {
202 const PrettySuffix kPrettyBytesBinarySuffixes[] = {
203 { "TB", int64_t(1) << 40 },
204 { "GB", int64_t(1) << 30 },
205 { "MB", int64_t(1) << 20 },
206 { "kB", int64_t(1) << 10 },
211 const PrettySuffix kPrettyBytesBinaryIECSuffixes[] = {
212 { "TiB", int64_t(1) << 40 },
213 { "GiB", int64_t(1) << 30 },
214 { "MiB", int64_t(1) << 20 },
215 { "KiB", int64_t(1) << 10 },
220 const PrettySuffix kPrettyUnitsMetricSuffixes[] = {
229 const PrettySuffix kPrettyUnitsBinarySuffixes[] = {
230 { "T", int64_t(1) << 40 },
231 { "G", int64_t(1) << 30 },
232 { "M", int64_t(1) << 20 },
233 { "k", int64_t(1) << 10 },
238 const PrettySuffix kPrettyUnitsBinaryIECSuffixes[] = {
239 { "Ti", int64_t(1) << 40 },
240 { "Gi", int64_t(1) << 30 },
241 { "Mi", int64_t(1) << 20 },
242 { "Ki", int64_t(1) << 10 },
247 const PrettySuffix kPrettySISuffixes[] = {
272 const PrettySuffix* const kPrettySuffixes[PRETTY_NUM_TYPES] = {
274 kPrettyBytesMetricSuffixes,
275 kPrettyBytesBinarySuffixes,
276 kPrettyBytesBinaryIECSuffixes,
277 kPrettyUnitsMetricSuffixes,
278 kPrettyUnitsBinarySuffixes,
279 kPrettyUnitsBinaryIECSuffixes,
285 std::string prettyPrint(double val, PrettyType type, bool addSpace) {
288 // pick the suffixes to use
290 assert(type < PRETTY_NUM_TYPES);
291 const PrettySuffix* suffixes = kPrettySuffixes[type];
293 // find the first suffix we're bigger than -- then use it
294 double abs_val = fabs(val);
295 for (int i = 0; suffixes[i].suffix; ++i) {
296 if (abs_val >= suffixes[i].val) {
297 snprintf(buf, sizeof buf, "%.4g%s%s",
298 (suffixes[i].val ? (val / suffixes[i].val)
300 (addSpace ? " " : ""),
302 return std::string(buf);
306 // no suffix, we've got a tiny value -- just print it in sci-notation
307 snprintf(buf, sizeof buf, "%.4g", val);
308 return std::string(buf);
312 //1) Benchmark & optimize
313 double prettyToDouble(folly::StringPiece *const prettyString,
314 const PrettyType type) {
315 double value = folly::to<double>(prettyString);
316 while (prettyString->size() > 0 && std::isspace(prettyString->front())) {
317 prettyString->advance(1); //Skipping spaces between number and suffix
319 const PrettySuffix* suffixes = kPrettySuffixes[type];
320 int longestPrefixLen = -1;
321 int bestPrefixId = -1;
322 for (int j = 0 ; suffixes[j].suffix; ++j) {
323 if (suffixes[j].suffix[0] == ' '){//Checking for " " -> number rule.
324 if (longestPrefixLen == -1) {
325 longestPrefixLen = 0; //No characters to skip
328 } else if (prettyString->startsWith(suffixes[j].suffix)) {
329 int suffixLen = int(strlen(suffixes[j].suffix));
330 //We are looking for a longest suffix matching prefix of the string
331 //after numeric value. We need this in case suffixes have common prefix.
332 if (suffixLen > longestPrefixLen) {
333 longestPrefixLen = suffixLen;
338 if (bestPrefixId == -1) { //No valid suffix rule found
339 throw std::invalid_argument(folly::to<std::string>(
340 "Unable to parse suffix \"",
341 prettyString->toString(), "\""));
343 prettyString->advance(size_t(longestPrefixLen));
344 return suffixes[bestPrefixId].val ? value * suffixes[bestPrefixId].val :
348 double prettyToDouble(folly::StringPiece prettyString, const PrettyType type){
349 double result = prettyToDouble(&prettyString, type);
350 detail::enforceWhitespace(prettyString);
354 std::string hexDump(const void* ptr, size_t size) {
355 std::ostringstream os;
356 hexDump(ptr, size, std::ostream_iterator<StringPiece>(os, "\n"));
360 fbstring errnoStr(int err) {
361 int savedErrno = errno;
363 // Ensure that we reset errno upon exit.
364 auto guard(makeGuard([&] { errno = savedErrno; }));
371 // https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/strerror_r.3.html
372 // http://www.kernel.org/doc/man-pages/online/pages/man3/strerror.3.html
373 #if defined(_WIN32) && (defined(__MINGW32__) || defined(_MSC_VER))
374 // mingw64 has no strerror_r, but Windows has strerror_s, which C11 added
375 // as well. So maybe we should use this across all platforms (together
376 // with strerrorlen_s). Note strerror_r and _s have swapped args.
377 int r = strerror_s(buf, sizeof(buf), err);
379 result = to<fbstring>(
380 "Unknown error ", err,
381 " (strerror_r failed with error ", errno, ")");
385 #elif defined(FOLLY_HAVE_XSI_STRERROR_R) || \
386 defined(__APPLE__) || defined(__ANDROID__)
387 // Using XSI-compatible strerror_r
388 int r = strerror_r(err, buf, sizeof(buf));
390 // OSX/FreeBSD use EINVAL and Linux uses -1 so just check for non-zero
392 result = to<fbstring>(
393 "Unknown error ", err,
394 " (strerror_r failed with error ", errno, ")");
399 // Using GNU strerror_r
400 result.assign(strerror_r(err, buf, sizeof(buf)));
408 void toLowerAscii8(char& c) {
409 // Branchless tolower, based on the input-rotating trick described
410 // at http://www.azillionmonkeys.com/qed/asmexample.html
412 // This algorithm depends on an observation: each uppercase
413 // ASCII character can be converted to its lowercase equivalent
416 // Step 1: Clear the high order bit. We'll deal with it in Step 5.
417 uint8_t rotated = uint8_t(c & 0x7f);
418 // Currently, the value of rotated, as a function of the original c is:
423 // Step 2: Add 0x25 (37)
425 // Now the value of rotated, as a function of the original c is:
428 // above 'Z': 128-164
430 // Step 3: clear the high order bit
436 // Step 4: Add 0x1a (26)
442 // At this point, note that only the uppercase letters have been
443 // transformed into values with the high order bit set (128 and above).
445 // Step 5: Shift the high order bit 2 spaces to the right: the spot
446 // where the only 1 bit in 0x20 is. But first, how we ignored the
447 // high order bit of the original c in step 1? If that bit was set,
448 // we may have just gotten a false match on a value in the range
449 // 128+'A' to 128+'Z'. To correct this, need to clear the high order
450 // bit of rotated if the high order bit of c is set. Since we don't
451 // care about the other bits in rotated, the easiest thing to do
452 // is invert all the bits in c and bitwise-and them with rotated.
456 // Step 6: Apply a mask to clear everything except the 0x20 bit
460 // At this point, rotated is 0x20 if c is 'A'-'Z' and 0x00 otherwise
462 // Step 7: Add rotated to c
466 void toLowerAscii32(uint32_t& c) {
467 // Besides being branchless, the algorithm in toLowerAscii8() has another
468 // interesting property: None of the addition operations will cause
469 // an overflow in the 8-bit value. So we can pack four 8-bit values
470 // into a uint32_t and run each operation on all four values in parallel
471 // without having to use any CPU-specific SIMD instructions.
472 uint32_t rotated = c & uint32_t(0x7f7f7f7fL);
473 rotated += uint32_t(0x25252525L);
474 rotated &= uint32_t(0x7f7f7f7fL);
475 rotated += uint32_t(0x1a1a1a1aL);
477 // Step 5 involves a shift, so some bits will spill over from each
478 // 8-bit value into the next. But that's okay, because they're bits
479 // that will be cleared by the mask in step 6 anyway.
482 rotated &= uint32_t(0x20202020L);
486 void toLowerAscii64(uint64_t& c) {
487 // 64-bit version of toLower32
488 uint64_t rotated = c & uint64_t(0x7f7f7f7f7f7f7f7fL);
489 rotated += uint64_t(0x2525252525252525L);
490 rotated &= uint64_t(0x7f7f7f7f7f7f7f7fL);
491 rotated += uint64_t(0x1a1a1a1a1a1a1a1aL);
494 rotated &= uint64_t(0x2020202020202020L);
500 void toLowerAscii(char* str, size_t length) {
501 static const size_t kAlignMask64 = 7;
502 static const size_t kAlignMask32 = 3;
504 // Convert a character at a time until we reach an address that
505 // is at least 32-bit aligned
506 size_t n = (size_t)str;
508 n = std::min(n, length);
511 n = std::min(4 - n, length);
513 toLowerAscii8(str[offset]);
515 } while (offset < n);
518 n = (size_t)(str + offset);
520 if ((n != 0) && (offset + 4 <= length)) {
521 // The next address is 32-bit aligned but not 64-bit aligned.
522 // Convert the next 4 bytes in order to get to the 64-bit aligned
523 // part of the input.
524 toLowerAscii32(*(uint32_t*)(str + offset));
528 // Convert 8 characters at a time
529 while (offset + 8 <= length) {
530 toLowerAscii64(*(uint64_t*)(str + offset));
534 // Convert 4 characters at a time
535 while (offset + 4 <= length) {
536 toLowerAscii32(*(uint32_t*)(str + offset));
540 // Convert any characters remaining after the last 4-byte aligned group
541 while (offset < length) {
542 toLowerAscii8(str[offset]);
549 size_t hexDumpLine(const void* ptr, size_t offset, size_t size,
551 static char hexValues[] = "0123456789abcdef";
555 // (1+2)*16: hex bytes, each preceded by a space
556 // 1: space separating the two halves
563 const uint8_t* p = reinterpret_cast<const uint8_t*>(ptr) + offset;
564 size_t n = std::min(size - offset, size_t(16));
565 line.push_back(hexValues[(offset >> 28) & 0xf]);
566 line.push_back(hexValues[(offset >> 24) & 0xf]);
567 line.push_back(hexValues[(offset >> 20) & 0xf]);
568 line.push_back(hexValues[(offset >> 16) & 0xf]);
569 line.push_back(hexValues[(offset >> 12) & 0xf]);
570 line.push_back(hexValues[(offset >> 8) & 0xf]);
571 line.push_back(hexValues[(offset >> 4) & 0xf]);
572 line.push_back(hexValues[offset & 0xf]);
575 for (size_t i = 0; i < n; i++) {
581 line.push_back(hexValues[(p[i] >> 4) & 0xf]);
582 line.push_back(hexValues[p[i] & 0xf]);
585 // 3 spaces for each byte we're not printing, one separating the halves
587 line.append(3 * (16 - n) + (n <= 8), ' ');
590 for (size_t i = 0; i < n; i++) {
591 char c = (p[i] >= 32 && p[i] <= 126 ? static_cast<char>(p[i]) : '.');
594 line.append(16 - n, ' ');
596 DCHECK_EQ(line.size(), 78u);
601 } // namespace detail
603 std::string stripLeftMargin(std::string s) {
604 std::vector<StringPiece> pieces;
605 split("\n", s, pieces);
606 auto piecer = range(pieces);
608 auto piece = (piecer.end() - 1);
609 auto needle = std::find_if(piece->begin(),
611 [](char c) { return c != ' ' && c != '\t'; });
612 if (needle == piece->end()) {
613 (piecer.end() - 1)->clear();
615 piece = piecer.begin();
616 needle = std::find_if(piece->begin(),
618 [](char c) { return c != ' ' && c != '\t'; });
619 if (needle == piece->end()) {
620 piecer.erase(piecer.begin(), piecer.begin() + 1);
623 const auto sentinel = std::numeric_limits<size_t>::max();
624 auto indent = sentinel;
625 size_t max_length = 0;
626 for (piece = piecer.begin(); piece != piecer.end(); piece++) {
627 needle = std::find_if(piece->begin(),
629 [](char c) { return c != ' ' && c != '\t'; });
630 if (needle != piece->end()) {
631 indent = std::min<size_t>(indent, size_t(needle - piece->begin()));
633 max_length = std::max<size_t>(piece->size(), max_length);
636 indent = indent == sentinel ? max_length : indent;
637 for (piece = piecer.begin(); piece != piecer.end(); piece++) {
638 if (piece->size() < indent) {
641 piece->erase(piece->begin(), piece->begin() + indent);
644 return join("\n", piecer);
649 #ifdef FOLLY_DEFINED_DMGL
650 # undef FOLLY_DEFINED_DMGL
657 # undef DMGL_RET_POSTFIX