cmake: fix path to FindGLog.cmake
[folly.git] / folly / String.cpp
1 /*
2  * Copyright 2012-present Facebook, Inc.
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
17 #include <folly/String.h>
18
19 #include <cctype>
20 #include <cerrno>
21 #include <cstdarg>
22 #include <cstring>
23 #include <iterator>
24 #include <stdexcept>
25
26 #include <glog/logging.h>
27
28 #include <folly/ScopeGuard.h>
29
30 namespace folly {
31
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, "");
37
38 static inline bool is_oddspace(char c) {
39   return c == '\n' || c == '\t' || c == '\r';
40 }
41
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.
46
47 loop:
48   for (; !sp.empty() && sp.front() == ' '; sp.pop_front()) {
49   }
50   if (!sp.empty() && is_oddspace(sp.front())) {
51     sp.pop_front();
52     goto loop;
53   }
54
55   return sp;
56 }
57
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.
62
63 loop:
64   for (; !sp.empty() && sp.back() == ' '; sp.pop_back()) {
65   }
66   if (!sp.empty() && is_oddspace(sp.back())) {
67     sp.pop_back();
68     goto loop;
69   }
70
71   return sp;
72 }
73
74 namespace {
75
76 int stringAppendfImplHelper(char* buf,
77                             size_t bufsize,
78                             const char* format,
79                             va_list args) {
80   va_list args_copy;
81   va_copy(args_copy, args);
82   int bytes_used = vsnprintf(buf, bufsize, format, args_copy);
83   va_end(args_copy);
84   return bytes_used;
85 }
86
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.
91   //
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
97   // stack variable.
98   std::array<char, 128> inline_buffer;
99
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: ",
106         format));
107   }
108
109   if (static_cast<size_t>(bytes_used) < inline_buffer.size()) {
110     output.append(inline_buffer.data(), size_t(bytes_used));
111     return;
112   }
113
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);
122
123   // We don't keep the trailing '\0' in our output string
124   output.append(heap_buffer.get(), size_t(final_bytes_used));
125 }
126
127 } // namespace
128
129 std::string stringPrintf(const char* format, ...) {
130   va_list ap;
131   va_start(ap, format);
132   SCOPE_EXIT {
133     va_end(ap);
134   };
135   return stringVPrintf(format, ap);
136 }
137
138 std::string stringVPrintf(const char* format, va_list ap) {
139   std::string ret;
140   stringAppendfImpl(ret, format, ap);
141   return ret;
142 }
143
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, ...) {
147   va_list ap;
148   va_start(ap, format);
149   SCOPE_EXIT {
150     va_end(ap);
151   };
152   return stringVAppendf(output, format, ap);
153 }
154
155 std::string& stringVAppendf(std::string* output,
156                             const char* format,
157                             va_list ap) {
158   stringAppendfImpl(*output, format, ap);
159   return *output;
160 }
161
162 void stringPrintf(std::string* output, const char* format, ...) {
163   va_list ap;
164   va_start(ap, format);
165   SCOPE_EXIT {
166     va_end(ap);
167   };
168   return stringVPrintf(output, format, ap);
169 }
170
171 void stringVPrintf(std::string* output, const char* format, va_list ap) {
172   output->clear();
173   stringAppendfImpl(*output, format, ap);
174 };
175
176 namespace {
177
178 struct PrettySuffix {
179   const char* suffix;
180   double val;
181 };
182
183 const PrettySuffix kPrettyTimeSuffixes[] = {
184   { "s ", 1e0L },
185   { "ms", 1e-3L },
186   { "us", 1e-6L },
187   { "ns", 1e-9L },
188   { "ps", 1e-12L },
189   { "s ", 0 },
190   { nullptr, 0 },
191 };
192
193 const PrettySuffix kPrettyBytesMetricSuffixes[] = {
194   { "TB", 1e12L },
195   { "GB", 1e9L },
196   { "MB", 1e6L },
197   { "kB", 1e3L },
198   { "B ", 0L },
199   { nullptr, 0 },
200 };
201
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 },
207   { "B ", 0L },
208   { nullptr, 0 },
209 };
210
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 },
216   { "B  ", 0L },
217   { nullptr, 0 },
218 };
219
220 const PrettySuffix kPrettyUnitsMetricSuffixes[] = {
221   { "tril", 1e12L },
222   { "bil",  1e9L },
223   { "M",    1e6L },
224   { "k",    1e3L },
225   { " ",      0  },
226   { nullptr, 0 },
227 };
228
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 },
234   { " ", 0 },
235   { nullptr, 0 },
236 };
237
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 },
243   { "  ", 0 },
244   { nullptr, 0 },
245 };
246
247 const PrettySuffix kPrettySISuffixes[] = {
248   { "Y", 1e24L },
249   { "Z", 1e21L },
250   { "E", 1e18L },
251   { "P", 1e15L },
252   { "T", 1e12L },
253   { "G", 1e9L },
254   { "M", 1e6L },
255   { "k", 1e3L },
256   { "h", 1e2L },
257   { "da", 1e1L },
258   { "d", 1e-1L },
259   { "c", 1e-2L },
260   { "m", 1e-3L },
261   { "u", 1e-6L },
262   { "n", 1e-9L },
263   { "p", 1e-12L },
264   { "f", 1e-15L },
265   { "a", 1e-18L },
266   { "z", 1e-21L },
267   { "y", 1e-24L },
268   { " ", 0 },
269   { nullptr, 0}
270 };
271
272 const PrettySuffix* const kPrettySuffixes[PRETTY_NUM_TYPES] = {
273   kPrettyTimeSuffixes,
274   kPrettyBytesMetricSuffixes,
275   kPrettyBytesBinarySuffixes,
276   kPrettyBytesBinaryIECSuffixes,
277   kPrettyUnitsMetricSuffixes,
278   kPrettyUnitsBinarySuffixes,
279   kPrettyUnitsBinaryIECSuffixes,
280   kPrettySISuffixes,
281 };
282
283 } // namespace
284
285 std::string prettyPrint(double val, PrettyType type, bool addSpace) {
286   char buf[100];
287
288   // pick the suffixes to use
289   assert(type >= 0);
290   assert(type < PRETTY_NUM_TYPES);
291   const PrettySuffix* suffixes = kPrettySuffixes[type];
292
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)
299                                 : val),
300                (addSpace ? " " : ""),
301                suffixes[i].suffix);
302       return std::string(buf);
303     }
304   }
305
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);
309 }
310
311 //TODO:
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
318   }
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
326         bestPrefixId = j;
327       }
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;
334         bestPrefixId = j;
335       }
336     }
337   }
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(), "\""));
342   }
343   prettyString->advance(size_t(longestPrefixLen));
344   return suffixes[bestPrefixId].val ? value * suffixes[bestPrefixId].val :
345                                       value;
346 }
347
348 double prettyToDouble(folly::StringPiece prettyString, const PrettyType type){
349   double result = prettyToDouble(&prettyString, type);
350   detail::enforceWhitespace(prettyString);
351   return result;
352 }
353
354 std::string hexDump(const void* ptr, size_t size) {
355   std::ostringstream os;
356   hexDump(ptr, size, std::ostream_iterator<StringPiece>(os, "\n"));
357   return os.str();
358 }
359
360 fbstring errnoStr(int err) {
361   int savedErrno = errno;
362
363   // Ensure that we reset errno upon exit.
364   auto guard(makeGuard([&] { errno = savedErrno; }));
365
366   char buf[1024];
367   buf[0] = '\0';
368
369   fbstring result;
370
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);
378   if (r != 0) {
379     result = to<fbstring>(
380       "Unknown error ", err,
381       " (strerror_r failed with error ", errno, ")");
382   } else {
383     result.assign(buf);
384   }
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));
389
390   // OSX/FreeBSD use EINVAL and Linux uses -1 so just check for non-zero
391   if (r != 0) {
392     result = to<fbstring>(
393       "Unknown error ", err,
394       " (strerror_r failed with error ", errno, ")");
395   } else {
396     result.assign(buf);
397   }
398 #else
399   // Using GNU strerror_r
400   result.assign(strerror_r(err, buf, sizeof(buf)));
401 #endif
402
403   return result;
404 }
405
406 namespace {
407
408 void toLowerAscii8(char& c) {
409   // Branchless tolower, based on the input-rotating trick described
410   // at http://www.azillionmonkeys.com/qed/asmexample.html
411   //
412   // This algorithm depends on an observation: each uppercase
413   // ASCII character can be converted to its lowercase equivalent
414   // by adding 0x20.
415
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:
419   //   below 'A':   0- 64
420   //   'A'-'Z':    65- 90
421   //   above 'Z':  91-127
422
423   // Step 2: Add 0x25 (37)
424   rotated += 0x25;
425   // Now the value of rotated, as a function of the original c is:
426   //   below 'A':   37-101
427   //   'A'-'Z':    102-127
428   //   above 'Z':  128-164
429
430   // Step 3: clear the high order bit
431   rotated &= 0x7f;
432   //   below 'A':   37-101
433   //   'A'-'Z':    102-127
434   //   above 'Z':    0- 36
435
436   // Step 4: Add 0x1a (26)
437   rotated += 0x1a;
438   //   below 'A':   63-127
439   //   'A'-'Z':    128-153
440   //   above 'Z':   25- 62
441
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).
444
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.
453   rotated &= ~c;
454   rotated >>= 2;
455
456   // Step 6: Apply a mask to clear everything except the 0x20 bit
457   // in rotated.
458   rotated &= 0x20;
459
460   // At this point, rotated is 0x20 if c is 'A'-'Z' and 0x00 otherwise
461
462   // Step 7: Add rotated to c
463   c += char(rotated);
464 }
465
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);
476
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.
480   rotated &= ~c;
481   rotated >>= 2;
482   rotated &= uint32_t(0x20202020L);
483   c += rotated;
484 }
485
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);
492   rotated &= ~c;
493   rotated >>= 2;
494   rotated &= uint64_t(0x2020202020202020L);
495   c += rotated;
496 }
497
498 } // namespace
499
500 void toLowerAscii(char* str, size_t length) {
501   static const size_t kAlignMask64 = 7;
502   static const size_t kAlignMask32 = 3;
503
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;
507   n &= kAlignMask32;
508   n = std::min(n, length);
509   size_t offset = 0;
510   if (n != 0) {
511     n = std::min(4 - n, length);
512     do {
513       toLowerAscii8(str[offset]);
514       offset++;
515     } while (offset < n);
516   }
517
518   n = (size_t)(str + offset);
519   n &= kAlignMask64;
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));
525     offset += 4;
526   }
527
528   // Convert 8 characters at a time
529   while (offset + 8 <= length) {
530     toLowerAscii64(*(uint64_t*)(str + offset));
531     offset += 8;
532   }
533
534   // Convert 4 characters at a time
535   while (offset + 4 <= length) {
536     toLowerAscii32(*(uint32_t*)(str + offset));
537     offset += 4;
538   }
539
540   // Convert any characters remaining after the last 4-byte aligned group
541   while (offset < length) {
542     toLowerAscii8(str[offset]);
543     offset++;
544   }
545 }
546
547 namespace detail {
548
549 size_t hexDumpLine(const void* ptr, size_t offset, size_t size,
550                    std::string& line) {
551   static char hexValues[] = "0123456789abcdef";
552   // Line layout:
553   // 8: address
554   // 1: space
555   // (1+2)*16: hex bytes, each preceded by a space
556   // 1: space separating the two halves
557   // 3: "  |"
558   // 16: characters
559   // 1: "|"
560   // Total: 78
561   line.clear();
562   line.reserve(78);
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]);
573   line.push_back(' ');
574
575   for (size_t i = 0; i < n; i++) {
576     if (i == 8) {
577       line.push_back(' ');
578     }
579
580     line.push_back(' ');
581     line.push_back(hexValues[(p[i] >> 4) & 0xf]);
582     line.push_back(hexValues[p[i] & 0xf]);
583   }
584
585   // 3 spaces for each byte we're not printing, one separating the halves
586   // if necessary
587   line.append(3 * (16 - n) + (n <= 8), ' ');
588   line.append("  |");
589
590   for (size_t i = 0; i < n; i++) {
591     char c = (p[i] >= 32 && p[i] <= 126 ? static_cast<char>(p[i]) : '.');
592     line.push_back(c);
593   }
594   line.append(16 - n, ' ');
595   line.push_back('|');
596   DCHECK_EQ(line.size(), 78u);
597
598   return n;
599 }
600
601 } // namespace detail
602
603 std::string stripLeftMargin(std::string s) {
604   std::vector<StringPiece> pieces;
605   split("\n", s, pieces);
606   auto piecer = range(pieces);
607
608   auto piece = (piecer.end() - 1);
609   auto needle = std::find_if(piece->begin(),
610                              piece->end(),
611                              [](char c) { return c != ' ' && c != '\t'; });
612   if (needle == piece->end()) {
613     (piecer.end() - 1)->clear();
614   }
615   piece = piecer.begin();
616   needle = std::find_if(piece->begin(),
617                         piece->end(),
618                         [](char c) { return c != ' ' && c != '\t'; });
619   if (needle == piece->end()) {
620     piecer.erase(piecer.begin(), piecer.begin() + 1);
621   }
622
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(),
628                           piece->end(),
629                           [](char c) { return c != ' ' && c != '\t'; });
630     if (needle != piece->end()) {
631       indent = std::min<size_t>(indent, size_t(needle - piece->begin()));
632     } else {
633       max_length = std::max<size_t>(piece->size(), max_length);
634     }
635   }
636   indent = indent == sentinel ? max_length : indent;
637   for (piece = piecer.begin(); piece != piecer.end(); piece++) {
638     if (piece->size() < indent) {
639       piece->clear();
640     } else {
641       piece->erase(piece->begin(), piece->begin() + indent);
642     }
643   }
644   return join("\n", piecer);
645 }
646
647 } // namespace folly
648
649 #ifdef FOLLY_DEFINED_DMGL
650 # undef FOLLY_DEFINED_DMGL
651 # undef DMGL_NO_OPTS
652 # undef DMGL_PARAMS
653 # undef DMGL_ANSI
654 # undef DMGL_JAVA
655 # undef DMGL_VERBOSE
656 # undef DMGL_TYPES
657 # undef DMGL_RET_POSTFIX
658 #endif